Update app.py
Browse files91 unmodified lines929394959697989910010110210310410531 unmodified lines13713813914014114214314414514614714814915015115215391 unmodified linesresponse = requests.post(url, headers=headers, json=payload, timeout=20)response.raise_for_status()result = response.json()reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()if not reply:reply = "[No explanation returned]"logger.info("Explanation generated successfully")return {"reply": reply}except requests.RequestException as e:logger.error(f"OpenRouter error in /explain: {e}")raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")@app.post("/classify")def classify(req: ExplainRequest):31 unmodified linesresponse = requests.post(url, headers=headers, json=payload, timeout=20)response.raise_for_status()result = response.json()reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()if not reply:logger.error("OpenRouter returned empty response")raise HTTPException(status_code=500, detail="No response from OpenRouter")logger.info(f"Classification successful with model {model_id}: {reply}")return {"reply": reply, "status": response.status_code, "model": model_id}except requests.RequestException as e:logger.error(f"OpenRouter error in /classify: {e}")raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")# Optional: Run with uvicornif __name__ == "__main__":91 unmodified lines929394959697989910010110210310410510610710810911011111211311411511611731 unmodified lines14915015115215315415515615715815916016116216316416516616716816917017117217317417517691 unmodified linesresponse = requests.post(url, headers=headers, json=payload, timeout=20)response.raise_for_status()result = response.json()# Check for OpenRouter error in response body (even with 200 status)if "error" in result:error_detail = result.get("error", {}).get("message", str(result.get("error")))logger.error(f"OpenRouter returned error in /explain: {error_detail}")logger.error(f"Full response: {result}")raise HTTPException(status_code=500, detail=f"OpenRouter error: {error_detail}")reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()if not reply:logger.error(f"OpenRouter returned empty response in /explain. Full result: {result}")reply = "[No explanation returned]"logger.info("Explanation generated successfully")return {"reply": reply}except requests.RequestException as e:logger.error(f"OpenRouter network error in /explain: {e}")raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")except Exception as e:logger.error(f"Unexpected error in /explain: {e}")raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")@app.post("/classify")def classify(req: ExplainRequest):31 unmodified linesresponse = requests.post(url, headers=headers, json=payload, timeout=20)response.raise_for_status()result = response.json()# Check for OpenRouter error in response body (even with 200 status)if "error" in result:error_detail = result.get("error", {}).get("message", str(result.get("error")))logger.error(f"OpenRouter returned error in /classify: {error_detail}")logger.error(f"Full response: {result}")raise HTTPException(status_code=500, detail=f"OpenRouter error: {error_detail}")reply = result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()if not reply:logger.error(f"OpenRouter returned empty response in /classify. Full result: {result}")raise HTTPException(status_code=500, detail="No response from OpenRouter")logger.info(f"Classification successful with model {model_id}: {reply}")return {"reply": reply, "status": response.status_code, "model": model_id}except requests.RequestException as e:logger.error(f"OpenRouter network error in /classify: {e}")raise HTTPException(status_code=500, detail=f"Error contacting OpenRouter: {e}")except Exception as e:logger.error(f"Unexpected error in /classify: {e}")raise HTTPException(status_code=500, detail=f"Unexpected error: {e}")# Optional: Run with uvicornif __name__ == "__main__":
app.py+26-3