Ali Hashhash commited on
Commit
06803fa
Β·
1 Parent(s): 8a0d292
src/api/notes_routes.py CHANGED
@@ -305,10 +305,10 @@ async def process_video_task(task_id: str, youtube_url: str, language: str, user
305
  if isinstance(seg, dict) and seg.get("key_insight"):
306
  key_points_list.append(seg["key_insight"])
307
 
308
- # Step 2: Generate the categories (keep in memory)
309
- from src.summarization.topic_classifier import classify_topics
310
  raw_topics = summary_json.get("topics", [])
311
- categories = classify_topics(raw_topics) if raw_topics else ["Education & Science"]
312
 
313
  # Step 3: Combine everything into ONE single dictionary
314
  note_data = {
@@ -317,7 +317,7 @@ async def process_video_task(task_id: str, youtube_url: str, language: str, user
317
  "videoTitle": video_title,
318
  "notes": final_markdown,
319
  "thumbnail": f"https://img.youtube.com/vi/{video_id}/mqdefault.jpg" if video_id else "",
320
- "category": categories,
321
  "keyPoints": key_points_list,
322
  "createdAt": datetime.utcnow(),
323
  "updatedAt": datetime.utcnow(),
@@ -328,8 +328,8 @@ async def process_video_task(task_id: str, youtube_url: str, language: str, user
328
  # The Flutter app will perform the final save after user edits.
329
  tasks[task_id]["status"] = "completed"
330
  tasks[task_id]["notes"] = final_markdown
331
- tasks[task_id]["topics"] = categories
332
- tasks[task_id]["category"] = categories
333
  tasks[task_id]["keyPoints"] = key_points_list
334
  logger.info(f"βœ… Task {task_id} completed successfully!")
335
 
 
305
  if isinstance(seg, dict) and seg.get("key_insight"):
306
  key_points_list.append(seg["key_insight"])
307
 
308
+ # Step 2: Generate the single primary category (keep in memory)
309
+ from src.summarization.topic_classifier import get_primary_category
310
  raw_topics = summary_json.get("topics", [])
311
+ category = get_primary_category(raw_topics) if raw_topics else "Education & Science"
312
 
313
  # Step 3: Combine everything into ONE single dictionary
314
  note_data = {
 
317
  "videoTitle": video_title,
318
  "notes": final_markdown,
319
  "thumbnail": f"https://img.youtube.com/vi/{video_id}/mqdefault.jpg" if video_id else "",
320
+ "category": category,
321
  "keyPoints": key_points_list,
322
  "createdAt": datetime.utcnow(),
323
  "updatedAt": datetime.utcnow(),
 
328
  # The Flutter app will perform the final save after user edits.
329
  tasks[task_id]["status"] = "completed"
330
  tasks[task_id]["notes"] = final_markdown
331
+ tasks[task_id]["topics"] = raw_topics
332
+ tasks[task_id]["category"] = category
333
  tasks[task_id]["keyPoints"] = key_points_list
334
  logger.info(f"βœ… Task {task_id} completed successfully!")
335
 
src/summarization/topic_classifier.py CHANGED
@@ -2,11 +2,11 @@
2
  Topic Classifier β€” maps dynamic LLM-extracted topics to predefined UI categories.
3
 
4
  Usage:
5
- from src.summarization.topic_classifier import classify_topics
6
 
7
  topics = ["Python", "Machine Learning", "Neural Networks"]
8
- result = classify_topics(topics)
9
- # => ["Technology & AI"]
10
 
11
  Categories:
12
  Technology & AI | Business & Finance | Education & Science
@@ -199,22 +199,24 @@ _register("Health & Sports", [
199
  # PUBLIC API
200
  # ─────────────────────────────────────────────────────────────────────────────
201
 
202
- def classify_topics(topics: List[str]) -> List[str]:
203
  """
204
- Map a list of dynamically extracted topics to predefined UI categories.
 
 
 
205
 
206
  Args:
207
  topics: List of topic strings from the LLM (e.g. ["Python", "Deep Learning"]).
208
 
209
  Returns:
210
- A deduplicated, ordered list of matching category names.
211
- Falls back to ["Education & Science"] if no match is found.
212
 
213
  Example:
214
- >>> classify_topics(["Python", "Machine Learning", "Neural Networks"])
215
- ["Technology & AI"]
216
- >>> classify_topics(["Investing", "AI Stocks"])
217
- ["Business & Finance", "Technology & AI"]
218
  """
219
  matched: Set[str] = set()
220
 
@@ -233,17 +235,25 @@ def classify_topics(topics: List[str]) -> List[str]:
233
  break
234
 
235
  if not matched:
236
- matched.add("Education & Science")
 
 
 
 
 
237
 
238
- # Return in the same order as CATEGORIES for consistency
239
- return [cat for cat in CATEGORIES if cat in matched]
 
 
 
 
240
 
241
 
242
  def get_primary_category(topics: List[str]) -> str:
243
  """
244
  Return the single best-matching category for the given topics.
245
 
246
- Useful when only one category tag is needed (e.g. a badge in the UI).
247
  """
248
- categories = classify_topics(topics)
249
- return categories[0] if categories else "Education & Science"
 
2
  Topic Classifier β€” maps dynamic LLM-extracted topics to predefined UI categories.
3
 
4
  Usage:
5
+ from src.summarization.topic_classifier import classify_topic, get_primary_category
6
 
7
  topics = ["Python", "Machine Learning", "Neural Networks"]
8
+ result = classify_topic(topics)
9
+ # => "Technology & AI"
10
 
11
  Categories:
12
  Technology & AI | Business & Finance | Education & Science
 
199
  # PUBLIC API
200
  # ─────────────────────────────────────────────────────────────────────────────
201
 
202
+ def classify_topic(topics: List[str]) -> str:
203
  """
204
+ Map a list of dynamically extracted topics to a SINGLE predefined UI category.
205
+
206
+ Returns the single best-matching category (the first match in CATEGORIES order).
207
+ Falls back to "Education & Science" if no match is found.
208
 
209
  Args:
210
  topics: List of topic strings from the LLM (e.g. ["Python", "Deep Learning"]).
211
 
212
  Returns:
213
+ A single category string.
 
214
 
215
  Example:
216
+ >>> classify_topic(["Python", "Machine Learning", "Neural Networks"])
217
+ "Technology & AI"
218
+ >>> classify_topic(["Investing", "AI Stocks"])
219
+ "Business & Finance"
220
  """
221
  matched: Set[str] = set()
222
 
 
235
  break
236
 
237
  if not matched:
238
+ return "Education & Science"
239
+
240
+ # Return the first match in CATEGORIES order for consistency
241
+ for cat in CATEGORIES:
242
+ if cat in matched:
243
+ return cat
244
 
245
+ return "Education & Science"
246
+
247
+
248
+ def classify_topics(topics: List[str]) -> List[str]:
249
+ """Backward-compatible wrapper β€” returns a single-element list."""
250
+ return [classify_topic(topics)]
251
 
252
 
253
  def get_primary_category(topics: List[str]) -> str:
254
  """
255
  Return the single best-matching category for the given topics.
256
 
257
+ Alias for classify_topic().
258
  """
259
+ return classify_topic(topics)