khurrameycon commited on
Commit
79aceed
·
verified ·
1 Parent(s): 69eed4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -3
app.py CHANGED
@@ -136,8 +136,8 @@ def llm_chat_response(text: str, image_url: Optional[str] = None) -> str:
136
  api_key=HF_TOKEN
137
  )
138
 
139
- # Build the messages payload dynamically based on whether an image URL is provided.
140
- # If only text is provided, add an instruction for a one-line description.
141
  message_content = [{
142
  "type": "text",
143
  "text": text + ("" if image_url else " describe in one line only")
@@ -160,7 +160,41 @@ def llm_chat_response(text: str, image_url: Optional[str] = None) -> str:
160
  messages=messages,
161
  max_tokens=500
162
  )
163
- return completion.choices[0].message['content']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
  except Exception as e:
166
  logger.error(f"Error in llm_chat_response: {str(e)}")
@@ -198,3 +232,4 @@ async def method_not_allowed_handler(request, exc):
198
  status_code=405,
199
  content={"error": "Method not allowed. Please check the API documentation."}
200
  )
 
 
136
  api_key=HF_TOKEN
137
  )
138
 
139
+ # Build the messages payload dynamically.
140
+ # If only text is provided, we append a default instruction.
141
  message_content = [{
142
  "type": "text",
143
  "text": text + ("" if image_url else " describe in one line only")
 
160
  messages=messages,
161
  max_tokens=500
162
  )
163
+
164
+ # Debug log the raw response for troubleshooting.
165
+ logger.info(f"Raw model response: {completion}")
166
+
167
+ # Ensure we have a valid response.
168
+ if not completion.choices or len(completion.choices) == 0:
169
+ logger.error("No choices returned from model.")
170
+ raise HTTPException(status_code=500, detail="Model returned no choices.")
171
+
172
+ # Extract the message; use dict get to avoid NoneType errors.
173
+ response_message = None
174
+ # Some responses may be dicts or objects; try both approaches.
175
+ choice = completion.choices[0]
176
+ if hasattr(choice, "message"):
177
+ response_message = choice.message
178
+ elif isinstance(choice, dict):
179
+ response_message = choice.get("message")
180
+
181
+ if not response_message:
182
+ logger.error(f"Response message is empty: {choice}")
183
+ raise HTTPException(status_code=500, detail="Model response did not include a message.")
184
+
185
+ # Extract the content from the message.
186
+ content = None
187
+ if isinstance(response_message, dict):
188
+ content = response_message.get("content")
189
+ # If for some reason it's not a dict, try attribute access.
190
+ if content is None and hasattr(response_message, "content"):
191
+ content = response_message.content
192
+
193
+ if not content:
194
+ logger.error(f"Message content is missing: {response_message}")
195
+ raise HTTPException(status_code=500, detail="Model message did not include content.")
196
+
197
+ return content
198
 
199
  except Exception as e:
200
  logger.error(f"Error in llm_chat_response: {str(e)}")
 
232
  status_code=405,
233
  content={"error": "Method not allowed. Please check the API documentation."}
234
  )
235
+