Spaces:
Running
Running
File size: 2,538 Bytes
e996610 96fe355 50093fa 7cbb5f2 320c744 c328c76 7cbb5f2 96fe355 c0dcd29 21c6bce c0dcd29 ae50a12 c0dcd29 96fe355 c0dcd29 96fe355 7cbb5f2 b1f16e2 7ec2144 96fe355 7ec2144 96fe355 7ec2144 659a7c4 7ec2144 73adad0 b1f16e2 7ec2144 4d69588 6b323fa 4fa4a41 1b7fe97 7af0b15 6172e67 7af0b15 6172e67 6b323fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
from PIL import Image
from io import BytesIO
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:
# Open the image file
image = Image.open(image_path).convert("RGB")
# Resize the image to a height of 512 while maintaining the aspect ratio
base_height = 512
h_percent = (base_height / float(image.size[1]))
w_size = int((float(image.size[0]) * float(h_percent)))
image = image.resize((w_size, base_height), Image.LANCZOS)
# Convert the image to a byte stream
buffered = BytesIO()
image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return img_str
except FileNotFoundError:
print(f"Error: The file {image_path} was not found.")
return None
except Exception as e: # Add generic exception handling
print(f"Error: {e}")
return None
def feifeichat(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"
with gr.Blocks() as demo:
gr.Markdown("Image To Flux Prompt")
with gr.Tab(label="Image To Flux Prompt"):
input_img = gr.Image(label="Input Picture",height=320,type="filepath")
submit_btn = gr.Button(value="Submit")
output_text = gr.Textbox(label="Flux Prompt")
submit_btn.click(feifeichat, [input_img], [output_text])
demo.launch()
|