Rahul-Samedavar commited on
Commit
ba9555f
·
1 Parent(s): 95d6996

removed few insights

Browse files
Files changed (2) hide show
  1. main.py +77 -0
  2. utils.py +35 -0
main.py CHANGED
@@ -8,6 +8,7 @@ from auth import hash_password, verify_password, create_token
8
  from deps import get_db, get_current_user
9
  from config import API_BASE_URL
10
  from models import User, Log, CtrReport, Restaurant, Review
 
11
 
12
 
13
  app = FastAPI()
@@ -279,4 +280,80 @@ def get_few_reviews(
279
  "total_reviews": len(reviews),
280
  "selected_count": len(selected_reviews),
281
  "selected_reviews": selected_reviews
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
282
  }
 
8
  from deps import get_db, get_current_user
9
  from config import API_BASE_URL
10
  from models import User, Log, CtrReport, Restaurant, Review
11
+ from utils import extract_points
12
 
13
 
14
  app = FastAPI()
 
280
  "total_reviews": len(reviews),
281
  "selected_count": len(selected_reviews),
282
  "selected_reviews": selected_reviews
283
+ }
284
+
285
+
286
+
287
+ @app.get("/few-insights")
288
+ def get_all_insights(
289
+ eps: float = 0.41,
290
+ min_samples: int = 2,
291
+ db: Session = Depends(get_db)
292
+ ):
293
+
294
+ reports = (
295
+ db.query(CtrReport)
296
+ .order_by(CtrReport.id.desc())
297
+ .all()
298
+ )
299
+
300
+ if not reports:
301
+ return {
302
+ "count": 0,
303
+ "insights": [],
304
+ "selected_insights": []
305
+ }
306
+
307
+ # flatten all insight points
308
+ all_points = []
309
+
310
+ for report in reports:
311
+
312
+ if not report.insights:
313
+ continue
314
+
315
+ extracted = extract_points(report.insights)
316
+
317
+ all_points.extend(extracted)
318
+
319
+ # remove duplicates while preserving order
320
+ unique_points = list(dict.fromkeys(all_points))
321
+
322
+ selected_insights = []
323
+
324
+ if unique_points:
325
+
326
+ try:
327
+ response = requests.post(
328
+ f"{API_BASE_URL}/get_representatives",
329
+ json={
330
+ "texts": unique_points,
331
+ "eps": eps,
332
+ "min_samples": min_samples
333
+ },
334
+ timeout=60
335
+ )
336
+
337
+ except requests.RequestException as exc:
338
+ raise HTTPException(
339
+ 502,
340
+ f"Representative insight service failed: {exc}"
341
+ )
342
+
343
+ if response.status_code != 200:
344
+ raise HTTPException(
345
+ 502,
346
+ "Representative insight service error"
347
+ )
348
+
349
+ payload = response.json()
350
+
351
+ selected_insights = payload.get(
352
+ "representatives",
353
+ []
354
+ )
355
+
356
+ return {
357
+ "count": len(selected_insights),
358
+ "selected_insights": selected_insights
359
  }
utils.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+
3
+ def extract_points(markdown_text: str) -> list[str]:
4
+ """
5
+ Extracts numbered/bulleted markdown points as clean strings.
6
+
7
+ Removes:
8
+ - markdown bold/italic/code formatting
9
+ - numbering/bullets
10
+ - extra spaces/newlines
11
+ """
12
+
13
+ lines = markdown_text.splitlines()
14
+ points = []
15
+
16
+ for line in lines:
17
+ line = line.strip()
18
+
19
+ if not line:
20
+ continue
21
+
22
+ # Match numbered or bulleted list items
23
+ if re.match(r"^(\d+\.\s+|[-*]\s+)", line):
24
+
25
+ # Remove numbering/bullets
26
+ line = re.sub(r"^(\d+\.\s+|[-*]\s+)", "", line)
27
+
28
+ # Remove markdown formatting
29
+ line = re.sub(r"\*\*(.*?)\*\*", r"\1", line) # bold
30
+ line = re.sub(r"\*(.*?)\*", r"\1", line) # italic
31
+ line = re.sub(r"`(.*?)`", r"\1", line) # inline code
32
+
33
+ points.append(line.strip())
34
+
35
+ return points