Upload folder using huggingface_hub
Browse files- app.py +2 -2
- modules/answerer.py +7 -14
app.py
CHANGED
|
@@ -306,7 +306,7 @@ def init():
|
|
| 306 |
channels = "https://www.youtube.com/@onedayonepasuram6126,https://www.youtube.com/@srisookthi,https://www.youtube.com/@learn-aksharam"
|
| 307 |
for resp in index_channels(channels):
|
| 308 |
print(resp)
|
| 309 |
-
|
| 310 |
if __name__ == "__main__":
|
| 311 |
-
init()
|
| 312 |
demo.launch()
|
|
|
|
| 306 |
channels = "https://www.youtube.com/@onedayonepasuram6126,https://www.youtube.com/@srisookthi,https://www.youtube.com/@learn-aksharam"
|
| 307 |
for resp in index_channels(channels):
|
| 308 |
print(resp)
|
| 309 |
+
|
| 310 |
if __name__ == "__main__":
|
| 311 |
+
# init()
|
| 312 |
demo.launch()
|
modules/answerer.py
CHANGED
|
@@ -37,9 +37,7 @@ def answer_query(query: str, collection, top_k: int = 5) -> LLMAnswer:
|
|
| 37 |
|
| 38 |
# Build context lines for the LLM
|
| 39 |
context_lines = []
|
| 40 |
-
top_videos_list = []
|
| 41 |
for r in results:
|
| 42 |
-
# Ensure each result is a dict
|
| 43 |
if not isinstance(r, dict):
|
| 44 |
continue
|
| 45 |
vid_id = r.get("video_id", "")
|
|
@@ -50,12 +48,6 @@ def answer_query(query: str, collection, top_k: int = 5) -> LLMAnswer:
|
|
| 50 |
f"- {title} ({channel}) (https://youtube.com/watch?v={vid_id})\n description: {description}"
|
| 51 |
)
|
| 52 |
|
| 53 |
-
top_videos_list.append(
|
| 54 |
-
VideoItem(
|
| 55 |
-
video_id=vid_id, title=title, channel=channel, description=description
|
| 56 |
-
)
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
context_text = "\n".join(context_lines)
|
| 60 |
|
| 61 |
# Call LLM with structured output
|
|
@@ -67,26 +59,27 @@ def answer_query(query: str, collection, top_k: int = 5) -> LLMAnswer:
|
|
| 67 |
"role": "system",
|
| 68 |
"content": (
|
| 69 |
"You are a helpful assistant that answers questions using YouTube video metadata. "
|
| 70 |
-
"Return your response strictly as the LLMAnswer class, including 'answer_text' and a list of **relevant** 'top_videos'
|
| 71 |
-
"
|
| 72 |
-
-
|
| 73 |
-
-
|
| 74 |
),
|
| 75 |
},
|
| 76 |
{
|
| 77 |
"role": "user",
|
| 78 |
-
"content": f"Question: {query}\n\
|
| 79 |
},
|
| 80 |
],
|
| 81 |
response_format=LLMAnswer,
|
| 82 |
)
|
| 83 |
|
| 84 |
-
llm_answer = response.choices[0].message.parsed
|
| 85 |
answer_text = llm_answer.answer_text
|
| 86 |
video_html = build_video_html(llm_answer.top_videos)
|
| 87 |
return answer_text, video_html
|
| 88 |
|
| 89 |
|
|
|
|
| 90 |
def build_video_html(videos: list[VideoItem]) -> str:
|
| 91 |
"""Build a clean HTML table from top_videos."""
|
| 92 |
if not videos:
|
|
|
|
| 37 |
|
| 38 |
# Build context lines for the LLM
|
| 39 |
context_lines = []
|
|
|
|
| 40 |
for r in results:
|
|
|
|
| 41 |
if not isinstance(r, dict):
|
| 42 |
continue
|
| 43 |
vid_id = r.get("video_id", "")
|
|
|
|
| 48 |
f"- {title} ({channel}) (https://youtube.com/watch?v={vid_id})\n description: {description}"
|
| 49 |
)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
context_text = "\n".join(context_lines)
|
| 52 |
|
| 53 |
# Call LLM with structured output
|
|
|
|
| 59 |
"role": "system",
|
| 60 |
"content": (
|
| 61 |
"You are a helpful assistant that answers questions using YouTube video metadata. "
|
| 62 |
+
"Return your response strictly as the LLMAnswer class, including 'answer_text' and a list of **only the most relevant** 'top_videos'.\n"
|
| 63 |
+
"- `answer_text` MUST be very short and concise in natural language (max 1–2 sentences).\n"
|
| 64 |
+
"- Use `top_videos` to include only the 2–3 most relevant items from context.\n"
|
| 65 |
+
"- Do not include all items unless all are clearly relevant.\n"
|
| 66 |
),
|
| 67 |
},
|
| 68 |
{
|
| 69 |
"role": "user",
|
| 70 |
+
"content": f"Question: {query}\n\nCandidate videos:\n{context_text}\n\nPick only the relevant ones.",
|
| 71 |
},
|
| 72 |
],
|
| 73 |
response_format=LLMAnswer,
|
| 74 |
)
|
| 75 |
|
| 76 |
+
llm_answer = response.choices[0].message.parsed
|
| 77 |
answer_text = llm_answer.answer_text
|
| 78 |
video_html = build_video_html(llm_answer.top_videos)
|
| 79 |
return answer_text, video_html
|
| 80 |
|
| 81 |
|
| 82 |
+
|
| 83 |
def build_video_html(videos: list[VideoItem]) -> str:
|
| 84 |
"""Build a clean HTML table from top_videos."""
|
| 85 |
if not videos:
|