Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -94,7 +94,6 @@
|
|
94 |
|
95 |
# return Response("No audio generated", status_code=400)
|
96 |
|
97 |
-
|
98 |
from fastapi import FastAPI, Response, HTTPException
|
99 |
from fastapi.responses import FileResponse, JSONResponse
|
100 |
from kokoro import KPipeline
|
@@ -146,7 +145,7 @@ def llm_chat_response(text, image_base64=None):
|
|
146 |
|
147 |
logger.info("Initializing InferenceClient...")
|
148 |
client = InferenceClient(
|
149 |
-
provider="
|
150 |
api_key=HF_TOKEN
|
151 |
)
|
152 |
|
@@ -160,7 +159,7 @@ def llm_chat_response(text, image_base64=None):
|
|
160 |
{"role": "system", "content": system_message},
|
161 |
{"role": "user", "content": [
|
162 |
{"type": "text", "text": text if text else "Describe what you see in the image in one line only"},
|
163 |
-
{"type": "
|
164 |
]}
|
165 |
]
|
166 |
else:
|
@@ -179,35 +178,27 @@ def llm_chat_response(text, image_base64=None):
|
|
179 |
|
180 |
logger.info(f"Received response from model")
|
181 |
|
182 |
-
#
|
183 |
if not completion.choices or len(completion.choices) == 0:
|
184 |
logger.error("No choices returned from model.")
|
185 |
raise HTTPException(status_code=500, detail="Model returned no choices.")
|
186 |
|
187 |
-
# Extract the
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
if content is None and hasattr(response_message, "content"):
|
204 |
-
content = response_message.content
|
205 |
-
|
206 |
-
if not content:
|
207 |
-
logger.error(f"Message content is missing: {response_message}")
|
208 |
-
raise HTTPException(status_code=500, detail="Model message did not include content.")
|
209 |
-
|
210 |
-
return content
|
211 |
|
212 |
except Exception as e:
|
213 |
logger.error(f"Error during model inference: {str(e)}")
|
|
|
94 |
|
95 |
# return Response("No audio generated", status_code=400)
|
96 |
|
|
|
97 |
from fastapi import FastAPI, Response, HTTPException
|
98 |
from fastapi.responses import FileResponse, JSONResponse
|
99 |
from kokoro import KPipeline
|
|
|
145 |
|
146 |
logger.info("Initializing InferenceClient...")
|
147 |
client = InferenceClient(
|
148 |
+
provider="together", # Updated to the provider shown in the sample
|
149 |
api_key=HF_TOKEN
|
150 |
)
|
151 |
|
|
|
159 |
{"role": "system", "content": system_message},
|
160 |
{"role": "user", "content": [
|
161 |
{"type": "text", "text": text if text else "Describe what you see in the image in one line only"},
|
162 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}}
|
163 |
]}
|
164 |
]
|
165 |
else:
|
|
|
178 |
|
179 |
logger.info(f"Received response from model")
|
180 |
|
181 |
+
# Simplified response handling based on the sample code
|
182 |
if not completion.choices or len(completion.choices) == 0:
|
183 |
logger.error("No choices returned from model.")
|
184 |
raise HTTPException(status_code=500, detail="Model returned no choices.")
|
185 |
|
186 |
+
# Extract the content directly using the expected format
|
187 |
+
try:
|
188 |
+
# Get message from first choice
|
189 |
+
message = completion.choices[0].message
|
190 |
+
|
191 |
+
# Extract content from message
|
192 |
+
if hasattr(message, "content"):
|
193 |
+
return message.content
|
194 |
+
elif isinstance(message, dict) and "content" in message:
|
195 |
+
return message["content"]
|
196 |
+
else:
|
197 |
+
logger.error(f"Unexpected message format: {message}")
|
198 |
+
raise HTTPException(status_code=500, detail="Unexpected message format from model")
|
199 |
+
except Exception as e:
|
200 |
+
logger.error(f"Error extracting message content: {str(e)}")
|
201 |
+
raise HTTPException(status_code=500, detail=f"Failed to extract response content: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
|
203 |
except Exception as e:
|
204 |
logger.error(f"Error during model inference: {str(e)}")
|