Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -5,6 +5,9 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from datetime import datetime
|
| 7 |
import uuid
|
|
|
|
|
|
|
|
|
|
| 8 |
from transformers import pipeline
|
| 9 |
import logging, traceback
|
| 10 |
from typing import Optional, List, Union
|
|
@@ -16,7 +19,7 @@ from model import (
|
|
| 16 |
)
|
| 17 |
|
| 18 |
app = FastAPI(
|
| 19 |
-
title="π§
|
| 20 |
description="Multilingual GenAI for smarter feedback β summarization, sentiment, emotion, aspects, Q&A and tags.",
|
| 21 |
version="2025.1.0",
|
| 22 |
openapi_url="/openapi.json",
|
|
@@ -38,13 +41,13 @@ log_store = [] # β
Shared in-memory churn log
|
|
| 38 |
|
| 39 |
@app.get("/", response_class=HTMLResponse)
|
| 40 |
def root():
|
| 41 |
-
return "<h1>
|
| 42 |
|
| 43 |
@app.get("/docs", include_in_schema=False)
|
| 44 |
def custom_swagger_ui():
|
| 45 |
return get_swagger_ui_html(
|
| 46 |
openapi_url=app.openapi_url,
|
| 47 |
-
title="π§ Swagger UI -
|
| 48 |
swagger_favicon_url="https://cdn-icons-png.flaticon.com/512/3794/3794616.png",
|
| 49 |
swagger_js_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.18.3/swagger-ui-bundle.js",
|
| 50 |
swagger_css_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.18.3/swagger-ui.css",
|
|
@@ -188,6 +191,9 @@ async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
|
|
| 188 |
sentiment_pipeline = pipeline("sentiment-analysis", model=data.model)
|
| 189 |
|
| 190 |
for i, review_text in enumerate(data.reviews):
|
|
|
|
|
|
|
|
|
|
| 191 |
if len(review_text.split()) < 20:
|
| 192 |
results.append({
|
| 193 |
"review": review_text,
|
|
@@ -195,6 +201,7 @@ async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
|
|
| 195 |
})
|
| 196 |
continue
|
| 197 |
|
|
|
|
| 198 |
summary = smart_summarize(review_text, n_clusters=2 if data.intelligence else 1)
|
| 199 |
sentiment = sentiment_pipeline(review_text)[0]
|
| 200 |
|
|
@@ -246,8 +253,7 @@ async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
|
|
| 246 |
logging.error(f"π₯ Bulk processing failed: {traceback.format_exc()}")
|
| 247 |
raise HTTPException(status_code=500, detail="Failed to analyze bulk reviews")
|
| 248 |
|
| 249 |
-
|
| 250 |
-
openai_client = OpenAI()
|
| 251 |
|
| 252 |
@app.post("/rootcause/")
|
| 253 |
async def root_cause_analysis(payload: dict, x_api_key: str = Header(None)):
|
|
@@ -272,7 +278,7 @@ async def root_cause_analysis(payload: dict, x_api_key: str = Header(None)):
|
|
| 272 |
Suggestion: ...
|
| 273 |
"""
|
| 274 |
|
| 275 |
-
response =
|
| 276 |
model="gpt-4",
|
| 277 |
messages=[{"role": "user", "content": prompt}]
|
| 278 |
)
|
|
@@ -292,6 +298,6 @@ async def root_cause_analysis(payload: dict, x_api_key: str = Header(None)):
|
|
| 292 |
"suggestion": extract_line("Suggestion")
|
| 293 |
}
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from datetime import datetime
|
| 7 |
import uuid
|
| 8 |
+
import os
|
| 9 |
+
import openai
|
| 10 |
+
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
| 11 |
from transformers import pipeline
|
| 12 |
import logging, traceback
|
| 13 |
from typing import Optional, List, Union
|
|
|
|
| 19 |
)
|
| 20 |
|
| 21 |
app = FastAPI(
|
| 22 |
+
title="π§ ChurnSight AI",
|
| 23 |
description="Multilingual GenAI for smarter feedback β summarization, sentiment, emotion, aspects, Q&A and tags.",
|
| 24 |
version="2025.1.0",
|
| 25 |
openapi_url="/openapi.json",
|
|
|
|
| 41 |
|
| 42 |
@app.get("/", response_class=HTMLResponse)
|
| 43 |
def root():
|
| 44 |
+
return "<h1>ChurnSight AI Backend is Running</h1>"
|
| 45 |
|
| 46 |
@app.get("/docs", include_in_schema=False)
|
| 47 |
def custom_swagger_ui():
|
| 48 |
return get_swagger_ui_html(
|
| 49 |
openapi_url=app.openapi_url,
|
| 50 |
+
title="π§ Swagger UI - ChurnSight AI",
|
| 51 |
swagger_favicon_url="https://cdn-icons-png.flaticon.com/512/3794/3794616.png",
|
| 52 |
swagger_js_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.18.3/swagger-ui-bundle.js",
|
| 53 |
swagger_css_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@4.18.3/swagger-ui.css",
|
|
|
|
| 191 |
sentiment_pipeline = pipeline("sentiment-analysis", model=data.model)
|
| 192 |
|
| 193 |
for i, review_text in enumerate(data.reviews):
|
| 194 |
+
if not review_text.strip():
|
| 195 |
+
continue # Skip empty reviews
|
| 196 |
+
|
| 197 |
if len(review_text.split()) < 20:
|
| 198 |
results.append({
|
| 199 |
"review": review_text,
|
|
|
|
| 201 |
})
|
| 202 |
continue
|
| 203 |
|
| 204 |
+
|
| 205 |
summary = smart_summarize(review_text, n_clusters=2 if data.intelligence else 1)
|
| 206 |
sentiment = sentiment_pipeline(review_text)[0]
|
| 207 |
|
|
|
|
| 253 |
logging.error(f"π₯ Bulk processing failed: {traceback.format_exc()}")
|
| 254 |
raise HTTPException(status_code=500, detail="Failed to analyze bulk reviews")
|
| 255 |
|
| 256 |
+
# Already set with os.environ β nothing else needed
|
|
|
|
| 257 |
|
| 258 |
@app.post("/rootcause/")
|
| 259 |
async def root_cause_analysis(payload: dict, x_api_key: str = Header(None)):
|
|
|
|
| 278 |
Suggestion: ...
|
| 279 |
"""
|
| 280 |
|
| 281 |
+
response = openai.ChatCompletion.create(
|
| 282 |
model="gpt-4",
|
| 283 |
messages=[{"role": "user", "content": prompt}]
|
| 284 |
)
|
|
|
|
| 298 |
"suggestion": extract_line("Suggestion")
|
| 299 |
}
|
| 300 |
|
| 301 |
+
except Exception as e:
|
| 302 |
+
logging.error(f"Root cause analysis failed: {traceback.format_exc()}")
|
| 303 |
+
return JSONResponse(status_code=500, content={"detail": f"Root cause generation failed: {str(e)}"})
|