Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,46 @@
|
|
1 |
-
# This is a Gradio app that integrates a chat interface with a text-to-image and image editing model.
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import random
|
5 |
import os
|
|
|
|
|
|
|
6 |
from huggingface_hub import InferenceClient
|
|
|
7 |
|
8 |
# --- Constants ---
|
9 |
MAX_SEED = np.iinfo(np.int32).max
|
10 |
|
11 |
-
# ---
|
12 |
-
client =
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# --- Core Inference Function for ChatInterface ---
|
19 |
-
def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps):
|
20 |
"""
|
21 |
Performs image generation or editing based on user input from the chat interface.
|
22 |
"""
|
|
|
|
|
|
|
23 |
prompt = message["text"]
|
24 |
files = message["files"]
|
25 |
|
@@ -34,32 +55,44 @@ def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps):
|
|
34 |
print(f"Received image: {files[0]}")
|
35 |
try:
|
36 |
# Try to open and convert the image
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
except Exception as e:
|
40 |
raise gr.Error(f"Could not process the uploaded image: {str(e)}. Please try uploading a different image format (JPEG, PNG, WebP).")
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
image = client.image_to_image(
|
45 |
-
|
46 |
prompt=prompt,
|
47 |
model="black-forest-labs/FLUX.1-Kontext-dev",
|
48 |
-
guidance_scale
|
49 |
-
|
50 |
-
seed=seed
|
51 |
)
|
|
|
52 |
else:
|
53 |
print(f"Received prompt for text-to-image: {prompt}")
|
|
|
|
|
54 |
image = client.text_to_image(
|
55 |
prompt=prompt,
|
56 |
model="black-forest-labs/FLUX.1-Kontext-dev",
|
57 |
-
guidance_scale
|
58 |
-
|
59 |
-
seed=seed
|
60 |
)
|
61 |
-
|
62 |
-
|
|
|
63 |
return gr.Image(value=image)
|
64 |
|
65 |
# --- UI Definition using gr.ChatInterface ---
|
@@ -69,15 +102,14 @@ randomize_checkbox = gr.Checkbox(label="Randomize seed", value=False)
|
|
69 |
guidance_slider = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=2.5)
|
70 |
steps_slider = gr.Slider(label="Steps", minimum=1, maximum=30, value=28, step=1)
|
71 |
|
72 |
-
#
|
73 |
-
#
|
74 |
-
examples = None
|
75 |
|
76 |
demo = gr.ChatInterface(
|
77 |
fn=chat_fn,
|
78 |
-
title="FLUX.1 Kontext [dev]",
|
79 |
description="""<p style='text-align: center;'>
|
80 |
-
A simple chat UI for the <b>FLUX.1 Kontext</b> model
|
81 |
<br>
|
82 |
To edit an image, upload it and type your instructions (e.g., "Add a hat").
|
83 |
<br>
|
@@ -85,7 +117,7 @@ demo = gr.ChatInterface(
|
|
85 |
<br>
|
86 |
Find the model on <a href='https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev' target='_blank'>Hugging Face</a>.
|
87 |
</p>""",
|
88 |
-
multimodal=True,
|
89 |
textbox=gr.MultimodalTextbox(
|
90 |
file_types=["image"],
|
91 |
placeholder="Type a prompt and/or upload an image...",
|
@@ -97,9 +129,8 @@ demo = gr.ChatInterface(
|
|
97 |
guidance_slider,
|
98 |
steps_slider
|
99 |
],
|
100 |
-
examples=examples,
|
101 |
theme="soft"
|
102 |
)
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
-
demo.launch(
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import random
|
4 |
import os
|
5 |
+
import tempfile
|
6 |
+
from PIL import Image, ImageOps
|
7 |
+
import pillow_heif # For HEIF/AVIF support
|
8 |
from huggingface_hub import InferenceClient
|
9 |
+
import io
|
10 |
|
11 |
# --- Constants ---
|
12 |
MAX_SEED = np.iinfo(np.int32).max
|
13 |
|
14 |
+
# --- Global client variable ---
|
15 |
+
client = None
|
16 |
+
|
17 |
+
def load_client():
|
18 |
+
"""Initialize the Inference Client"""
|
19 |
+
global client
|
20 |
+
if client is None:
|
21 |
+
# Register HEIF opener with PIL for AVIF/HEIF support
|
22 |
+
pillow_heif.register_heif_opener()
|
23 |
+
|
24 |
+
# Get token from environment variable
|
25 |
+
hf_token = os.getenv("HF_TOKEN")
|
26 |
+
if hf_token:
|
27 |
+
client = InferenceClient(
|
28 |
+
provider="fal-ai",
|
29 |
+
api_key=hf_token,
|
30 |
+
bill_to="huggingface",
|
31 |
+
)
|
32 |
+
else:
|
33 |
+
raise gr.Error("HF_TOKEN environment variable not found. Please add your Hugging Face token to the Space settings.")
|
34 |
+
return client
|
35 |
|
36 |
# --- Core Inference Function for ChatInterface ---
|
37 |
+
def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps, progress=gr.Progress()):
|
38 |
"""
|
39 |
Performs image generation or editing based on user input from the chat interface.
|
40 |
"""
|
41 |
+
# Load client
|
42 |
+
client = load_client()
|
43 |
+
|
44 |
prompt = message["text"]
|
45 |
files = message["files"]
|
46 |
|
|
|
55 |
print(f"Received image: {files[0]}")
|
56 |
try:
|
57 |
# Try to open and convert the image
|
58 |
+
input_image = Image.open(files[0])
|
59 |
+
# Convert to RGB if needed (handles RGBA, P, etc.)
|
60 |
+
if input_image.mode != "RGB":
|
61 |
+
input_image = input_image.convert("RGB")
|
62 |
+
# Auto-orient the image based on EXIF data
|
63 |
+
input_image = ImageOps.exif_transpose(input_image)
|
64 |
+
|
65 |
+
# Convert PIL image to bytes for the API
|
66 |
+
img_byte_arr = io.BytesIO()
|
67 |
+
input_image.save(img_byte_arr, format='PNG')
|
68 |
+
input_image_bytes = img_byte_arr.getvalue()
|
69 |
+
|
70 |
except Exception as e:
|
71 |
raise gr.Error(f"Could not process the uploaded image: {str(e)}. Please try uploading a different image format (JPEG, PNG, WebP).")
|
72 |
+
|
73 |
+
# Use image_to_image for editing
|
74 |
+
progress(0.1, desc="Processing image...")
|
75 |
image = client.image_to_image(
|
76 |
+
input_image_bytes,
|
77 |
prompt=prompt,
|
78 |
model="black-forest-labs/FLUX.1-Kontext-dev",
|
79 |
+
# Note: guidance_scale and steps might not be supported by the API
|
80 |
+
# Check the API documentation for available parameters
|
|
|
81 |
)
|
82 |
+
progress(1.0, desc="Complete!")
|
83 |
else:
|
84 |
print(f"Received prompt for text-to-image: {prompt}")
|
85 |
+
# Use text_to_image for generation
|
86 |
+
progress(0.1, desc="Generating image...")
|
87 |
image = client.text_to_image(
|
88 |
prompt=prompt,
|
89 |
model="black-forest-labs/FLUX.1-Kontext-dev",
|
90 |
+
# Note: guidance_scale and steps might not be supported by the API
|
91 |
+
# Check the API documentation for available parameters
|
|
|
92 |
)
|
93 |
+
progress(1.0, desc="Complete!")
|
94 |
+
|
95 |
+
# The client returns a PIL Image object
|
96 |
return gr.Image(value=image)
|
97 |
|
98 |
# --- UI Definition using gr.ChatInterface ---
|
|
|
102 |
guidance_slider = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=10.0, step=0.1, value=2.5)
|
103 |
steps_slider = gr.Slider(label="Steps", minimum=1, maximum=30, value=28, step=1)
|
104 |
|
105 |
+
# Note: The Inference Client API may not support all parameters like guidance_scale and steps
|
106 |
+
# Check the API documentation for supported parameters
|
|
|
107 |
|
108 |
demo = gr.ChatInterface(
|
109 |
fn=chat_fn,
|
110 |
+
title="FLUX.1 Kontext [dev] - Inference Client",
|
111 |
description="""<p style='text-align: center;'>
|
112 |
+
A simple chat UI for the <b>FLUX.1 Kontext</b> model using Hugging Face Inference Client with fal-ai provider.
|
113 |
<br>
|
114 |
To edit an image, upload it and type your instructions (e.g., "Add a hat").
|
115 |
<br>
|
|
|
117 |
<br>
|
118 |
Find the model on <a href='https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev' target='_blank'>Hugging Face</a>.
|
119 |
</p>""",
|
120 |
+
multimodal=True,
|
121 |
textbox=gr.MultimodalTextbox(
|
122 |
file_types=["image"],
|
123 |
placeholder="Type a prompt and/or upload an image...",
|
|
|
129 |
guidance_slider,
|
130 |
steps_slider
|
131 |
],
|
|
|
132 |
theme="soft"
|
133 |
)
|
134 |
|
135 |
if __name__ == "__main__":
|
136 |
+
demo.launch()
|