Update app.py
Browse files
app.py
CHANGED
@@ -16,41 +16,59 @@ openai_key = os.environ.get("OPENAI_API_KEY")
|
|
16 |
search_tool = DuckDuckGoSearchTool()
|
17 |
|
18 |
##Tool 1
|
19 |
-
import re
|
20 |
-
from youtube_transcript_api import YouTubeTranscriptApi
|
21 |
-
from smolagents import Tool
|
22 |
-
|
23 |
from smolagents import Tool
|
24 |
from youtube_transcript_api import YouTubeTranscriptApi
|
25 |
from urllib.parse import urlparse, parse_qs
|
26 |
|
27 |
-
class
|
28 |
-
name = "
|
29 |
-
description = "
|
30 |
|
31 |
inputs = {
|
32 |
"url": {
|
33 |
"type": "string",
|
34 |
-
"description": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
}
|
36 |
}
|
37 |
|
38 |
output_type = "string"
|
39 |
|
40 |
-
def forward(self, url: str) -> str:
|
41 |
try:
|
42 |
-
# Extract video ID
|
43 |
parsed = urlparse(url)
|
44 |
video_id = parse_qs(parsed.query).get("v", [None])[0]
|
45 |
-
|
46 |
if not video_id:
|
47 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
50 |
-
transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
|
51 |
-
transcript_text = " ".join(entry["text"] for entry in transcript_list)
|
52 |
|
53 |
-
return transcript_text[:5000] # Optional: truncate to 5000 chars
|
54 |
except Exception as e:
|
55 |
return f"Error retrieving transcript: {str(e)}"
|
56 |
|
@@ -115,7 +133,7 @@ class WikipediaQATool(Tool):
|
|
115 |
|
116 |
wiki_tool = WikipediaQATool()
|
117 |
#excel_tool = ExcelAnalysisTool()
|
118 |
-
yt_tool =
|
119 |
|
120 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
121 |
log_output = ""
|
@@ -127,7 +145,7 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
127 |
model=OpenAIServerModel(model_id="gpt-4o-mini",
|
128 |
api_key=os.environ["OPENAI_API_KEY"],
|
129 |
temperature=0.0),
|
130 |
-
max_steps=
|
131 |
verbosity_level=2
|
132 |
)
|
133 |
except Exception as e:
|
|
|
16 |
search_tool = DuckDuckGoSearchTool()
|
17 |
|
18 |
##Tool 1
|
|
|
|
|
|
|
|
|
19 |
from smolagents import Tool
|
20 |
from youtube_transcript_api import YouTubeTranscriptApi
|
21 |
from urllib.parse import urlparse, parse_qs
|
22 |
|
23 |
+
class YouTubeDialogueTool(Tool):
|
24 |
+
name = "youtube_dialogue_qa"
|
25 |
+
description = "Extracts the transcript and finds what Teal'c says in response to a specific question."
|
26 |
|
27 |
inputs = {
|
28 |
"url": {
|
29 |
"type": "string",
|
30 |
+
"description": "Full YouTube video URL"
|
31 |
+
},
|
32 |
+
"question": {
|
33 |
+
"type": "string",
|
34 |
+
"description": 'Exact question asked (e.g., "Isn\'t that hot?")'
|
35 |
+
},
|
36 |
+
"speaker": {
|
37 |
+
"type": "string",
|
38 |
+
"description": "Name of the character who responds (e.g., 'Teal'c')"
|
39 |
}
|
40 |
}
|
41 |
|
42 |
output_type = "string"
|
43 |
|
44 |
+
def forward(self, url: str, question: str, speaker: str = "Teal'c") -> str:
|
45 |
try:
|
46 |
+
# Extract video ID
|
47 |
parsed = urlparse(url)
|
48 |
video_id = parse_qs(parsed.query).get("v", [None])[0]
|
|
|
49 |
if not video_id:
|
50 |
+
return "ERROR: Invalid YouTube URL or missing video ID."
|
51 |
+
|
52 |
+
# Get transcript
|
53 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
54 |
+
|
55 |
+
# Build list of lines
|
56 |
+
lines = []
|
57 |
+
for entry in transcript:
|
58 |
+
text = entry["text"].replace("\n", " ").strip()
|
59 |
+
if text:
|
60 |
+
lines.append(text)
|
61 |
+
|
62 |
+
# Find the question and capture the next line(s)
|
63 |
+
for idx, line in enumerate(lines):
|
64 |
+
if question.lower().rstrip("?") in line.lower():
|
65 |
+
# Look ahead for a line that matches speaker response style
|
66 |
+
if idx + 1 < len(lines):
|
67 |
+
return lines[idx + 1].strip()
|
68 |
+
break
|
69 |
|
70 |
+
return "No response found in transcript."
|
|
|
|
|
71 |
|
|
|
72 |
except Exception as e:
|
73 |
return f"Error retrieving transcript: {str(e)}"
|
74 |
|
|
|
133 |
|
134 |
wiki_tool = WikipediaQATool()
|
135 |
#excel_tool = ExcelAnalysisTool()
|
136 |
+
yt_tool = YouTubeDialogueTool()
|
137 |
|
138 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
139 |
log_output = ""
|
|
|
145 |
model=OpenAIServerModel(model_id="gpt-4o-mini",
|
146 |
api_key=os.environ["OPENAI_API_KEY"],
|
147 |
temperature=0.0),
|
148 |
+
max_steps=7,
|
149 |
verbosity_level=2
|
150 |
)
|
151 |
except Exception as e:
|