Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -78,23 +78,38 @@ def auto_fill(value: Optional[str], default: str = "Generic") -> str:
|
|
78 |
@app.post("/analyze/")
|
79 |
async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
|
80 |
if x_api_key != VALID_API_KEY:
|
81 |
-
raise HTTPException(status_code=401, detail="Unauthorized: Invalid API key")
|
82 |
|
83 |
if len(data.text.split()) < 10:
|
84 |
-
raise HTTPException(status_code=400, detail="Review
|
85 |
|
86 |
try:
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
except Exception as e:
|
90 |
-
logging.error(f"
|
91 |
-
raise HTTPException(status_code=500, detail="
|
92 |
-
|
93 |
-
return {
|
94 |
-
"summary": summary,
|
95 |
-
"sentiment": sentiment,
|
96 |
-
"emotion": "joy",
|
97 |
-
"product_category": auto_fill(data.product_category),
|
98 |
-
"device": auto_fill(data.device, "Web"),
|
99 |
-
"industry": auto_fill(data.industry)
|
100 |
-
}
|
|
|
78 |
@app.post("/analyze/")
|
79 |
async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
|
80 |
if x_api_key != VALID_API_KEY:
|
81 |
+
raise HTTPException(status_code=401, detail="β Unauthorized: Invalid API key")
|
82 |
|
83 |
if len(data.text.split()) < 10:
|
84 |
+
raise HTTPException(status_code=400, detail="β οΈ Review too short for analysis (min. 10 words).")
|
85 |
|
86 |
try:
|
87 |
+
# Summary Generation
|
88 |
+
try:
|
89 |
+
summary = smart_summarize(data.text) if data.intelligence else summarize_review(data.text)
|
90 |
+
except Exception as e:
|
91 |
+
logging.error(f"π Summarization error: {traceback.format_exc()}")
|
92 |
+
raise HTTPException(status_code=500, detail="π§ Failed to generate summary. Please try again.")
|
93 |
+
|
94 |
+
# Sentiment Analysis
|
95 |
+
try:
|
96 |
+
sentiment = sentiment_pipeline(data.text)[0]
|
97 |
+
except Exception as e:
|
98 |
+
logging.error(f"π Sentiment analysis error: {traceback.format_exc()}")
|
99 |
+
raise HTTPException(status_code=500, detail="π Sentiment analysis failed. Please retry.")
|
100 |
+
|
101 |
+
# (Optional future: plug in emotion model)
|
102 |
+
emotion = "joy" # hardcoded placeholder
|
103 |
+
|
104 |
+
return {
|
105 |
+
"summary": summary,
|
106 |
+
"sentiment": sentiment,
|
107 |
+
"emotion": emotion,
|
108 |
+
"product_category": auto_fill(data.product_category),
|
109 |
+
"device": auto_fill(data.device, "Web"),
|
110 |
+
"industry": auto_fill(data.industry)
|
111 |
+
}
|
112 |
+
|
113 |
except Exception as e:
|
114 |
+
logging.error(f"π₯ Unexpected analysis failure: {traceback.format_exc()}")
|
115 |
+
raise HTTPException(status_code=500, detail="Internal Server Error during analysis. Please contact support.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|