Hasitha16 commited on
Commit
45710cd
Β·
verified Β·
1 Parent(s): b9b3bd4

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +30 -15
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 is too short to analyze meaningfully.")
85
 
86
  try:
87
- summary = smart_summarize(data.text) if data.intelligence else summarize_review(data.text)
88
- sentiment = sentiment_pipeline(data.text)[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  except Exception as e:
90
- logging.error(f"Analysis error: {e}")
91
- raise HTTPException(status_code=500, detail="Failed to process text. Please retry.")
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.")