Spaces:
Sleeping
Sleeping
Upload chat_app.py
Browse files- chat_app.py +57 -0
chat_app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from retrieval import *
|
3 |
+
import requests
|
4 |
+
|
5 |
+
def ask_llama_about_chunks(question):
|
6 |
+
top_chunks = retrieval(question)
|
7 |
+
|
8 |
+
merged_chunks = merge_chunks(top_chunks)
|
9 |
+
|
10 |
+
context_text = "\n\n".join(
|
11 |
+
f"[{i + 1}] [{chunk['video_id']}] [{chunk['start_time']}] - [{chunk['end_time']}]: {chunk['subtitle']}" for i, chunk in enumerate(merged_chunks)
|
12 |
+
)
|
13 |
+
|
14 |
+
prompt = f"""You are an assistant helping users understand video content.
|
15 |
+
Here are relevant pieces of text from a video, each labeled [1] through [10].
|
16 |
+
|
17 |
+
QUESTION: "{question}"
|
18 |
+
|
19 |
+
Based only on the context, which segment of text best answers the users question or comment. Prioritize longer responses. Return ONLY the number of the most relevant chunk (e.g., '1').
|
20 |
+
Do NOT return any explanation.
|
21 |
+
|
22 |
+
CONTEXT:
|
23 |
+
{context_text}
|
24 |
+
|
25 |
+
ANSWER:"""
|
26 |
+
|
27 |
+
response = requests.post(
|
28 |
+
"http://host.docker.internal:11434/api/generate",
|
29 |
+
json={"model": "llama3", "prompt": prompt, "stream": False}
|
30 |
+
)
|
31 |
+
answer = response.json()["response"].strip()
|
32 |
+
|
33 |
+
try:
|
34 |
+
best_chunk_index = int(answer) - 1
|
35 |
+
best_chunk = merged_chunks[best_chunk_index]
|
36 |
+
except:
|
37 |
+
best_chunk = merged_chunks[0]
|
38 |
+
|
39 |
+
video_clip_path = get_video_segment(best_chunk['video_id'], best_chunk["start_time"], best_chunk["end_time"])
|
40 |
+
|
41 |
+
return best_chunk['subtitle'], gr.Video(video_clip_path)
|
42 |
+
|
43 |
+
iface = gr.Interface(
|
44 |
+
fn=ask_llama_about_chunks,
|
45 |
+
inputs=[
|
46 |
+
gr.Textbox(label="Add your question here:")
|
47 |
+
],
|
48 |
+
outputs=[
|
49 |
+
gr.Textbox(label="Best Matching Transcript"),
|
50 |
+
gr.Video(label="Relevant Clip")
|
51 |
+
],
|
52 |
+
title = "Chat With Your Video Library",
|
53 |
+
description = "Ask questions about machine learning and the most relevant video segment and captions will be returned.",
|
54 |
+
)
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
iface.launch()
|