raul-padua commited on
Commit
8e25013
·
verified ·
1 Parent(s): 170273e

Update app.py

Browse files

Update `app.py` to Use Tools from `tools/`

Files changed (1) hide show
  1. app.py +13 -81
app.py CHANGED
@@ -1,99 +1,31 @@
1
- from smolagents import CodeAgent, HfApiModel, tool
2
- import datetime
3
- import requests
4
- import pytz
5
  import yaml
6
- from transformers import pipeline
7
- from keybert import KeyBERT
8
- import wikipediaapi
9
- from tools.final_answer import FinalAnswerTool
10
  from Gradio_UI import GradioUI
11
 
12
- @tool # Text Summarizer
13
- def text_summarizer(text: str, max_length: int = 50) -> str:
14
- """Summarizes the input text.
15
-
16
- Args:
17
- text (str): The text to summarize.
18
- max_length (int): The maximum length of the summary.
19
-
20
- Returns:
21
- str: The summarized version of the input text or an error message.
22
- """
23
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
24
- try:
25
- summary = summarizer(text, max_length=max_length, min_length=20, do_sample=False)
26
- return summary[0]['summary_text']
27
- except Exception as e:
28
- return f"Error: {str(e)}"
29
-
30
- @tool # Keyword Extractor
31
- def keyword_extractor(text: str, num_keywords: int = 5) -> str:
32
- """Extracts keywords from the input text.
33
-
34
- Args:
35
- text (str): The text to extract keywords from.
36
- num_keywords (int): The number of keywords to extract.
37
-
38
- Returns:
39
- str: A string containing the extracted keywords or an error message.
40
- """
41
- kw_model = KeyBERT()
42
- try:
43
- keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), top_n=num_keywords)
44
- return ", ".join([kw[0] for kw in keywords])
45
- except Exception as e:
46
- return f"Error: {str(e)}"
47
-
48
- @tool # Wikipedia Lookup
49
- def wikipedia_lookup(query: str, sentences: int = 3) -> str:
50
- """Fetches a summary from Wikipedia.
51
-
52
- Args:
53
- query (str): The topic to search for.
54
- sentences (int): The number of sentences for the summary.
55
-
56
- Returns:
57
- str: A summary of the topic or an error message.
58
- """
59
- wiki = wikipediaapi.Wikipedia('en', headers={"User-Agent": "raul-padua/HF_Agents_Course"})
60
- page = wiki.page(query)
61
- return page.summary[:500] + '...' if page.exists() else "No page found."
62
-
63
- @tool # Live Weather Fetching
64
- def weather_fetcher(city: str) -> str:
65
- """Fetches current weather conditions for a city.
66
-
67
- Args:
68
- city (str): The city name to get weather data for.
69
-
70
- Returns:
71
- str: A string describing the weather or an error message.
72
- """
73
- try:
74
- api_key = "b648dc2263850640175b2c0b26c1a974"
75
- url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
76
- response = requests.get(url).json()
77
- return f"{response['weather'][0]['description']}, {response['main']['temp']}°C"
78
- except Exception as e:
79
- return f"Error: {str(e)}"
80
 
 
81
  final_answer = FinalAnswerTool()
 
 
 
 
82
  model = HfApiModel(
83
  max_tokens=2096,
84
  temperature=0.5,
85
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
86
  )
87
 
88
- with open("prompts.yaml", 'r') as stream:
89
- prompt_templates = yaml.safe_load(stream)
90
-
91
  agent = CodeAgent(
92
  model=model,
93
- tools=[final_answer, text_summarizer, keyword_extractor, wikipedia_lookup, weather_fetcher],
94
  max_steps=6,
95
  verbosity_level=1,
96
  prompt_templates=prompt_templates
97
  )
98
 
99
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, HfApiModel
 
 
 
2
  import yaml
3
+ from tools import FinalAnswerTool, VisitWebpageTool, DuckDuckGoSearchTool
 
 
 
4
  from Gradio_UI import GradioUI
5
 
6
+ # Load prompts
7
+ with open("prompts.yaml", 'r') as stream:
8
+ prompt_templates = yaml.safe_load(stream)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ # Initialize tools
11
  final_answer = FinalAnswerTool()
12
+ web_search = DuckDuckGoSearchTool(max_results=5)
13
+ visit_webpage = VisitWebpageTool()
14
+
15
+ # Configure model
16
  model = HfApiModel(
17
  max_tokens=2096,
18
  temperature=0.5,
19
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
20
  )
21
 
22
+ # Create agent
 
 
23
  agent = CodeAgent(
24
  model=model,
25
+ tools=[final_answer, web_search, visit_webpage],
26
  max_steps=6,
27
  verbosity_level=1,
28
  prompt_templates=prompt_templates
29
  )
30
 
31
+ # Launch Gradio UI