bako96 commited on
Commit
f3532e0
·
verified ·
1 Parent(s): f193cd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -197
app.py CHANGED
@@ -1,223 +1,100 @@
1
- from smolagents import Agent, DuckDuckGoSearchTool, tool
 
 
 
 
2
  import re
3
- from typing import Dict, Any, List
4
- import json
 
5
 
6
  @tool
7
- def search_youtuber(youtuber_name: str) -> Dict[str, Any]:
8
- """
9
- Searches for information about a YouTuber using DuckDuckGo.
10
-
11
  Args:
12
- youtuber_name: Name of the YouTuber to search for
13
-
14
- Returns:
15
- Dictionary containing search results about the YouTuber
16
  """
17
  search_tool = DuckDuckGoSearchTool()
18
 
19
- # First search for the YouTuber's channel
20
- channel_results = search_tool(f"{youtuber_name} youtube channel")
21
 
22
- # Then search for more specific information
23
- stats_results = search_tool(f"{youtuber_name} youtube stats subscribers")
24
 
25
- # Search for recent content/news
26
- content_results = search_tool(f"{youtuber_name} recent videos content")
27
 
28
- # Search for background information
29
- background_results = search_tool(f"{youtuber_name} youtube background history")
30
-
31
- return {
32
- "channel_results": channel_results,
33
- "stats_results": stats_results,
34
- "content_results": content_results,
35
- "background_results": background_results
36
- }
37
-
38
- @tool
39
- def extract_youtube_info(search_results: Dict[str, Any]) -> Dict[str, Any]:
40
- """
41
- Extracts relevant YouTuber information from search results.
42
 
43
- Args:
44
- search_results: Dictionary containing search results
45
-
46
- Returns:
47
- Dictionary containing extracted YouTuber information
48
- """
49
- # Extract channel URL
50
- channel_url = None
51
- for result in search_results.get("channel_results", []):
52
- if "youtube.com/channel/" in result.get("url", "") or "youtube.com/c/" in result.get("url", "") or "youtube.com/@" in result.get("url", ""):
53
- channel_url = result.get("url")
54
  break
55
 
56
- # Extract subscriber count and other stats
57
- subscriber_count = None
58
- view_count = None
59
- for result in search_results.get("stats_results", []):
60
- desc = result.get("desc", "")
61
- # Look for subscriber patterns like "10M subscribers" or "10 million subscribers"
62
- sub_match = re.search(r"(\d+(?:\.\d+)?[KMB]?)\s+subscribers", desc, re.IGNORECASE)
63
- if sub_match:
64
- subscriber_count = sub_match.group(1)
65
-
66
- # Look for view patterns
67
- view_match = re.search(r"(\d+(?:\.\d+)?[KMB]?)\s+views", desc, re.IGNORECASE)
68
- if view_match:
69
- view_count = view_match.group(1)
70
-
71
- # Extract recent videos/content
72
- recent_videos = []
73
- for result in search_results.get("content_results", []):
74
- if "video" in result.get("desc", "").lower() or "upload" in result.get("desc", "").lower():
75
- recent_videos.append({
76
- "title": result.get("title"),
77
- "url": result.get("url"),
78
- "description": result.get("desc")
79
- })
80
- if len(recent_videos) >= 3:
81
- break
82
 
83
- # Extract background information
84
- background_info = []
85
- for result in search_results.get("background_results", []):
86
- if "started" in result.get("desc", "").lower() or "began" in result.get("desc", "").lower() or "history" in result.get("desc", "").lower():
87
- background_info.append({
88
- "title": result.get("title"),
89
- "url": result.get("url"),
90
- "description": result.get("desc")
91
- })
92
- if len(background_info) >= 2:
93
- break
94
 
95
- return {
96
- "channel_url": channel_url,
97
- "subscriber_count": subscriber_count,
98
- "view_count": view_count,
99
- "recent_videos": recent_videos,
100
- "background_info": background_info,
101
- "raw_results": {
102
- "channel": search_results.get("channel_results", [])[:3],
103
- "stats": search_results.get("stats_results", [])[:3],
104
- "content": search_results.get("content_results", [])[:3],
105
- "background": search_results.get("background_results", [])[:3]
106
- }
107
- }
108
 
109
  @tool
110
- def format_youtuber_profile(youtuber_name: str, info: Dict[str, Any]) -> str:
111
- """
112
- Formats YouTuber information with proper citations.
113
-
114
  Args:
115
- youtuber_name: Name of the YouTuber
116
- info: Dictionary containing YouTuber information
117
-
118
- Returns:
119
- Formatted information with citations
120
  """
121
- raw_results = info.get("raw_results", {})
122
-
123
- result = f"# {youtuber_name} - YouTube Creator Profile\n\n"
124
-
125
- # Channel information
126
- result += "## Channel Information\n"
127
- if info.get("channel_url"):
128
- result += f"- **Channel URL:** [{info.get('channel_url')}]({info.get('channel_url')}) \n"
129
-
130
- if info.get("subscriber_count"):
131
- result += f"- **Subscribers:** {info.get('subscriber_count')} \n"
132
-
133
- if info.get("view_count"):
134
- result += f"- **Views:** {info.get('view_count')} \n"
135
-
136
- # Add channel source citation
137
- if raw_results.get("channel") and len(raw_results.get("channel")) > 0:
138
- source = raw_results.get("channel")[0]
139
- result += f"\n: [{source.get('title')}]({source.get('url')})\n"
140
-
141
- # Add stats source citation
142
- if raw_results.get("stats") and len(raw_results.get("stats")) > 0:
143
- source = raw_results.get("stats")[0]
144
- result += f": [{source.get('title')}]({source.get('url')})\n"
145
-
146
- # Recent content
147
- if info.get("recent_videos") and len(info.get("recent_videos")) > 0:
148
- result += "\n## Recent Content\n"
149
- for i, video in enumerate(info.get("recent_videos")):
150
- result += f"- **{video.get('title')}** $CITE_{3+i}\n"
151
- result += f" {video.get('description')}\n"
152
- result += f" [Link]({video.get('url')})\n\n"
153
-
154
- # Content citations
155
- for i, source in enumerate(raw_results.get("content", [])[:len(info.get("recent_videos", []))]):
156
- result += f"$CITE_{3+i}: [{source.get('title')}]({source.get('url')})\n"
157
-
158
- # Background information
159
- if info.get("background_info") and len(info.get("background_info")) > 0:
160
- result += "\n## Background & History\n"
161
- cite_start = 3 + len(info.get("recent_videos", []))
162
- for i, bg in enumerate(info.get("background_info")):
163
- result += f"- **{bg.get('title')}** $CITE_{cite_start+i}\n"
164
- result += f" {bg.get('description')}\n\n"
165
-
166
- # Background citations
167
- cite_start = 3 + len(info.get("recent_videos", []))
168
- for i, source in enumerate(raw_results.get("background", [])[:len(info.get("background_info", []))]):
169
- result += f"$CITE_{cite_start+i}: [{source.get('title')}]({source.get('url')})\n"
170
-
171
- return result
172
-
173
- # Create the specialized YouTuber information agent
174
- search_tool = DuckDuckGoSearchTool()
175
 
176
- # Define the agent with the proper system prompt
177
- youtuber_info_agent = Agent(
178
- system_prompt="""You are a specialized YouTuber information agent. Your task is to find and provide detailed information about YouTube creators when given a YouTuber's name.
179
 
180
- ALWAYS include multiple distinct sources in your response, at LEAST 3-4, by $CITE_id at the end of each information part, like , .
181
-
182
- Follow these steps:
183
- 1. Search for the YouTuber using the search_youtuber tool
184
- 2. Extract relevant information using the extract_youtube_info tool
185
- 3. Format the information with proper citations using the format_youtuber_profile tool
186
- 4. Present the information to the user in a well-organized format
187
-
188
- Always verify the information before presenting it and indicate if certain information couldn't be retrieved.
189
- """,
190
- tools=[
191
- search_tool,
192
- search_youtuber,
193
- extract_youtube_info,
194
- format_youtuber_profile
195
- ],
196
- max_steps=8,
197
- verbose=True
198
  )
199
 
200
- # Set up the Gradio interface
201
- import gradio as gr
202
 
203
- def process_request(youtuber_name):
204
- # Use the agent to process the request
205
- response = youtuber_info_agent(f"Please provide information about the YouTuber: {youtuber_name}")
206
- return response
207
-
208
- # Create the Gradio interface
209
- interface = gr.Interface(
210
- fn=process_request,
211
- inputs=gr.Textbox(label="Enter YouTuber Name"),
212
- outputs=gr.Markdown(label="YouTuber Information"),
213
- title="YouTuber Information Agent",
214
- description="Enter a YouTuber's name to get information about them with proper citations.",
215
- examples=[
216
- ["MrBeast"],
217
- ["PewDiePie"],
218
- ["Marques Brownlee"]
219
- ]
220
  )
221
 
222
- # Launch with sharing enabled
223
- interface.launch(share=True)
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
+ 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 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
+ # First search for the channel's basic information
20
+ basic_search_results = search_tool(f"{channel_name} youtube channel info")
21
 
22
+ # Then specifically search for subscriber count
23
+ sub_search_results = search_tool(f"{channel_name} youtube channel subscribers count")
24
 
25
+ # Extract relevant information from search results
26
+ channel_info = f"Information about YouTube channel '{channel_name}':\n\n"
27
 
28
+ # Try to extract subscriber count using regex patterns
29
+ subscriber_count = "Could not determine subscriber count"
30
+ sub_patterns = [
31
+ r'(\d+(?:\.\d+)?)\s*(?:M|million)\s+subscribers',
32
+ r'(\d+(?:\.\d+)?)\s*(?:K|thousand)\s+subscribers',
33
+ r'(\d+)\s+subscribers',
34
+ r'subscribers:\s*(\d+(?:\.\d+)?(?:\s*[KM])?)',
35
+ ]
 
 
 
 
 
 
36
 
37
+ for result in sub_search_results + basic_search_results:
38
+ for pattern in sub_patterns:
39
+ matches = re.search(pattern, result, re.IGNORECASE)
40
+ if matches:
41
+ subscriber_count = matches.group(1)
42
+ break
43
+ if subscriber_count != "Could not determine subscriber count":
 
 
 
 
44
  break
45
 
46
+ # Compile all information
47
+ channel_info += f"Channel Name: {channel_name}\n"
48
+ channel_info += f"Subscriber Count: {subscriber_count}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
+ # Extract other relevant information
51
+ for result in basic_search_results:
52
+ if "about" in result.lower() and channel_name.lower() in result.lower():
53
+ channel_info += f"Description: {result}\n"
54
+ break
 
 
 
 
 
 
55
 
56
+ return channel_info
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  @tool
59
+ def get_current_time_in_timezone(timezone: str) -> str:
60
+ """A tool that fetches the current local time in a specified timezone.
 
 
61
  Args:
62
+ timezone: A string representing a valid timezone (e.g., 'America/New_York').
 
 
 
 
63
  """
64
+ try:
65
+ # Create timezone object
66
+ tz = pytz.timezone(timezone)
67
+ # Get current time in that timezone
68
+ local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
69
+ return f"The current local time in {timezone} is: {local_time}"
70
+ except Exception as e:
71
+ return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ final_answer = FinalAnswerTool()
 
 
74
 
75
+ model = HfApiModel(
76
+ max_tokens=2096,
77
+ temperature=0.5,
78
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
79
+ custom_role_conversions=None,
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  )
81
 
82
+ # Import tool from Hub
83
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
84
 
85
+ with open("prompts.yaml", 'r') as stream:
86
+ prompt_templates = yaml.safe_load(stream)
87
+
88
+ agent = CodeAgent(
89
+ model=model,
90
+ tools=[get_youtube_channel_info, get_current_time_in_timezone, DuckDuckGoSearchTool(), final_answer], # Added your custom tools
91
+ max_steps=6,
92
+ verbosity_level=1,
93
+ grammar=None,
94
+ planning_interval=None,
95
+ name=None,
96
+ description=None,
97
+ prompt_templates=prompt_templates
 
 
 
 
98
  )
99
 
100
+ GradioUI(agent).launch()