DigiP-AI commited on
Commit
5f89994
·
verified ·
1 Parent(s): 50c8e25

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -71
app.py CHANGED
@@ -24,62 +24,6 @@ timeout = 100
24
  api_key = os.getenv("MISTRAL_API_KEY")
25
  Mistralclient = Mistral(api_key=api_key)
26
 
27
- def encode_image(image_path):
28
- """Encode the image to base64."""
29
- try:
30
- # Open the image file
31
- image = Image.open(image_path).convert("RGB")
32
-
33
- # Resize the image to a height of 512 while maintaining the aspect ratio
34
- base_height = 512
35
- h_percent = (base_height / float(image.size[1]))
36
- w_size = int((float(image.size[0]) * float(h_percent)))
37
- image = image.resize((w_size, base_height), Image.LANCZOS)
38
-
39
- # Convert the image to a byte stream
40
- buffered = BytesIO()
41
- image.save(buffered, format="JPEG")
42
- img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
43
-
44
- return img_str
45
- except FileNotFoundError:
46
- print(f"Error: The file {image_path} was not found.")
47
- return None
48
- except Exception as e: # Add generic exception handling
49
- print(f"Error: {e}")
50
- return None
51
-
52
- def feifeichat(image):
53
- try:
54
- model = "pixtral-large-2411"
55
- # Define the messages for the chat
56
- base64_image = encode_image(image)
57
- messages = [{
58
- "role":
59
- "user",
60
- "content": [
61
- {
62
- "type": "text",
63
- "text": "Please provide a detailed description of this photo"
64
- },
65
- {
66
- "type": "image_url",
67
- "image_url": f"data:image/jpeg;base64,{base64_image}"
68
- },
69
- ],
70
- "stream": False,
71
- }]
72
-
73
- partial_message = ""
74
- for chunk in Mistralclient.chat.stream(model=model, messages=messages):
75
- if chunk.data.choices[0].delta.content is not None:
76
- partial_message = partial_message + chunk.data.choices[
77
- 0].delta.content
78
- yield partial_message
79
- except Exception as e: # 添加通用异常处理
80
- print(f"Error: {e}")
81
- return "Please upload a photo"
82
-
83
  def clear():
84
  return None
85
 
@@ -138,7 +82,63 @@ examples = [
138
  "a beautiful woman with blonde hair and blue eyes",
139
  "a beautiful woman with brown hair and grey eyes",
140
  "a beautiful woman with black hair and brown eyes",
141
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
  # CSS to style the app
144
  css = """
@@ -154,18 +154,7 @@ footer{display:none !important}
154
  with gr.Blocks(theme=theme, css=css) as app:
155
  # Add a title to the app
156
  gr.HTML("<center><h1>🎨 Stable Diffusion 3.5 large turbo 🇬🇧</h1></center>")
157
- with gr.Tab(label="Image To Prompt"):
158
- with gr.Row():
159
- with gr.Column():
160
- input_img = gr.Image(label="Input Picture",height=320,type="filepath")
161
- submit_btn = gr.Button(value="Submit", variant='primary')
162
- with gr.Column():
163
- output_text = gr.Textbox(label="Flux Prompt", show_copy_button = True)
164
- clr_button =gr.Button("Clear",variant="primary", elem_id="clear_button")
165
- clr_button.click(lambda: gr.Textbox(value=""), None, output_text)
166
-
167
- submit_btn.click(feifeichat, [input_img], [output_text])
168
-
169
  with gr.Tabs() as tabs:
170
  with gr.TabItem("Text to Image"):
171
  # Container for all the UI elements
@@ -211,9 +200,21 @@ with gr.Blocks(theme=theme, css=css) as app:
211
 
212
  # Bind the button to the query function with the added width and height inputs
213
  text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output)
 
 
 
 
 
 
 
 
 
 
214
 
215
-
216
 
 
 
217
  if __name__ == "__main__":
218
- app.launch()
219
 
 
24
  api_key = os.getenv("MISTRAL_API_KEY")
25
  Mistralclient = Mistral(api_key=api_key)
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def clear():
28
  return None
29
 
 
82
  "a beautiful woman with blonde hair and blue eyes",
83
  "a beautiful woman with brown hair and grey eyes",
84
  "a beautiful woman with black hair and brown eyes",
85
+ ]
86
+
87
+ def encode_image(image_path):
88
+ """Encode the image to base64."""
89
+ try:
90
+ # Open the image file
91
+ image = Image.open(image_path).convert("RGB")
92
+
93
+ # Resize the image to a height of 512 while maintaining the aspect ratio
94
+ base_height = 512
95
+ h_percent = (base_height / float(image.size[1]))
96
+ w_size = int((float(image.size[0]) * float(h_percent)))
97
+ image = image.resize((w_size, base_height), Image.LANCZOS)
98
+
99
+ # Convert the image to a byte stream
100
+ buffered = BytesIO()
101
+ image.save(buffered, format="JPEG")
102
+ img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
103
+
104
+ return img_str
105
+ except FileNotFoundError:
106
+ print(f"Error: The file {image_path} was not found.")
107
+ return None
108
+ except Exception as e: # Add generic exception handling
109
+ print(f"Error: {e}")
110
+ return None
111
+
112
+ def feifeichat(image):
113
+ try:
114
+ model = "pixtral-large-2411"
115
+ # Define the messages for the chat
116
+ base64_image = encode_image(image)
117
+ messages = [{
118
+ "role":
119
+ "user",
120
+ "content": [
121
+ {
122
+ "type": "text",
123
+ "text": "Please provide a detailed description of this photo"
124
+ },
125
+ {
126
+ "type": "image_url",
127
+ "image_url": f"data:image/jpeg;base64,{base64_image}"
128
+ },
129
+ ],
130
+ "stream": False,
131
+ }]
132
+
133
+ partial_message = ""
134
+ for chunk in Mistralclient.chat.stream(model=model, messages=messages):
135
+ if chunk.data.choices[0].delta.content is not None:
136
+ partial_message = partial_message + chunk.data.choices[
137
+ 0].delta.content
138
+ yield partial_message
139
+ except Exception as e: # 添加通用异常处理
140
+ print(f"Error: {e}")
141
+ return "Please upload a photo"
142
 
143
  # CSS to style the app
144
  css = """
 
154
  with gr.Blocks(theme=theme, css=css) as app:
155
  # Add a title to the app
156
  gr.HTML("<center><h1>🎨 Stable Diffusion 3.5 large turbo 🇬🇧</h1></center>")
157
+
 
 
 
 
 
 
 
 
 
 
 
158
  with gr.Tabs() as tabs:
159
  with gr.TabItem("Text to Image"):
160
  # Container for all the UI elements
 
200
 
201
  # Bind the button to the query function with the added width and height inputs
202
  text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=image_output)
203
+
204
+ with gr.Tab(label="Image To Prompt"):
205
+ with gr.Row():
206
+ with gr.Column():
207
+ input_img = gr.Image(label="Input Picture",height=320,type="filepath")
208
+ submit_btn = gr.Button(value="Submit", variant='primary')
209
+ with gr.Column():
210
+ output_text = gr.Textbox(label="Flux Prompt", show_copy_button = True)
211
+ clr_button =gr.Button("Clear",variant="primary", elem_id="clear_button")
212
+ clr_button.click(lambda: gr.Textbox(value=""), None, output_text)
213
 
214
+ submit_btn.click(feifeichat, [input_img], [output_text])
215
 
216
+
217
+ app.queue(default_concurrency_limit=200, max_size=200) # <-- Sets up a queue with default parameters
218
  if __name__ == "__main__":
219
+ app.launch(show_api=False, share=False)
220