Hasitha16 commited on
Commit
e430cc4
Β·
verified Β·
1 Parent(s): b809a1b

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -18
main.py CHANGED
@@ -5,12 +5,12 @@ 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 logging, traceback
9
- from model import summarize_review, smart_summarize
10
  from typing import Optional, List
11
 
12
  app = FastAPI(
13
- title="🧠 NeuroPulse AI",
14
  description="Multilingual GenAI for smarter feedback β€” summarization, sentiment, emotion, aspects, Q&A and tags.",
15
  version="2025.1.0",
16
  openapi_url="/openapi.json",
@@ -35,7 +35,7 @@ async def exception_handler(request: Request, exc: Exception):
35
  def custom_swagger_ui():
36
  return get_swagger_ui_html(
37
  openapi_url=app.openapi_url,
38
- title="🧠 Swagger UI - NeuroPulse AI",
39
  swagger_favicon_url="https://cdn-icons-png.flaticon.com/512/3794/3794616.png",
40
  swagger_js_url="https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js",
41
  swagger_css_url="https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui.css",
@@ -52,6 +52,7 @@ class ReviewInput(BaseModel):
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,40 +63,48 @@ class BulkReviewInput(BaseModel):
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
-
76
  @app.post("/analyze/")
77
  async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
78
  if x_api_key != VALID_API_KEY:
79
  raise HTTPException(status_code=401, detail="❌ Unauthorized: Invalid API key")
80
-
81
  if len(data.text.split()) < 10:
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.")
 
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, detect_industry, detect_product_category, answer_followup
10
  from typing import Optional, List
11
 
12
  app = FastAPI(
13
+ title="\U0001f9e0 NeuroPulse AI",
14
  description="Multilingual GenAI for smarter feedback β€” summarization, sentiment, emotion, aspects, Q&A and tags.",
15
  version="2025.1.0",
16
  openapi_url="/openapi.json",
 
35
  def custom_swagger_ui():
36
  return get_swagger_ui_html(
37
  openapi_url=app.openapi_url,
38
+ title="\U0001f9e0 Swagger UI - NeuroPulse AI",
39
  swagger_favicon_url="https://cdn-icons-png.flaticon.com/512/3794/3794616.png",
40
  swagger_js_url="https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js",
41
  swagger_css_url="https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui.css",
 
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
  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
  sentiment_pipeline = pipeline("sentiment-analysis")
71
 
 
 
 
 
 
72
  @app.post("/analyze/")
73
  async def analyze(data: ReviewInput, x_api_key: str = Header(None)):
74
  if x_api_key != VALID_API_KEY:
75
  raise HTTPException(status_code=401, detail="❌ Unauthorized: Invalid API key")
 
76
  if len(data.text.split()) < 10:
77
  raise HTTPException(status_code=400, detail="⚠️ Review too short for analysis (min. 10 words).")
78
 
79
  try:
80
+ # Smart summary logic based on verbosity and intelligence
81
  if data.verbosity.lower() == "brief":
82
+ summary = summarize_review(data.text)
83
+ else:
84
+ summary = smart_summarize(data.text, n_clusters=2 if data.intelligence else 1)
85
 
86
  sentiment = sentiment_pipeline(data.text)[0]
87
+ emotion = "joy" # Placeholder
88
+
89
+ # Auto-detection logic
90
+ industry = data.industry if data.industry and data.industry.lower() != "auto-detect" else detect_industry(data.text)
91
+ product_category = data.product_category if data.product_category and data.product_category.lower() != "auto-detect" else detect_product_category(data.text)
92
+ device = "Web"
93
+
94
+ follow_up_response = None
95
+ if data.follow_up:
96
+ follow_up_response = answer_followup(data.text, data.follow_up)
97
 
98
  return {
99
  "summary": summary,
100
  "sentiment": sentiment,
101
+ "emotion": emotion,
102
+ "product_category": product_category,
103
+ "device": device,
104
+ "industry": industry,
105
+ "follow_up": follow_up_response
106
  }
107
 
108
+ except Exception as e:
109
  logging.error(f"πŸ”₯ Unexpected analysis failure: {traceback.format_exc()}")
110
+ raise HTTPException(status_code=500, detail="Internal Server Error during analysis. Please contact support.")