gaur3009 commited on
Commit
7c9fdc3
·
verified ·
1 Parent(s): c917d73

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -1,26 +1,42 @@
1
  import gradio as gr
2
- from PIL import Image, ImageEnhance
3
  import requests
 
4
  from io import BytesIO
5
 
6
- # Core image edit function (simulate behavior — replace with your model!)
7
- def run_edit(image, prompt, negative_prompt, guidance_scale=7, steps=20):
8
- print("Prompt:", prompt)
9
- print("Negative Prompt:", negative_prompt)
10
- print("Guidance Scale:", guidance_scale)
11
- print("Steps:", steps)
12
 
13
- # Dummy transformation: increase contrast and add a pink overlay
14
- image = image.convert("RGB")
15
- enhancer = ImageEnhance.Contrast(image)
16
- image = enhancer.enhance(1.3)
17
 
18
- pink_overlay = Image.new("RGB", image.size, (255, 182, 193)) # Light pink
19
- edited = Image.blend(image, pink_overlay, alpha=0.2)
 
20
 
21
- return edited
 
 
 
 
 
 
 
 
22
 
23
- # Gradio Interface matching Hugging Face API structure
 
 
 
 
 
 
 
 
 
 
 
24
  interface = gr.Interface(
25
  fn=run_edit,
26
  inputs=[
@@ -31,11 +47,11 @@ interface = gr.Interface(
31
  gr.Slider(label="Steps", value=20, minimum=1, maximum=100)
32
  ],
33
  outputs=gr.Image(label="Your result image"),
34
- title="Image Editing with Prompt",
35
- description="Edit an image using prompts. Adjust the scale and steps as needed.",
36
  )
37
 
38
- # Assign API name so Hugging Face Client can call it via `/run_edit`
39
  interface.api_name = "/run_edit"
40
 
41
  # Launch app (required for HF Spaces)
 
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
+ # Load external Hugging Face Space
8
+ hf_client = Client("multimodalart/cosxl")
 
 
 
 
9
 
10
+ # Function that proxies the input to multimodalart/cosxl Space
11
+ def run_edit(image, prompt, negative_prompt, guidance_scale=7.0, steps=20):
12
+ print("→ Sending to external model...")
 
13
 
14
+ # Save uploaded PIL image to a temp file
15
+ image_path = "/tmp/input_image.png"
16
+ image.save(image_path)
17
 
18
+ # Call the external API using gradio_client
19
+ result = hf_client.predict(
20
+ image=handle_file(image_path),
21
+ prompt=prompt,
22
+ negative_prompt=negative_prompt,
23
+ guidance_scale=guidance_scale,
24
+ steps=steps,
25
+ api_name="/run_edit"
26
+ )
27
 
28
+ # `result` is a dict with a `url` key (or `path`)
29
+ image_url = result.get("url")
30
+ if not image_url:
31
+ raise ValueError("No image returned from remote API.")
32
+
33
+ # Download the result image
34
+ response = requests.get(image_url)
35
+ edited_image = Image.open(BytesIO(response.content)).convert("RGB")
36
+
37
+ return edited_image
38
+
39
+ # Build the Gradio interface
40
  interface = gr.Interface(
41
  fn=run_edit,
42
  inputs=[
 
47
  gr.Slider(label="Steps", value=20, minimum=1, maximum=100)
48
  ],
49
  outputs=gr.Image(label="Your result image"),
50
+ title="Image Editing with Remote API",
51
+ description="This Gradio app sends inputs to the multimodalart/cosxl model and shows the real result.",
52
  )
53
 
54
+ # API name for use via gradio_client
55
  interface.api_name = "/run_edit"
56
 
57
  # Launch app (required for HF Spaces)