Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
|
| 2 |
import requests
|
| 3 |
from tools.final_answer import FinalAnswerTool
|
| 4 |
from Gradio_UI import GradioUI
|
| 5 |
|
| 6 |
-
#
|
| 7 |
@tool
|
| 8 |
def get_btc_price() -> str:
|
| 9 |
"""Fetches the current Bitcoin price in USD using the CoinGecko API."""
|
|
@@ -17,28 +17,35 @@ def get_btc_price() -> str:
|
|
| 17 |
return '{"error": "Bitcoin price not found"}'
|
| 18 |
return f"The current Bitcoin price is ${price:.2f} USD."
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_btc_news() -> str:
|
| 23 |
-
"""Fetches the latest Bitcoin news
|
| 24 |
search_query = "latest Bitcoin news"
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
return '{"error": "Failed to fetch Bitcoin news"}'
|
| 28 |
-
|
| 29 |
-
# Extract titles and meta descriptions from the search results
|
| 30 |
-
news_titles = [result['title'] for result in search_results]
|
| 31 |
-
news_descriptions = [result['snippet'] for result in search_results]
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
# Initialize the FinalAnswerTool
|
| 39 |
final_answer = FinalAnswerTool()
|
| 40 |
|
| 41 |
-
# Initialize the model
|
| 42 |
model = HfApiModel(
|
| 43 |
max_tokens=2096,
|
| 44 |
temperature=0.5,
|
|
@@ -49,7 +56,7 @@ model = HfApiModel(
|
|
| 49 |
# Create the agent
|
| 50 |
agent = CodeAgent(
|
| 51 |
model=model,
|
| 52 |
-
tools=[get_btc_price, get_btc_news, final_answer], # Add
|
| 53 |
max_steps=6,
|
| 54 |
verbosity_level=1,
|
| 55 |
grammar=None,
|
|
@@ -59,5 +66,5 @@ agent = CodeAgent(
|
|
| 59 |
prompt_templates=None # Set to None if not using prompt templates
|
| 60 |
)
|
| 61 |
|
| 62 |
-
# Launch the Gradio
|
| 63 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import requests
|
| 3 |
from tools.final_answer import FinalAnswerTool
|
| 4 |
from Gradio_UI import GradioUI
|
| 5 |
|
| 6 |
+
# Tool for fetching Bitcoin price
|
| 7 |
@tool
|
| 8 |
def get_btc_price() -> str:
|
| 9 |
"""Fetches the current Bitcoin price in USD using the CoinGecko API."""
|
|
|
|
| 17 |
return '{"error": "Bitcoin price not found"}'
|
| 18 |
return f"The current Bitcoin price is ${price:.2f} USD."
|
| 19 |
|
| 20 |
+
|
| 21 |
+
# Tool for fetching Bitcoin news using DuckDuckGo search
|
| 22 |
@tool
|
| 23 |
def get_btc_news() -> str:
|
| 24 |
+
"""Fetches the latest Bitcoin news from DuckDuckGo search."""
|
| 25 |
search_query = "latest Bitcoin news"
|
| 26 |
+
# Performing a DuckDuckGo search using the DuckDuckGoSearchTool
|
| 27 |
+
search_results = DuckDuckGoSearchTool().run(search_query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Ensure search_results is a list of articles with titles and meta descriptions
|
| 30 |
+
try:
|
| 31 |
+
# Process the results and extract titles and descriptions
|
| 32 |
+
articles = []
|
| 33 |
+
for result in search_results['RelatedTopics']:
|
| 34 |
+
title = result.get('Text', '')
|
| 35 |
+
description = result.get('FirstURL', '')
|
| 36 |
+
articles.append(f"Title: {title}\nDescription: {description}\n")
|
| 37 |
+
if articles:
|
| 38 |
+
return "\n\n".join(articles)
|
| 39 |
+
else:
|
| 40 |
+
return "No relevant news found."
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error fetching Bitcoin news: {str(e)}"
|
| 43 |
+
|
| 44 |
|
| 45 |
+
# Initialize the FinalAnswerTool
|
| 46 |
final_answer = FinalAnswerTool()
|
| 47 |
|
| 48 |
+
# Initialize the model
|
| 49 |
model = HfApiModel(
|
| 50 |
max_tokens=2096,
|
| 51 |
temperature=0.5,
|
|
|
|
| 56 |
# Create the agent
|
| 57 |
agent = CodeAgent(
|
| 58 |
model=model,
|
| 59 |
+
tools=[get_btc_price, get_btc_news, final_answer], # Add your tools here
|
| 60 |
max_steps=6,
|
| 61 |
verbosity_level=1,
|
| 62 |
grammar=None,
|
|
|
|
| 66 |
prompt_templates=None # Set to None if not using prompt templates
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# Launch the Gradio interface
|
| 70 |
GradioUI(agent).launch()
|