Hasitha16 commited on
Commit
022e485
·
verified ·
1 Parent(s): 25a23d8

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +50 -0
main.py CHANGED
@@ -245,3 +245,53 @@ async def bulk_analyze(data: BulkReviewInput, token: str = Query(None)):
245
  except Exception as e:
246
  logging.error(f"🔥 Bulk processing failed: {traceback.format_exc()}")
247
  raise HTTPException(status_code=500, detail="Failed to analyze bulk reviews")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  except Exception as e:
246
  logging.error(f"🔥 Bulk processing failed: {traceback.format_exc()}")
247
  raise HTTPException(status_code=500, detail="Failed to analyze bulk reviews")
248
+
249
+ from openai import OpenAI
250
+ openai_client = OpenAI()
251
+
252
+ @app.post("/rootcause/")
253
+ async def root_cause_analysis(payload: dict, x_api_key: str = Header(None)):
254
+ if x_api_key and x_api_key != VALID_API_KEY:
255
+ raise HTTPException(status_code=401, detail="Invalid API key")
256
+
257
+ try:
258
+ text = payload.get("text", "").strip()
259
+ if not text or len(text.split()) < 5:
260
+ raise HTTPException(status_code=400, detail="Insufficient input for root cause analysis.")
261
+
262
+ prompt = f"""
263
+ Analyze the following customer feedback and extract:
264
+ 1. The main problem
265
+ 2. The possible root cause
266
+ 3. A suggested fix or which team might need to handle it
267
+
268
+ Feedback: '''{text}'''
269
+ Format your answer as:
270
+ Problem: ...
271
+ Cause: ...
272
+ Suggestion: ...
273
+ """
274
+
275
+ response = openai_client.chat.completions.create(
276
+ model="gpt-4",
277
+ messages=[{"role": "user", "content": prompt}]
278
+ )
279
+
280
+ output = response.choices[0].message.content
281
+ lines = output.splitlines()
282
+
283
+ def extract_line(tag):
284
+ for line in lines:
285
+ if line.lower().startswith(tag.lower()):
286
+ return line.split(":", 1)[-1].strip()
287
+ return "—"
288
+
289
+ return {
290
+ "problem": extract_line("Problem"),
291
+ "cause": extract_line("Cause"),
292
+ "suggestion": extract_line("Suggestion")
293
+ }
294
+
295
+ except Exception as e:
296
+ logging.error(f"Root cause analysis failed: {traceback.format_exc()}")
297
+ raise HTTPException(status_code=500, detail="Root cause generation failed.")