Jan Albrecht commited on
Commit
8247d09
·
1 Parent(s): 25a2f38

Enhance Gradio UI and add news fetching tool with DuckDuckGo integration

Browse files
Files changed (4) hide show
  1. Gradio_UI.py +1 -1
  2. app.py +46 -7
  3. requirements.txt +1 -0
  4. tmp.py +41 -0
Gradio_UI.py CHANGED
@@ -283,7 +283,7 @@ class GradioUI:
283
  [upload_file, file_uploads_log],
284
  [upload_status, file_uploads_log],
285
  )
286
- text_input = gr.Textbox(lines=1, label="Chat Message")
287
  text_input.submit(
288
  self.log_user_message,
289
  [text_input, file_uploads_log],
 
283
  [upload_file, file_uploads_log],
284
  [upload_status, file_uploads_log],
285
  )
286
+ text_input = gr.Textbox(lines=1, label="Chat Message :)")
287
  text_input.submit(
288
  self.log_user_message,
289
  [text_input, file_uploads_log],
app.py CHANGED
@@ -1,22 +1,61 @@
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
 
2
  import datetime
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
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
  @tool
12
- def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
  #Keep this format for the description / args / args description but feel free to modify the tool
14
  """A tool that does nothing yet
15
  Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
  """
19
- return "What magic will you build ?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  @tool
22
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -36,7 +75,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
36
 
37
  final_answer = FinalAnswerTool()
38
  model = HfApiModel(
39
- max_tokens=2096,
40
  temperature=0.5,
41
  model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',# it is possible that this model may be overloaded
42
  custom_role_conversions=None,
@@ -51,7 +90,7 @@ with open("prompts.yaml", 'r') as stream:
51
 
52
  agent = CodeAgent(
53
  model=model,
54
- tools=[final_answer], ## add your tools here (don't remove final answer)
55
  max_steps=6,
56
  verbosity_level=1,
57
  grammar=None,
 
1
  from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
+ from duckduckgo_search import DDGS
3
  import datetime
4
  import requests
5
  import pytz
6
  import yaml
7
  from tools.final_answer import FinalAnswerTool
8
+ import bs4
9
  from Gradio_UI import GradioUI
10
+ from playwright.sync_api import sync_playwright
11
+
12
+ def fetch_url(url: str):
13
+ with sync_playwright() as p:
14
+ browser = p.webkit.launch()
15
+ page = browser.new_page()
16
+ page.goto(url)
17
+ print(f"fetch: {page.title()} in {url}")
18
+ content = bs4.BeautifulSoup(page.content(), features="lxml")
19
+
20
+ result = ""
21
+ for chunk in content.find_all('p'):
22
+ result += chunk.text + "\n"
23
+ browser.close()
24
+ return result
25
 
26
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
27
  @tool
28
+ def get_news_from_duckduckgo(keywords: str)-> str: #it's import to specify the return type
29
  #Keep this format for the description / args / args description but feel free to modify the tool
30
  """A tool that does nothing yet
31
  Args:
32
+ keywords: A string of keywords to search for in the news.
 
33
  """
34
+ ddgs = DDGS()
35
+ results = ddgs.news(keywords=keywords, timelimit="w", max_results=5)
36
+
37
+ if len(results) == 0:
38
+ raise Exception("No results found! Try a less restrictive/shorter query.")
39
+
40
+ model = HfApiModel(
41
+ max_tokens=1048,
42
+ temperature=0.5,
43
+ model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',# it is possible that this model may be overloaded
44
+ custom_role_conversions=None,
45
+ )
46
+
47
+ summaries = []
48
+ for result in results:
49
+ content = fetch_url(result['url'])
50
+ messages = [
51
+ {"role": "system", "content": [{"type": "text", "text": "You are a news bot. Please summarize the news articles."}]},
52
+ {"role": "user", "content": [{"type": "text", "text": result['title'] + "\n" + content}]}
53
+ ]
54
+ summary = model(messages).content
55
+ print(summary)
56
+ summaries.append(summary)
57
+
58
+ return "## Search Results\n\n" + "\n\n".join(summaries)
59
 
60
  @tool
61
  def get_current_time_in_timezone(timezone: str) -> str:
 
75
 
76
  final_answer = FinalAnswerTool()
77
  model = HfApiModel(
78
+ max_tokens=2096*4,
79
  temperature=0.5,
80
  model_id='deepseek-ai/DeepSeek-R1-Distill-Qwen-32B',# it is possible that this model may be overloaded
81
  custom_role_conversions=None,
 
90
 
91
  agent = CodeAgent(
92
  model=model,
93
+ tools=[final_answer, get_news_from_duckduckgo], ## add your tools here (don't remove final answer)
94
  max_steps=6,
95
  verbosity_level=1,
96
  grammar=None,
requirements.txt CHANGED
@@ -3,3 +3,4 @@ smolagents
3
  requests
4
  duckduckgo_search
5
  pandas
 
 
3
  requests
4
  duckduckgo_search
5
  pandas
6
+ gradio
tmp.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 4,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stdout",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "Requirement already satisfied: playwright in c:\\users\\janal\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (1.50.0)\n",
13
+ "Requirement already satisfied: pyee<13,>=12 in c:\\users\\janal\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from playwright) (12.1.1)\n",
14
+ "Requirement already satisfied: greenlet<4.0.0,>=3.1.1 in c:\\users\\janal\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from playwright) (3.1.1)\n",
15
+ "Requirement already satisfied: typing-extensions in c:\\users\\janal\\appdata\\local\\programs\\python\\python312\\lib\\site-packages (from pyee<13,>=12->playwright) (4.12.2)\n"
16
+ ]
17
+ },
18
+ {
19
+ "name": "stderr",
20
+ "output_type": "stream",
21
+ "text": [
22
+ "\n",
23
+ "[notice] A new release of pip is available: 24.2 -> 25.0.1\n",
24
+ "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
25
+ ]
26
+ }
27
+ ],
28
+ "source": [
29
+ "%%python -m pip install playwright\n",
30
+ "%%playwright install"
31
+ ]
32
+ }
33
+ ],
34
+ "metadata": {
35
+ "language_info": {
36
+ "name": "python"
37
+ }
38
+ },
39
+ "nbformat": 4,
40
+ "nbformat_minor": 2
41
+ }