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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -79
app.py CHANGED
@@ -3,95 +3,34 @@ import datetime
3
  import requests
4
  import pytz
5
  import yaml
6
- import re
7
  from tools.final_answer import FinalAnswerTool
8
 
9
  from Gradio_UI import GradioUI
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:
@@ -108,15 +47,20 @@ def get_current_time_in_timezone(timezone: str) -> str:
108
  except Exception as e:
109
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
110
 
 
111
  final_answer = FinalAnswerTool()
112
 
 
 
 
113
  model = HfApiModel(
114
  max_tokens=2096,
115
  temperature=0.5,
116
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
117
  custom_role_conversions=None,
118
  )
119
 
 
120
  # Import tool from Hub
121
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
122
 
@@ -125,7 +69,7 @@ with open("prompts.yaml", 'r') as stream:
125
 
126
  agent = CodeAgent(
127
  model=model,
128
- tools=[get_youtube_channel_info, get_current_time_in_timezone, final_answer], # Added your custom tools
129
  max_steps=6,
130
  verbosity_level=1,
131
  grammar=None,
@@ -135,4 +79,5 @@ agent = CodeAgent(
135
  prompt_templates=prompt_templates
136
  )
137
 
 
138
  GradioUI(agent).launch()
 
3
  import requests
4
  import pytz
5
  import yaml
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
  @tool
11
  def get_youtube_channel_info(channel_name: str) -> str:
12
+ """A tool that provides basic information about a YouTube channel using DuckDuckGo search.
13
  Args:
14
+ channel_name: The name of the YouTube channel to get information about
15
  """
16
+ search_tool = DuckDuckGoSearchTool(max_results=5) # Create DuckDuckGo search tool instance [[0]](#__0)
17
 
18
+ # Search for the YouTube channel information
19
+ query = f"{channel_name} youtube channel information subscribers videos"
20
+ search_results = search_tool(query) # Execute the search [[1]](#__1)
21
 
22
+ # Format the results in a readable way
23
+ formatted_info = f"Information about YouTube channel '{channel_name}':\n\n"
 
24
 
25
+ if search_results and len(search_results) > 0:
26
+ for i, result in enumerate(search_results[:3]): # Limit to top 3 results
27
+ formatted_info += f"Source {i+1}: {result.get('title', 'No title')}\n"
28
+ formatted_info += f"Description: {result.get('snippet', 'No description')}\n"
29
+ formatted_info += f"URL: {result.get('link', 'No link')}\n\n"
30
+ else:
31
+ formatted_info += "No information found for this channel."
32
 
33
+ return formatted_info # Return the formatted information [[2]](#__2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  @tool
36
  def get_current_time_in_timezone(timezone: str) -> str:
 
47
  except Exception as e:
48
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
49
 
50
+
51
  final_answer = FinalAnswerTool()
52
 
53
+ # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
54
+ # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
55
+
56
  model = HfApiModel(
57
  max_tokens=2096,
58
  temperature=0.5,
59
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
60
  custom_role_conversions=None,
61
  )
62
 
63
+
64
  # Import tool from Hub
65
  image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
66
 
 
69
 
70
  agent = CodeAgent(
71
  model=model,
72
+ tools=[final_answer, get_youtube_channel_info, get_current_time_in_timezone, image_generation_tool], # Added our tools here [[3]](#__3)
73
  max_steps=6,
74
  verbosity_level=1,
75
  grammar=None,
 
79
  prompt_templates=prompt_templates
80
  )
81
 
82
+
83
  GradioUI(agent).launch()