Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -18,11 +18,13 @@ from deep_translator import GoogleTranslator
|
|
18 |
from datetime import datetime
|
19 |
from theme import theme
|
20 |
from typing import Tuple
|
|
|
21 |
from fastapi import FastAPI
|
22 |
|
23 |
app = FastAPI()
|
24 |
|
25 |
-
|
|
|
26 |
|
27 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
28 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
@@ -97,6 +99,63 @@ examples = [
|
|
97 |
"a beautiful woman with black hair and brown eyes",
|
98 |
]
|
99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
css = """
|
101 |
#app-container {
|
102 |
max-width: 930px;
|
|
|
18 |
from datetime import datetime
|
19 |
from theme import theme
|
20 |
from typing import Tuple
|
21 |
+
from mistralai import Mistral
|
22 |
from fastapi import FastAPI
|
23 |
|
24 |
app = FastAPI()
|
25 |
|
26 |
+
api_key = os.getenv("MISTRAL_API_KEY")
|
27 |
+
Mistralclient = Mistral(api_key=api_key)
|
28 |
|
29 |
API_TOKEN = os.getenv("HF_READ_TOKEN")
|
30 |
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
|
|
99 |
"a beautiful woman with black hair and brown eyes",
|
100 |
]
|
101 |
|
102 |
+
def encode_image(image_path):
|
103 |
+
"""Encode the image to base64."""
|
104 |
+
try:
|
105 |
+
# Open the image file
|
106 |
+
image = Image.open(image_path).convert("RGB")
|
107 |
+
|
108 |
+
# Resize the image to a height of 512 while maintaining the aspect ratio
|
109 |
+
base_height = 512
|
110 |
+
h_percent = (base_height / float(image.size[1]))
|
111 |
+
w_size = int((float(image.size[0]) * float(h_percent)))
|
112 |
+
image = image.resize((w_size, base_height), Image.LANCZOS)
|
113 |
+
|
114 |
+
# Convert the image to a byte stream
|
115 |
+
buffered = BytesIO()
|
116 |
+
image.save(buffered, format="JPEG")
|
117 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
118 |
+
|
119 |
+
return img_str
|
120 |
+
except FileNotFoundError:
|
121 |
+
print(f"Error: The file {image_path} was not found.")
|
122 |
+
return None
|
123 |
+
except Exception as e: # Add generic exception handling
|
124 |
+
print(f"Error: {e}")
|
125 |
+
return None
|
126 |
+
|
127 |
+
def feifeichat(image):
|
128 |
+
try:
|
129 |
+
model = "pixtral-large-2411"
|
130 |
+
# Define the messages for the chat
|
131 |
+
base64_image = encode_image(image)
|
132 |
+
messages = [{
|
133 |
+
"role":
|
134 |
+
"user",
|
135 |
+
"content": [
|
136 |
+
{
|
137 |
+
"type": "text",
|
138 |
+
"text": "Please provide a detailed description of this photo"
|
139 |
+
},
|
140 |
+
{
|
141 |
+
"type": "image_url",
|
142 |
+
"image_url": f"data:image/jpeg;base64,{base64_image}"
|
143 |
+
},
|
144 |
+
],
|
145 |
+
"stream": False,
|
146 |
+
}]
|
147 |
+
|
148 |
+
partial_message = ""
|
149 |
+
for chunk in Mistralclient.chat.stream(model=model, messages=messages):
|
150 |
+
if chunk.data.choices[0].delta.content is not None:
|
151 |
+
partial_message = partial_message + chunk.data.choices[
|
152 |
+
0].delta.content
|
153 |
+
yield partial_message
|
154 |
+
except Exception as e: # 添加通用异常处理
|
155 |
+
print(f"Error: {e}")
|
156 |
+
return "Please upload a photo"
|
157 |
+
|
158 |
+
|
159 |
css = """
|
160 |
#app-container {
|
161 |
max-width: 930px;
|