aifeifei798 commited on
Commit
96fe355
·
verified ·
1 Parent(s): b388521

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -4
app.py CHANGED
@@ -1,17 +1,28 @@
1
- import gradio as gr
 
2
  import os
3
  from mistralai import Mistral
4
 
5
-
6
-
7
  api_key = os.getenv("MISTRAL_API_KEY")
8
  Mistralclient = Mistral(api_key=api_key)
9
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def feifeichat(image):
12
  try:
13
  model = "pixtral-large-2411"
14
  # Define the messages for the chat
 
15
  messages = [{
16
  "role":
17
  "user",
@@ -22,7 +33,7 @@ def feifeichat(image):
22
  },
23
  {
24
  "type": "image_url",
25
- "image_url": f"data:image/jpeg;base64,{image}"
26
  },
27
  ],
28
  }]
 
1
+ import base64
2
+ import requests
3
  import os
4
  from mistralai import Mistral
5
 
 
 
6
  api_key = os.getenv("MISTRAL_API_KEY")
7
  Mistralclient = Mistral(api_key=api_key)
8
 
9
+ def encode_image(image_path):
10
+ """Encode the image to base64."""
11
+ try:
12
+ with open(image_path, "rb") as image_file:
13
+ return base64.b64encode(image_file.read()).decode('utf-8')
14
+ except FileNotFoundError:
15
+ print(f"Error: The file {image_path} was not found.")
16
+ return None
17
+ except Exception as e: # Added general exception handling
18
+ print(f"Error: {e}")
19
+ return None
20
 
21
  def feifeichat(image):
22
  try:
23
  model = "pixtral-large-2411"
24
  # Define the messages for the chat
25
+ base64_image = encode_image(image)
26
  messages = [{
27
  "role":
28
  "user",
 
33
  },
34
  {
35
  "type": "image_url",
36
+ "image_url": f"data:image/jpeg;base64,{base64_image}"
37
  },
38
  ],
39
  }]