Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,77 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel,
|
| 2 |
import requests
|
| 3 |
from tools.final_answer import FinalAnswerTool
|
| 4 |
from Gradio_UI import GradioUI
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
@tool
|
| 8 |
-
def
|
| 9 |
-
"""Fetches
|
| 10 |
-
url = "https://api.coingecko.com/api/v3/simple/price?ids=
|
| 11 |
response = requests.get(url)
|
| 12 |
if response.status_code != 200:
|
| 13 |
-
return '{"error": "Failed to fetch
|
| 14 |
data = response.json()
|
| 15 |
-
price = data.get(
|
| 16 |
if price is None:
|
| 17 |
-
return '{"error": "
|
| 18 |
-
return f"
|
| 19 |
|
| 20 |
-
#
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""Fetches
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
for
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
-
# Initialize
|
| 52 |
model = HfApiModel(
|
| 53 |
-
max_tokens=2096,
|
| 54 |
-
temperature=0.5,
|
| 55 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
)
|
| 58 |
|
| 59 |
-
# Create the agent
|
| 60 |
agent = CodeAgent(
|
| 61 |
model=model,
|
| 62 |
-
tools=[
|
| 63 |
-
max_steps=
|
| 64 |
-
verbosity_level=
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
)
|
| 71 |
|
| 72 |
-
# Launch
|
| 73 |
-
GradioUI(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, tool
|
| 2 |
import requests
|
| 3 |
from tools.final_answer import FinalAnswerTool
|
| 4 |
from Gradio_UI import GradioUI
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
|
| 7 |
+
# Modified CoinGecko Price Tool with crypto parameter
|
| 8 |
@tool
|
| 9 |
+
def get_crypto_price(crypto: str) -> str:
|
| 10 |
+
"""Fetches current price for any cryptocurrency using CoinGecko API."""
|
| 11 |
+
url = f"https://api.coingecko.com/api/v3/simple/price?ids={crypto}&vs_currencies=usd"
|
| 12 |
response = requests.get(url)
|
| 13 |
if response.status_code != 200:
|
| 14 |
+
return f'{{"error": "Failed to fetch {crypto} price"}}'
|
| 15 |
data = response.json()
|
| 16 |
+
price = data.get(crypto, {}).get('usd')
|
| 17 |
if price is None:
|
| 18 |
+
return f'{{"error": "{crypto} price not found"}}'
|
| 19 |
+
return f"Current {crypto} price: ${price:,.2f} USD"
|
| 20 |
|
| 21 |
+
# DuckDuckGo Search Tool with crypto parameter
|
| 22 |
@tool
|
| 23 |
+
def get_crypto_news(crypto: str) -> str:
|
| 24 |
+
"""Fetches latest news for any cryptocurrency using DuckDuckGo."""
|
| 25 |
+
date_filter = (datetime.now() - timedelta(days=3)).strftime("%Y-%m-%d")
|
| 26 |
+
search_query = (
|
| 27 |
+
f"{crypto} cryptocurrency "
|
| 28 |
+
f"news after:{date_filter} "
|
| 29 |
+
"site:coindesk.com OR site:cointelegraph.com OR site:bitcoin.com"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
results = DuckDuckGoSearchTool().run(
|
| 33 |
+
query=search_query,
|
| 34 |
+
max_results=5,
|
| 35 |
+
region="us-en",
|
| 36 |
+
time_limit="d"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
if not results:
|
| 40 |
+
return "No recent news found"
|
| 41 |
+
|
| 42 |
+
news_items = []
|
| 43 |
+
for item in results:
|
| 44 |
+
news_items.append(
|
| 45 |
+
f"**{item.get('title', 'Untitled')}**\n"
|
| 46 |
+
f"{item.get('body', 'No description available')}\n"
|
| 47 |
+
f"Source: {item.get('href', '')}\n"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return "\n\n".join(news_items)
|
| 51 |
|
| 52 |
+
# Initialize agent with enhanced configuration
|
| 53 |
model = HfApiModel(
|
|
|
|
|
|
|
| 54 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 55 |
+
max_tokens=2096,
|
| 56 |
+
temperature=0.3,
|
| 57 |
+
top_p=0.95,
|
| 58 |
)
|
| 59 |
|
|
|
|
| 60 |
agent = CodeAgent(
|
| 61 |
model=model,
|
| 62 |
+
tools=[get_crypto_price, get_crypto_news, DuckDuckGoSearchTool(), FinalAnswerTool()],
|
| 63 |
+
max_steps=8,
|
| 64 |
+
verbosity_level=2,
|
| 65 |
+
safety_guardrails={
|
| 66 |
+
"web_access": "filter",
|
| 67 |
+
"data_privacy": "strict"
|
| 68 |
+
},
|
| 69 |
+
rate_limit=5 # Requests per minute
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# Launch Gradio interface with crypto parameter support
|
| 73 |
+
GradioUI(
|
| 74 |
+
agent,
|
| 75 |
+
default_prompt="Fetch latest price and news for:",
|
| 76 |
+
input_fields=["crypto (e.g., bitcoin)"]
|
| 77 |
+
).launch()
|