Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -80,37 +80,42 @@ def auto_fill(value: Optional[str], fallback: str) -> str:
|
|
80 |
async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
|
81 |
if x_api_key and x_api_key != VALID_API_KEY:
|
82 |
raise HTTPException(status_code=401, detail="❌ Invalid API key")
|
|
|
83 |
if len(data.text.split()) < 20:
|
84 |
raise HTTPException(status_code=400, detail="⚠️ Review too short for analysis (min. 20 words).")
|
85 |
|
86 |
try:
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
|
94 |
-
|
|
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
100 |
|
101 |
-
|
102 |
if data.follow_up:
|
103 |
follow_up_response = answer_followup(data.text, data.follow_up, verbosity=data.verbosity)
|
|
|
104 |
|
105 |
-
return
|
106 |
-
"summary": summary,
|
107 |
-
"sentiment": sentiment,
|
108 |
-
"emotion": emotion,
|
109 |
-
"product_category": product_category,
|
110 |
-
"device": device,
|
111 |
-
"industry": industry,
|
112 |
-
"follow_up": follow_up_response
|
113 |
-
}
|
114 |
|
115 |
except Exception as e:
|
116 |
logging.error(f"🔥 Unexpected analysis failure: {traceback.format_exc()}")
|
|
|
80 |
async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
|
81 |
if x_api_key and x_api_key != VALID_API_KEY:
|
82 |
raise HTTPException(status_code=401, detail="❌ Invalid API key")
|
83 |
+
|
84 |
if len(data.text.split()) < 20:
|
85 |
raise HTTPException(status_code=400, detail="⚠️ Review too short for analysis (min. 20 words).")
|
86 |
|
87 |
try:
|
88 |
+
response = {}
|
89 |
+
|
90 |
+
# Only perform core analysis if follow-up not present (or first-time analysis)
|
91 |
+
if not data.follow_up:
|
92 |
+
if data.verbosity.lower() == "brief":
|
93 |
+
summary = summarize_review(data.text, max_len=40, min_len=8)
|
94 |
+
else:
|
95 |
+
summary = smart_summarize(data.text, n_clusters=2 if data.intelligence else 1)
|
96 |
+
|
97 |
+
sentiment = sentiment_pipeline(data.text)[0]
|
98 |
+
emotion = "joy"
|
99 |
|
100 |
+
industry = detect_industry(data.text) if not data.industry or "auto" in data.industry.lower() else data.industry
|
101 |
+
product_category = detect_product_category(data.text) if not data.product_category or "auto" in data.product_category.lower() else data.product_category
|
102 |
+
device = "Web"
|
103 |
|
104 |
+
response = {
|
105 |
+
"summary": summary,
|
106 |
+
"sentiment": sentiment,
|
107 |
+
"emotion": emotion,
|
108 |
+
"product_category": product_category,
|
109 |
+
"device": device,
|
110 |
+
"industry": industry
|
111 |
+
}
|
112 |
|
113 |
+
# If follow-up question is asked, generate answer
|
114 |
if data.follow_up:
|
115 |
follow_up_response = answer_followup(data.text, data.follow_up, verbosity=data.verbosity)
|
116 |
+
response["follow_up"] = follow_up_response
|
117 |
|
118 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
except Exception as e:
|
121 |
logging.error(f"🔥 Unexpected analysis failure: {traceback.format_exc()}")
|