Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,8 @@ from theme import theme
|
|
11 |
from fastapi import FastAPI
|
12 |
|
13 |
app = FastAPI()
|
14 |
-
|
|
|
15 |
|
16 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
17 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
@@ -69,6 +70,62 @@ def query(prompt, is_negative=False, steps=35, cfg_scale=7, sampler="DPM++ 2M Ka
|
|
69 |
print(f"Error when trying to open the image: {e}")
|
70 |
return None
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
# CSS to style the app
|
73 |
css = """
|
74 |
.gradio-container {background-color: MediumAquaMarine}
|
@@ -91,6 +148,18 @@ with gr.Blocks(theme=theme, css=css) as app:
|
|
91 |
# Add a title to the app
|
92 |
gr.HTML("<center><h1>FLUX.1-Dev</h1></center>")
|
93 |
with gr.Tabs() as tabs:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
with gr.TabItem("✍️ Text to Image 🖼", visible=True):
|
95 |
# Container for all the UI elements
|
96 |
with gr.Column(elem_id="app-container"):
|
|
|
11 |
from fastapi import FastAPI
|
12 |
|
13 |
app = FastAPI()
|
14 |
+
|
15 |
+
# Based on a project by Nymbo
|
16 |
|
17 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
18 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
|
|
70 |
print(f"Error when trying to open the image: {e}")
|
71 |
return None
|
72 |
|
73 |
+
def encode_image(image_path):
|
74 |
+
"""Encode the image to base64."""
|
75 |
+
try:
|
76 |
+
# Open the image file
|
77 |
+
image = Image.open(image_path).convert("RGB")
|
78 |
+
|
79 |
+
# Resize the image to a height of 512 while maintaining the aspect ratio
|
80 |
+
base_height = 512
|
81 |
+
h_percent = (base_height / float(image.size[1]))
|
82 |
+
w_size = int((float(image.size[0]) * float(h_percent)))
|
83 |
+
image = image.resize((w_size, base_height), Image.LANCZOS)
|
84 |
+
|
85 |
+
# Convert the image to a byte stream
|
86 |
+
buffered = BytesIO()
|
87 |
+
image.save(buffered, format="JPEG")
|
88 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
89 |
+
|
90 |
+
return img_str
|
91 |
+
except FileNotFoundError:
|
92 |
+
print(f"Error: The file {image_path} was not found.")
|
93 |
+
return None
|
94 |
+
except Exception as e: # Add generic exception handling
|
95 |
+
print(f"Error: {e}")
|
96 |
+
return None
|
97 |
+
|
98 |
+
def feifeichat(image):
|
99 |
+
try:
|
100 |
+
model = "pixtral-large-2411"
|
101 |
+
# Define the messages for the chat
|
102 |
+
base64_image = encode_image(image)
|
103 |
+
messages = [{
|
104 |
+
"role":
|
105 |
+
"user",
|
106 |
+
"content": [
|
107 |
+
{
|
108 |
+
"type": "text",
|
109 |
+
"text": "Please provide a detailed description of this photo"
|
110 |
+
},
|
111 |
+
{
|
112 |
+
"type": "image_url",
|
113 |
+
"image_url": f"data:image/jpeg;base64,{base64_image}"
|
114 |
+
},
|
115 |
+
],
|
116 |
+
"stream": False,
|
117 |
+
}]
|
118 |
+
|
119 |
+
partial_message = ""
|
120 |
+
for chunk in Mistralclient.chat.stream(model=model, messages=messages):
|
121 |
+
if chunk.data.choices[0].delta.content is not None:
|
122 |
+
partial_message = partial_message + chunk.data.choices[
|
123 |
+
0].delta.content
|
124 |
+
yield partial_message
|
125 |
+
except Exception as e: # Adding generic exception handling
|
126 |
+
print(f"Error: {e}")
|
127 |
+
return "Please upload a photo"
|
128 |
+
|
129 |
# CSS to style the app
|
130 |
css = """
|
131 |
.gradio-container {background-color: MediumAquaMarine}
|
|
|
148 |
# Add a title to the app
|
149 |
gr.HTML("<center><h1>FLUX.1-Dev</h1></center>")
|
150 |
with gr.Tabs() as tabs:
|
151 |
+
with gr.TabItem(label="🖼 Image To Prompt 📄", visible=True):
|
152 |
+
with gr.Row():
|
153 |
+
with gr.Column():
|
154 |
+
input_img = gr.Image(label="Input Picture 🖼️",height=320,type="filepath")
|
155 |
+
submit_btn = gr.Button(value="Submit", variant='primary')
|
156 |
+
with gr.Column():
|
157 |
+
output_text = gr.Textbox(label="Flux Prompt ✍️", show_copy_button = True)
|
158 |
+
clr_button =gr.Button("Clear 🗑️ ",variant="primary", elem_id="clear_button")
|
159 |
+
clr_button.click(lambda: (None, None), None, [input_img, output_text], queue=False, show_api=False)
|
160 |
+
|
161 |
+
submit_btn.click(feifeichat, [input_img], [output_text])
|
162 |
+
|
163 |
with gr.TabItem("✍️ Text to Image 🖼", visible=True):
|
164 |
# Container for all the UI elements
|
165 |
with gr.Column(elem_id="app-container"):
|