Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,25 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
import spaces
|
4 |
-
import torch
|
5 |
import random
|
6 |
import os
|
7 |
-
import
|
8 |
-
from PIL import Image, ImageOps
|
9 |
-
import pillow_heif # For HEIF/AVIF support
|
10 |
-
|
11 |
-
# Import the pipeline from diffusers
|
12 |
-
from diffusers import FluxKontextPipeline
|
13 |
|
14 |
# --- Constants ---
|
15 |
MAX_SEED = np.iinfo(np.int32).max
|
16 |
|
17 |
-
# ---
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
""
|
22 |
-
|
23 |
-
if pipe is None:
|
24 |
-
# Register HEIF opener with PIL for AVIF/HEIF support
|
25 |
-
pillow_heif.register_heif_opener()
|
26 |
-
|
27 |
-
# Get token from environment variable
|
28 |
-
hf_token = os.getenv("HF_TOKEN")
|
29 |
-
if hf_token:
|
30 |
-
pipe = FluxKontextPipeline.from_pretrained(
|
31 |
-
"black-forest-labs/FLUX.1-Kontext-dev",
|
32 |
-
torch_dtype=torch.bfloat16,
|
33 |
-
token=hf_token,
|
34 |
-
)
|
35 |
-
else:
|
36 |
-
raise gr.Error("HF_TOKEN environment variable not found. Please add your Hugging Face token to the Space settings.")
|
37 |
-
return pipe
|
38 |
|
39 |
# --- Core Inference Function for ChatInterface ---
|
40 |
-
|
41 |
-
def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps, progress=gr.Progress(track_tqdm=True)):
|
42 |
"""
|
43 |
Performs image generation or editing based on user input from the chat interface.
|
44 |
"""
|
45 |
-
# Load and move model to GPU within the decorated function
|
46 |
-
pipe = load_model()
|
47 |
-
pipe = pipe.to("cuda")
|
48 |
-
|
49 |
prompt = message["text"]
|
50 |
files = message["files"]
|
51 |
|
@@ -55,42 +29,36 @@ def chat_fn(message, chat_history, seed, randomize_seed, guidance_scale, steps,
|
|
55 |
if randomize_seed:
|
56 |
seed = random.randint(0, MAX_SEED)
|
57 |
|
58 |
-
generator = torch.Generator(device="cuda").manual_seed(int(seed))
|
59 |
-
|
60 |
input_image = None
|
61 |
if files:
|
62 |
print(f"Received image: {files[0]}")
|
63 |
try:
|
64 |
# Try to open and convert the image
|
65 |
-
|
66 |
-
|
67 |
-
if input_image.mode != "RGB":
|
68 |
-
input_image = input_image.convert("RGB")
|
69 |
-
# Auto-orient the image based on EXIF data
|
70 |
-
input_image = ImageOps.exif_transpose(input_image)
|
71 |
except Exception as e:
|
72 |
raise gr.Error(f"Could not process the uploaded image: {str(e)}. Please try uploading a different image format (JPEG, PNG, WebP).")
|
73 |
-
|
74 |
-
|
75 |
-
|
|
|
|
|
76 |
prompt=prompt,
|
|
|
77 |
guidance_scale=guidance_scale,
|
78 |
num_inference_steps=steps,
|
79 |
-
|
80 |
-
)
|
81 |
else:
|
82 |
print(f"Received prompt for text-to-image: {prompt}")
|
83 |
-
image =
|
84 |
prompt=prompt,
|
|
|
85 |
guidance_scale=guidance_scale,
|
86 |
num_inference_steps=steps,
|
87 |
-
|
88 |
-
)
|
89 |
-
|
90 |
-
# Move model back to CPU to free GPU memory
|
91 |
-
pipe = pipe.to("cpu")
|
92 |
-
torch.cuda.empty_cache()
|
93 |
-
|
94 |
# Return the PIL Image as a Gradio Image component
|
95 |
return gr.Image(value=image)
|
96 |
|
@@ -134,4 +102,4 @@ demo = gr.ChatInterface(
|
|
134 |
)
|
135 |
|
136 |
if __name__ == "__main__":
|
137 |
-
demo.launch()
|
|
|
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 |
+
# --- Initialize Inference Client ---
|
12 |
+
client = InferenceClient(
|
13 |
+
provider="fal-ai",
|
14 |
+
api_key=os.environ["HF_TOKEN"],
|
15 |
+
bill_to="huggingface",
|
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 |
|
|
|
29 |
if randomize_seed:
|
30 |
seed = random.randint(0, MAX_SEED)
|
31 |
|
|
|
|
|
32 |
input_image = None
|
33 |
if files:
|
34 |
print(f"Received image: {files[0]}")
|
35 |
try:
|
36 |
# Try to open and convert the image
|
37 |
+
with open(files[0], "rb") as image_file:
|
38 |
+
input_image = image_file.read()
|
|
|
|
|
|
|
|
|
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 |
+
if input_image:
|
43 |
+
print(f"Received prompt for image editing: {prompt}")
|
44 |
+
image = client.image_to_image(
|
45 |
+
input_image,
|
46 |
prompt=prompt,
|
47 |
+
model="black-forest-labs/FLUX.1-Kontext-dev",
|
48 |
guidance_scale=guidance_scale,
|
49 |
num_inference_steps=steps,
|
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=guidance_scale,
|
58 |
num_inference_steps=steps,
|
59 |
+
seed=seed
|
60 |
+
)
|
61 |
+
|
|
|
|
|
|
|
|
|
62 |
# Return the PIL Image as a Gradio Image component
|
63 |
return gr.Image(value=image)
|
64 |
|
|
|
102 |
)
|
103 |
|
104 |
if __name__ == "__main__":
|
105 |
+
demo.launch(show_error=True)
|