Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import base64
|
| 3 |
-
from io import BytesIO
|
| 4 |
-
import os
|
| 5 |
from mistralai import Mistral
|
| 6 |
-
|
| 7 |
-
from PIL import Image
|
| 8 |
|
| 9 |
|
| 10 |
api_key = os.getenv("MISTRAL_API_KEY")
|
| 11 |
Mistralclient = Mistral(api_key=api_key)
|
| 12 |
|
| 13 |
-
def encode_image(image_path):
|
| 14 |
-
"""Encode the image to base64."""
|
| 15 |
-
try:
|
| 16 |
-
# 打开图片文件
|
| 17 |
-
image = Image.open(image_path).convert("RGB")
|
| 18 |
-
|
| 19 |
-
# 将图片转换为字节流
|
| 20 |
-
buffered = BytesIO()
|
| 21 |
-
image.save(buffered, format="JPEG")
|
| 22 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 23 |
-
|
| 24 |
-
return img_str
|
| 25 |
-
except FileNotFoundError:
|
| 26 |
-
print(f"Error: The file {image_path} was not found.")
|
| 27 |
-
return None
|
| 28 |
-
except Exception as e: # 添加通用异常处理
|
| 29 |
-
print(f"Error: {e}")
|
| 30 |
-
return None
|
| 31 |
|
| 32 |
def feifeichat(image):
|
| 33 |
try:
|
| 34 |
model = "pixtral-large-2411"
|
| 35 |
-
base64_image = encode_image(image)
|
| 36 |
# Define the messages for the chat
|
| 37 |
messages = [{
|
| 38 |
"role":
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from mistralai import Mistral
|
| 3 |
+
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
api_key = os.getenv("MISTRAL_API_KEY")
|
| 7 |
Mistralclient = Mistral(api_key=api_key)
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def feifeichat(image):
|
| 11 |
try:
|
| 12 |
model = "pixtral-large-2411"
|
|
|
|
| 13 |
# Define the messages for the chat
|
| 14 |
messages = [{
|
| 15 |
"role":
|