gaur3009 commited on
Commit
075527f
·
verified ·
1 Parent(s): 7135361

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -22
app.py CHANGED
@@ -1,25 +1,19 @@
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
3
- import requests
4
  from PIL import Image
5
- from io import BytesIO
6
 
7
- # Connect to external Hugging Face Space API
8
  hf_client = Client("multimodalart/cosxl")
9
 
10
- # Define your Gradio function
11
  def run_edit(image, prompt):
12
- # Forward other fixed values to the API
13
  negative_prompt = "blurry, distorted"
14
  guidance_scale = 7
15
  steps = 20
16
 
17
- # Save the uploaded PIL image
18
  image_path = "/tmp/uploaded_image.png"
19
  image.save(image_path)
20
 
21
- # Send request to external model
22
- result = hf_client.predict(
23
  image=handle_file(image_path),
24
  prompt=prompt,
25
  negative_prompt=negative_prompt,
@@ -28,29 +22,20 @@ def run_edit(image, prompt):
28
  api_name="/run_edit"
29
  )
30
 
31
- # Extract result image URL and load
32
- result_url = result.get("url")
33
- if result_url is None:
34
- raise ValueError("Failed to get image result from API.")
35
-
36
- response = requests.get(result_url)
37
- edited_image = Image.open(BytesIO(response.content)).convert("RGB")
38
  return edited_image
39
 
40
- # Gradio interface: only ask for image and prompt
41
  interface = gr.Interface(
42
  fn=run_edit,
43
  inputs=[
44
  gr.Image(label="Upload Image", type="pil"),
45
- gr.Textbox(label="Describe the edit (Prompt)", placeholder="e.g. Make the bus pink with clouds")
46
  ],
47
  outputs=gr.Image(label="Edited Image"),
48
- title="Smart Image Editor",
49
- description="Upload an image and describe what to change. The model will edit it intelligently."
50
  )
51
 
52
- # Enable API call from Hugging Face client
53
  interface.api_name = "/run_edit"
54
-
55
- # Launch app
56
  interface.launch()
 
1
  import gradio as gr
2
  from gradio_client import Client, handle_file
 
3
  from PIL import Image
 
4
 
 
5
  hf_client = Client("multimodalart/cosxl")
6
 
 
7
  def run_edit(image, prompt):
 
8
  negative_prompt = "blurry, distorted"
9
  guidance_scale = 7
10
  steps = 20
11
 
 
12
  image_path = "/tmp/uploaded_image.png"
13
  image.save(image_path)
14
 
15
+ # Predict (returns file path)
16
+ result_path = hf_client.predict(
17
  image=handle_file(image_path),
18
  prompt=prompt,
19
  negative_prompt=negative_prompt,
 
22
  api_name="/run_edit"
23
  )
24
 
25
+ # Load and return result
26
+ edited_image = Image.open(result_path).convert("RGB")
 
 
 
 
 
27
  return edited_image
28
 
 
29
  interface = gr.Interface(
30
  fn=run_edit,
31
  inputs=[
32
  gr.Image(label="Upload Image", type="pil"),
33
+ gr.Textbox(label="Prompt", placeholder="Describe what to edit")
34
  ],
35
  outputs=gr.Image(label="Edited Image"),
36
+ title="Image Editor using multimodalart/cosxl",
37
+ description="Uses Hugging Face model for real image editing based on prompt."
38
  )
39
 
 
40
  interface.api_name = "/run_edit"
 
 
41
  interface.launch()