Spaces:
Running
on
Zero
Running
on
Zero
import base64 | |
import requests | |
import os | |
from mistralai import Mistral | |
import gradio as gr | |
api_key = os.getenv("MISTRAL_API_KEY") | |
Mistralclient = Mistral(api_key=api_key) | |
def encode_image(image_path): | |
"""Encode the image to base64.""" | |
try: | |
with open(image_path, "rb") as image_file: | |
return base64.b64encode(image_file.read()).decode('utf-8') | |
except FileNotFoundError: | |
print(f"Error: The file {image_path} was not found.") | |
return None | |
except Exception as e: # Added general exception handling | |
print(f"Error: {e}") | |
return None | |
def feifeiflorence(image): | |
try: | |
model = "pixtral-large-2411" | |
# Define the messages for the chat | |
base64_image = encode_image(image) | |
messages = [{ | |
"role": | |
"user", | |
"content": [ | |
{ | |
"type": "text", | |
"text": "Please provide a detailed description of this photo" | |
}, | |
{ | |
"type": "image_url", | |
"image_url": f"data:image/jpeg;base64,{base64_image}" | |
}, | |
], | |
"stream": False, | |
}] | |
partial_message = "" | |
for chunk in Mistralclient.chat.stream(model=model, messages=messages): | |
if chunk.data.choices[0].delta.content is not None: | |
partial_message = partial_message + chunk.data.choices[ | |
0].delta.content | |
yield partial_message | |
except Exception as e: # 添加通用异常处理 | |
print(f"Error: {e}") | |
return "Please upload a photo" | |