Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,42 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
import requests
|
|
|
4 |
from io import BytesIO
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
print("Prompt:", prompt)
|
9 |
-
print("Negative Prompt:", negative_prompt)
|
10 |
-
print("Guidance Scale:", guidance_scale)
|
11 |
-
print("Steps:", steps)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
image = enhancer.enhance(1.3)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
20 |
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
35 |
-
description="
|
36 |
)
|
37 |
|
38 |
-
#
|
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)
|