Sonu313131 commited on
Commit
a7edfc4
·
verified ·
1 Parent(s): 88d4c4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -20,6 +20,10 @@ 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."
@@ -35,7 +39,8 @@ class YouTubeDialogueTool(Tool):
35
  },
36
  "speaker": {
37
  "type": "string",
38
- "description": "Name of the character who responds (e.g., 'Teal'c')"
 
39
  }
40
  }
41
 
@@ -43,26 +48,17 @@ class YouTubeDialogueTool(Tool):
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
@@ -74,6 +70,7 @@ class YouTubeDialogueTool(Tool):
74
 
75
 
76
 
 
77
  ##Tool 2
78
  import wikipedia
79
  from smolagents import Tool
 
20
  from youtube_transcript_api import YouTubeTranscriptApi
21
  from urllib.parse import urlparse, parse_qs
22
 
23
+ from smolagents import Tool
24
+ from youtube_transcript_api import YouTubeTranscriptApi
25
+ from urllib.parse import urlparse, parse_qs
26
+
27
  class YouTubeDialogueTool(Tool):
28
  name = "youtube_dialogue_qa"
29
  description = "Extracts the transcript and finds what Teal'c says in response to a specific question."
 
39
  },
40
  "speaker": {
41
  "type": "string",
42
+ "description": "Name of the character who responds (e.g., 'Teal'c')",
43
+ "nullable": True
44
  }
45
  }
46
 
 
48
 
49
  def forward(self, url: str, question: str, speaker: str = "Teal'c") -> str:
50
  try:
 
51
  parsed = urlparse(url)
52
  video_id = parse_qs(parsed.query).get("v", [None])[0]
53
  if not video_id:
54
  return "ERROR: Invalid YouTube URL or missing video ID."
55
 
 
56
  transcript = YouTubeTranscriptApi.get_transcript(video_id)
57
 
58
+ lines = [entry["text"].replace("\n", " ").strip() for entry in transcript if entry["text"].strip()]
 
 
 
 
 
59
 
 
60
  for idx, line in enumerate(lines):
61
  if question.lower().rstrip("?") in line.lower():
 
62
  if idx + 1 < len(lines):
63
  return lines[idx + 1].strip()
64
  break
 
70
 
71
 
72
 
73
+
74
  ##Tool 2
75
  import wikipedia
76
  from smolagents import Tool