bako96 commited on
Commit
294d8a6
·
verified ·
1 Parent(s): b98e877

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -40
app.py CHANGED
@@ -10,72 +10,89 @@ from Gradio_UI import GradioUI
10
 
11
  @tool
12
  def get_youtube_channel_info(channel_name: str) -> str:
13
- """A tool that fetches information about a YouTube channel including subscriber count.
14
  Args:
15
  channel_name: The name of the YouTube channel to search for.
16
  """
17
  search_tool = DuckDuckGoSearchTool()
18
 
19
- # Search for channel statistics specifically
20
- stats_search_results = search_tool(f"{channel_name} youtube channel statistics subscribers")
21
- print(f"stats_search_results: {stats_search_results}")
22
 
23
- # Search for channel information
24
- info_search_results = search_tool(f"{channel_name} youtube channel about")
25
- print(f"info_search_results: {info_search_results}")
26
 
27
- # Search specifically for subscriber count
28
- sub_search_results = search_tool(f"how many subscribers does {channel_name} have on youtube")
29
- print(f"sub_search_results: {sub_search_results}")
30
 
31
- # Compile information
32
- channel_info = f"Information about YouTube channel '{channel_name}':\n\n"
33
- channel_info += f"Channel Name: {channel_name}\n"
34
-
35
- # Extract subscriber count from search results
36
- subscriber_info = "Could not determine subscriber count"
37
-
38
- # Check all search results for subscriber information
39
- all_results = stats_search_results + sub_search_results + info_search_results
40
-
41
- for result in all_results:
42
  result_lower = result.lower()
43
- # Look for common subscriber count patterns without regex
44
- if "subscribers" in result_lower:
45
- # Extract the sentence containing subscriber information
46
  sentences = result.split('.')
47
  for sentence in sentences:
48
  if "subscribers" in sentence.lower():
49
- subscriber_info = sentence.strip()
50
  break
51
- if subscriber_info != "Could not determine subscriber count":
52
  break
53
 
54
- channel_info += f"Subscriber Count: {subscriber_info}\n"
55
 
56
- # Add channel description
 
57
  channel_description = "No description available"
58
- for result in info_search_results:
59
- if "about" in result.lower() and channel_name.lower() in result.lower():
 
60
  channel_description = result
61
  break
62
 
63
- channel_info += f"Description: {channel_description}\n"
64
 
65
- # Add content type/category if available
66
- content_info = ""
67
- for result in info_search_results:
68
- if "content" in result.lower() or "videos" in result.lower():
69
- content_info = result
70
- break
71
-
72
- if content_info:
73
- channel_info += f"Content: {content_info}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  return channel_info
76
 
77
 
78
 
 
79
  @tool
80
  def get_current_time_in_timezone(timezone: str) -> str:
81
  """A tool that fetches the current local time in a specified timezone.
 
10
 
11
  @tool
12
  def get_youtube_channel_info(channel_name: str) -> str:
13
+ """A tool that fetches basic information about a YouTube channel including subscriber count and recent videos.
14
  Args:
15
  channel_name: The name of the YouTube channel to search for.
16
  """
17
  search_tool = DuckDuckGoSearchTool()
18
 
19
+ # Search for basic channel info and subscriber count
20
+ channel_search = search_tool(f"{channel_name} youtube channel")
21
+ print(f"Channel search results: {channel_search}")
22
 
23
+ # Direct search for subscriber count
24
+ subscriber_search = search_tool(f"{channel_name} youtube subscribers count")
25
+ print(f"Subscriber search results: {subscriber_search}")
26
 
27
+ # Compile basic information
28
+ channel_info = f"YouTube Channel: {channel_name}\n\n"
 
29
 
30
+ # Extract subscriber count
31
+ subscriber_count = "Unknown"
32
+ for result in subscriber_search + channel_search:
 
 
 
 
 
 
 
 
33
  result_lower = result.lower()
34
+ if "subscribers" in result_lower and any(num in result_lower for num in ["million", "m subscribers", "k subscribers", " subscribers"]):
35
+ # Find the sentence with subscriber information
 
36
  sentences = result.split('.')
37
  for sentence in sentences:
38
  if "subscribers" in sentence.lower():
39
+ subscriber_count = sentence.strip()
40
  break
41
+ if subscriber_count != "Unknown":
42
  break
43
 
44
+ channel_info += f"Subscribers: {subscriber_count}\n\n"
45
 
46
+ # Try to get channel description/summary
47
+ description_search = search_tool(f"{channel_name} youtube channel about")
48
  channel_description = "No description available"
49
+
50
+ for result in description_search + channel_search:
51
+ if ("about" in result.lower() or "description" in result.lower()) and channel_name.lower() in result.lower():
52
  channel_description = result
53
  break
54
 
55
+ channel_info += f"Channel Summary: {channel_description}\n\n"
56
 
57
+ # If no good description was found, fetch video titles to help analyze content
58
+ if channel_description == "No description available" or len(channel_description) < 50:
59
+ video_search = search_tool(f"{channel_name} youtube recent videos")
60
+ video_titles = []
61
+
62
+ # Try to extract video titles from search results
63
+ for result in video_search:
64
+ # Look for patterns that might indicate video titles
65
+ if ":" in result and channel_name.lower() in result.lower():
66
+ # Potential video title after a colon
67
+ title_part = result.split(":", 1)[1].strip()
68
+ if 10 < len(title_part) < 100: # Reasonable title length
69
+ video_titles.append(title_part)
70
+
71
+ # Alternative approach: look for quotes which might contain titles
72
+ quoted_parts = result.split('"')
73
+ if len(quoted_parts) > 1:
74
+ for i in range(1, len(quoted_parts), 2):
75
+ potential_title = quoted_parts[i].strip()
76
+ if 10 < len(potential_title) < 100: # Reasonable title length
77
+ video_titles.append(potential_title)
78
+
79
+ # Deduplicate and limit to 10 titles
80
+ video_titles = list(dict.fromkeys(video_titles))[:10]
81
+
82
+ if video_titles:
83
+ channel_info += "Recent Videos:\n"
84
+ for i, title in enumerate(video_titles, 1):
85
+ channel_info += f"{i}. {title}\n"
86
+ else:
87
+ # If we couldn't extract titles, do another search specifically for videos
88
+ direct_videos = search_tool(f"{channel_name} youtube videos")
89
+ channel_info += "Content Topics: " + ", ".join(direct_videos[:3])
90
 
91
  return channel_info
92
 
93
 
94
 
95
+
96
  @tool
97
  def get_current_time_in_timezone(timezone: str) -> str:
98
  """A tool that fetches the current local time in a specified timezone.