Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -18,27 +18,21 @@ from deep_translator import GoogleTranslator
|
|
18 |
from datetime import datetime
|
19 |
from theme import theme
|
20 |
from typing import Tuple
|
21 |
-
from mistralai import Mistral
|
22 |
from fastapi import FastAPI
|
23 |
|
24 |
app = FastAPI()
|
25 |
|
26 |
|
|
|
27 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
28 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
29 |
timeout = 100
|
30 |
|
31 |
-
api_key = os.getenv("MISTRAL_KEY")
|
32 |
-
Mistralclient = Mistral(api_key=api_key)
|
33 |
-
|
34 |
def flip_image(x):
|
35 |
return np.fliplr(x)
|
36 |
|
37 |
def clear():
|
38 |
-
return None
|
39 |
-
|
40 |
-
def change_tab():
|
41 |
-
return gr.Tabs.update(selected=1)
|
42 |
|
43 |
def query(lora_id, prompt, is_negative=False, steps=28, cfg_scale=3.5, sampler="DPM++ 2M Karras", seed=-1, strength=100, width=896, height=1152):
|
44 |
if prompt == "" or prompt == None:
|
@@ -103,85 +97,18 @@ examples = [
|
|
103 |
"a beautiful woman with black hair and brown eyes",
|
104 |
]
|
105 |
|
106 |
-
def encode_image(image_path):
|
107 |
-
"""Encode the image to base64."""
|
108 |
-
try:
|
109 |
-
# Open the image file
|
110 |
-
image = Image.open(image_path).convert("RGB")
|
111 |
-
|
112 |
-
# Resize the image to a height of 512 while maintaining the aspect ratio
|
113 |
-
base_height = 512
|
114 |
-
h_percent = (base_height / float(image.size[1]))
|
115 |
-
w_size = int((float(image.size[0]) * float(h_percent)))
|
116 |
-
image = image.resize((w_size, base_height), Image.LANCZOS)
|
117 |
-
|
118 |
-
# Convert the image to a byte stream
|
119 |
-
buffered = BytesIO()
|
120 |
-
image.save(buffered, format="JPEG")
|
121 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
122 |
-
|
123 |
-
return img_str
|
124 |
-
except FileNotFoundError:
|
125 |
-
print(f"Error: The file {image_path} was not found.")
|
126 |
-
return None
|
127 |
-
except Exception as e: # Add generic exception handling
|
128 |
-
print(f"Error: {e}")
|
129 |
-
return None
|
130 |
-
|
131 |
-
def feifeichat(image):
|
132 |
-
try:
|
133 |
-
model = "pixtral-large-2411"
|
134 |
-
# Define the messages for the chat
|
135 |
-
base64_image = encode_image(image)
|
136 |
-
messages = [{
|
137 |
-
"role":
|
138 |
-
"user",
|
139 |
-
"content": [
|
140 |
-
{
|
141 |
-
"type": "text",
|
142 |
-
"text": "Please provide a detailed description of this photo"
|
143 |
-
},
|
144 |
-
{
|
145 |
-
"type": "image_url",
|
146 |
-
"image_url": f"data:image/jpeg;base64,{base64_image}"
|
147 |
-
},
|
148 |
-
],
|
149 |
-
"stream": False,
|
150 |
-
}]
|
151 |
-
|
152 |
-
partial_message = ""
|
153 |
-
for chunk in Mistralclient.chat.stream(model=model, messages=messages):
|
154 |
-
if chunk.data.choices[0].delta.content is not None:
|
155 |
-
partial_message = partial_message + chunk.data.choices[
|
156 |
-
0].delta.content
|
157 |
-
yield partial_message
|
158 |
-
except Exception as e: # 添加通用异常处理
|
159 |
-
print(f"Error: {e}")
|
160 |
-
return "Please upload a photo"
|
161 |
-
|
162 |
-
|
163 |
css = """
|
164 |
-
|
165 |
-
|
166 |
-
|
|
|
|
|
|
|
167 |
|
168 |
"""
|
169 |
with gr.Blocks(theme=theme, css=css, elem_id="app-container") as app:
|
170 |
gr.HTML("<center><h6>🎨 FLUX.1-Dev with LoRA 🇬🇧</h6></center>")
|
171 |
-
with gr.
|
172 |
-
with gr.TabItem(label="Image To Prompt", visible=True, id=1):
|
173 |
-
with gr.Row():
|
174 |
-
with gr.Column():
|
175 |
-
input_img = gr.Image(label="Input Picture 🖼️",height=320,type="filepath")
|
176 |
-
submit_btn = gr.Button(value="Submit", variant='primary')
|
177 |
-
with gr.Column():
|
178 |
-
output_text = gr.Textbox(label="Flux Prompt ✍️", show_copy_button = True)
|
179 |
-
clr_button =gr.Button("Clear 🗑️ ",variant="primary", elem_id="clear_button")
|
180 |
-
clr_button.click(lambda: (None, None), None, [input_img, output_text], queue=False, show_api=False)
|
181 |
-
|
182 |
-
submit_btn.click(feifeichat, [input_img], [output_text])
|
183 |
-
|
184 |
-
with gr.TabItem(label="Text to Image", visible=True, id=0):
|
185 |
with gr.Column(elem_id="app-container"):
|
186 |
with gr.Row():
|
187 |
with gr.Column(elem_id="prompt-container"):
|
@@ -254,7 +181,7 @@ with gr.Blocks(theme=theme, css=css, elem_id="app-container") as app:
|
|
254 |
|
255 |
text_button.click(query, inputs=[custom_lora, text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=[image_output, seed_output])
|
256 |
|
257 |
-
with gr.
|
258 |
with gr.Row():
|
259 |
image_input = gr.Image()
|
260 |
image_output = gr.Image(format="png")
|
@@ -262,7 +189,7 @@ with gr.Blocks(theme=theme, css=css, elem_id="app-container") as app:
|
|
262 |
image_button = gr.Button("Run", variant='primary')
|
263 |
image_button.click(flip_image, inputs=image_input, outputs=image_output, concurrency_limit=2)
|
264 |
|
265 |
-
with gr.
|
266 |
with gr.Row():
|
267 |
gr.Markdown(
|
268 |
"""
|
|
|
18 |
from datetime import datetime
|
19 |
from theme import theme
|
20 |
from typing import Tuple
|
|
|
21 |
from fastapi import FastAPI
|
22 |
|
23 |
app = FastAPI()
|
24 |
|
25 |
|
26 |
+
|
27 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
28 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
29 |
timeout = 100
|
30 |
|
|
|
|
|
|
|
31 |
def flip_image(x):
|
32 |
return np.fliplr(x)
|
33 |
|
34 |
def clear():
|
35 |
+
return None
|
|
|
|
|
|
|
36 |
|
37 |
def query(lora_id, prompt, is_negative=False, steps=28, cfg_scale=3.5, sampler="DPM++ 2M Karras", seed=-1, strength=100, width=896, height=1152):
|
38 |
if prompt == "" or prompt == None:
|
|
|
97 |
"a beautiful woman with black hair and brown eyes",
|
98 |
]
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
css = """
|
101 |
+
#app-container {
|
102 |
+
max-width: 930px;
|
103 |
+
margin-left: auto;
|
104 |
+
margin-right: auto;
|
105 |
+
}
|
106 |
+
".gradio-container {background: url('file=abstract.jpg')}
|
107 |
|
108 |
"""
|
109 |
with gr.Blocks(theme=theme, css=css, elem_id="app-container") as app:
|
110 |
gr.HTML("<center><h6>🎨 FLUX.1-Dev with LoRA 🇬🇧</h6></center>")
|
111 |
+
with gr.Tab("Text to Image"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
with gr.Column(elem_id="app-container"):
|
113 |
with gr.Row():
|
114 |
with gr.Column(elem_id="prompt-container"):
|
|
|
181 |
|
182 |
text_button.click(query, inputs=[custom_lora, text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=[image_output, seed_output])
|
183 |
|
184 |
+
with gr.Tab("Flip Image"):
|
185 |
with gr.Row():
|
186 |
image_input = gr.Image()
|
187 |
image_output = gr.Image(format="png")
|
|
|
189 |
image_button = gr.Button("Run", variant='primary')
|
190 |
image_button.click(flip_image, inputs=image_input, outputs=image_output, concurrency_limit=2)
|
191 |
|
192 |
+
with gr.Tab("Tips"):
|
193 |
with gr.Row():
|
194 |
gr.Markdown(
|
195 |
"""
|