Hasitha16 commited on
Commit
413509a
Β·
verified Β·
1 Parent(s): b34787f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -25
main.py CHANGED
@@ -5,7 +5,7 @@ from fastapi.openapi.docs import get_swagger_ui_html
5
  from fastapi.middleware.cors import CORSMiddleware
6
  from pydantic import BaseModel
7
  from transformers import pipeline
8
- import os, logging, traceback
9
  from model import summarize_review, smart_summarize
10
  from typing import Optional, List
11
 
@@ -52,7 +52,6 @@ class ReviewInput(BaseModel):
52
  aspects: bool = False
53
  follow_up: Optional[str] = None
54
  product_category: Optional[str] = None
55
- device: Optional[str] = None
56
  intelligence: Optional[bool] = False
57
  verbosity: Optional[str] = "detailed"
58
  explain: Optional[bool] = False
@@ -63,15 +62,14 @@ class BulkReviewInput(BaseModel):
63
  industry: Optional[List[str]] = None
64
  aspects: bool = False
65
  product_category: Optional[List[str]] = None
66
- device: Optional[List[str]] = None
67
 
68
  VALID_API_KEY = "my-secret-key"
69
  logging.basicConfig(level=logging.INFO)
70
-
71
  sentiment_pipeline = pipeline("sentiment-analysis")
72
 
73
  def auto_fill(value: Optional[str], default: str = "Generic") -> str:
74
- if not value or value.lower() == "auto-detect":
75
  return default
76
  return value
77
 
@@ -84,32 +82,20 @@ async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
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.")
 
5
  from fastapi.middleware.cors import CORSMiddleware
6
  from pydantic import BaseModel
7
  from transformers import pipeline
8
+ import logging, traceback
9
  from model import summarize_review, smart_summarize
10
  from typing import Optional, List
11
 
 
52
  aspects: bool = False
53
  follow_up: Optional[str] = None
54
  product_category: Optional[str] = None
 
55
  intelligence: Optional[bool] = False
56
  verbosity: Optional[str] = "detailed"
57
  explain: Optional[bool] = False
 
62
  industry: Optional[List[str]] = None
63
  aspects: bool = False
64
  product_category: Optional[List[str]] = None
65
+ intelligence: Optional[bool] = False
66
 
67
  VALID_API_KEY = "my-secret-key"
68
  logging.basicConfig(level=logging.INFO)
 
69
  sentiment_pipeline = pipeline("sentiment-analysis")
70
 
71
  def auto_fill(value: Optional[str], default: str = "Generic") -> str:
72
+ if not value or value.strip().lower() == "auto-detect":
73
  return default
74
  return value
75
 
 
82
  raise HTTPException(status_code=400, detail="⚠️ Review too short for analysis (min. 10 words).")
83
 
84
  try:
85
+ summary = smart_summarize(data.text) if data.intelligence else summarize_review(data.text)
86
+ if data.verbosity.lower() == "brief":
87
+ summary = summary.split(".")[0].strip() + "."
 
 
 
 
 
 
 
 
 
 
88
 
89
+ sentiment = sentiment_pipeline(data.text)[0]
 
90
 
91
  return {
92
  "summary": summary,
93
  "sentiment": sentiment,
94
+ "emotion": "joy", # placeholder
95
+ "product_category": auto_fill(data.product_category, "General"),
96
+ "industry": auto_fill(data.industry, "Generic")
 
97
  }
98
 
99
+ except Exception:
100
  logging.error(f"πŸ”₯ Unexpected analysis failure: {traceback.format_exc()}")
101
  raise HTTPException(status_code=500, detail="Internal Server Error during analysis. Please contact support.")