adAstra144 commited on
Commit
70c785c
·
verified ·
1 Parent(s): 71fc0be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -92,14 +92,26 @@ def explain(req: ExplainRequest):
92
  response = requests.post(url, headers=headers, json=payload, timeout=20)
93
  response.raise_for_status()
94
  result = response.json()
 
 
 
 
 
 
 
 
95
  reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
96
  if not reply:
 
97
  reply = "[No explanation returned]"
98
  logger.info("Explanation generated successfully")
99
  return {"reply": reply}
100
  except requests.RequestException as e:
101
- logger.error(f"OpenRouter error in /explain: {e}")
102
  raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")
 
 
 
103
 
104
  @app.post("/classify")
105
  def classify(req: ExplainRequest):
@@ -137,17 +149,28 @@ def classify(req: ExplainRequest):
137
  response = requests.post(url, headers=headers, json=payload, timeout=20)
138
  response.raise_for_status()
139
  result = response.json()
 
 
 
 
 
 
 
 
140
  reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
141
 
142
  if not reply:
143
- logger.error("OpenRouter returned empty response")
144
  raise HTTPException(status_code=500, detail="No response from OpenRouter")
145
 
146
  logger.info(f"Classification successful with model {model_id}: {reply}")
147
  return {"reply": reply, "status": response.status_code, "model": model_id}
148
  except requests.RequestException as e:
149
- logger.error(f"OpenRouter error in /classify: {e}")
150
  raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")
 
 
 
151
 
152
  # Optional: Run with uvicorn
153
  if __name__ == "__main__":
 
92
  response = requests.post(url, headers=headers, json=payload, timeout=20)
93
  response.raise_for_status()
94
  result = response.json()
95
+
96
+ # Check for OpenRouter error in response body (even with 200 status)
97
+ if "error" in result:
98
+ error_detail = result.get("error", {}).get("message", str(result.get("error")))
99
+ logger.error(f"OpenRouter returned error in /explain: {error_detail}")
100
+ logger.error(f"Full response: {result}")
101
+ raise HTTPException(status_code=500, detail=f"OpenRouter error: {error_detail}")
102
+
103
  reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
104
  if not reply:
105
+ logger.error(f"OpenRouter returned empty response in /explain. Full result: {result}")
106
  reply = "[No explanation returned]"
107
  logger.info("Explanation generated successfully")
108
  return {"reply": reply}
109
  except requests.RequestException as e:
110
+ logger.error(f"OpenRouter network error in /explain: {e}")
111
  raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")
112
+ except Exception as e:
113
+ logger.error(f"Unexpected error in /explain: {e}")
114
+ raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
115
 
116
  @app.post("/classify")
117
  def classify(req: ExplainRequest):
 
149
  response = requests.post(url, headers=headers, json=payload, timeout=20)
150
  response.raise_for_status()
151
  result = response.json()
152
+
153
+ # Check for OpenRouter error in response body (even with 200 status)
154
+ if "error" in result:
155
+ error_detail = result.get("error", {}).get("message", str(result.get("error")))
156
+ logger.error(f"OpenRouter returned error in /classify: {error_detail}")
157
+ logger.error(f"Full response: {result}")
158
+ raise HTTPException(status_code=500, detail=f"OpenRouter error: {error_detail}")
159
+
160
  reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
161
 
162
  if not reply:
163
+ logger.error(f"OpenRouter returned empty response in /classify. Full result: {result}")
164
  raise HTTPException(status_code=500, detail="No response from OpenRouter")
165
 
166
  logger.info(f"Classification successful with model {model_id}: {reply}")
167
  return {"reply": reply, "status": response.status_code, "model": model_id}
168
  except requests.RequestException as e:
169
+ logger.error(f"OpenRouter network error in /classify: {e}")
170
  raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")
171
+ except Exception as e:
172
+ logger.error(f"Unexpected error in /classify: {e}")
173
+ raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")
174
 
175
  # Optional: Run with uvicorn
176
  if __name__ == "__main__":