DigiP-AI commited on
Commit
69b0ace
·
verified ·
1 Parent(s): 3936671

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py CHANGED
@@ -12,6 +12,7 @@ import io
12
  from io import BytesIO
13
  import base64
14
  import re
 
15
  from gradio_client import Client
16
  from fake_useragent import UserAgent
17
  import random
@@ -20,6 +21,65 @@ from fastapi import FastAPI
20
 
21
  app = FastAPI()
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  def query(prompt, negative_prompt, task, steps, sampler, cfg_scale, seed, width, height):
25
  result = {"prompt": prompt,"negative_prompt": negative_prompt,"task": task,"steps": steps,"sampler": sampler,"cfg_scale": cfg_scale,"seed": seed, "width": width, "height": height}
@@ -191,6 +251,19 @@ height: 100% !important;
191
  """
192
 
193
  with gr.Blocks(css=css, theme=theme) as app:
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
  with gr.Tab("Basic Settings"):
196
  with gr.Row():
 
12
  from io import BytesIO
13
  import base64
14
  import re
15
+ from mistralai import mistralai
16
  from gradio_client import Client
17
  from fake_useragent import UserAgent
18
  import random
 
21
 
22
  app = FastAPI()
23
 
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
 
84
  def query(prompt, negative_prompt, task, steps, sampler, cfg_scale, seed, width, height):
85
  result = {"prompt": prompt,"negative_prompt": negative_prompt,"task": task,"steps": steps,"sampler": sampler,"cfg_scale": cfg_scale,"seed": seed, "width": width, "height": height}
 
251
  """
252
 
253
  with gr.Blocks(css=css, theme=theme) as app:
254
+ with gr.Tab(label="Image To Prompt"):
255
+ with gr.Row():
256
+ with gr.Column(scale=4, min_width=300):
257
+ input_img = gr.Image(label="Input Picture", type="filepath")
258
+
259
+ with gr.Column(scale=3):
260
+ output_text = gr.Textbox(label="Flux Prompt", lines=2, scale=6, show_copy_button = True)
261
+ submit_btn = gr.Button(value="Generate Pompt", scale=4, variant='primary')
262
+ clear_prompt =gr.Button("Clear 🗑️",variant="primary", elem_id="clear_button")
263
+ clear_prompt.click(lambda: (None, None), None, [input_img, output_text], queue=False, show_api=False)
264
+
265
+ submit_btn.click(feifeichat, [input_img], [output_text])
266
+
267
 
268
  with gr.Tab("Basic Settings"):
269
  with gr.Row():