Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,112 +1,71 @@
|
|
| 1 |
-
from smolagents import CodeAgent, HfApiModel, tool
|
| 2 |
import requests
|
| 3 |
-
from
|
|
|
|
|
|
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
| 5 |
from Gradio_UI import GradioUI
|
| 6 |
-
import markdownify
|
| 7 |
-
import re
|
| 8 |
-
|
| 9 |
-
@tool
|
| 10 |
-
def web_search(query: str) -> str:
|
| 11 |
-
"""Performs a web search using DuckDuckGo and returns the top search results.
|
| 12 |
-
|
| 13 |
-
Args:
|
| 14 |
-
query: The search query string.
|
| 15 |
-
|
| 16 |
-
Returns:
|
| 17 |
-
A formatted string with the top search results, or an error message if the request fails.
|
| 18 |
-
"""
|
| 19 |
-
try:
|
| 20 |
-
url = f"https://duckduckgo.com/html/?q={query.replace(' ', '+')}"
|
| 21 |
-
headers = {"User-Agent": "Mozilla/5.0"}
|
| 22 |
-
response = requests.get(url, headers=headers)
|
| 23 |
-
response.raise_for_status()
|
| 24 |
-
|
| 25 |
-
# Extract search results
|
| 26 |
-
search_results = re.findall(r'<a rel="nofollow" class="result__url" href="(.*?)">', response.text)
|
| 27 |
-
|
| 28 |
-
if not search_results:
|
| 29 |
-
return "No relevant results found."
|
| 30 |
-
|
| 31 |
-
# Format the top results
|
| 32 |
-
results_str = "\n".join([f"{i+1}. {result}" for i, result in enumerate(search_results[:5])])
|
| 33 |
-
return f"Top search results:\n{results_str}"
|
| 34 |
-
|
| 35 |
-
except RequestException as e:
|
| 36 |
-
return f"Error performing web search: {str(e)}"
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return f"An unexpected error occurred: {str(e)}"
|
| 39 |
-
|
| 40 |
-
@tool
|
| 41 |
-
def visit_webpage(url: str) -> str:
|
| 42 |
-
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
| 43 |
-
|
| 44 |
-
Args:
|
| 45 |
-
url: The URL of the webpage to visit.
|
| 46 |
-
|
| 47 |
-
Returns:
|
| 48 |
-
The content of the webpage converted to Markdown, or an error message if the request fails.
|
| 49 |
-
"""
|
| 50 |
-
try:
|
| 51 |
-
response = requests.get(url)
|
| 52 |
-
response.raise_for_status() # Raise an exception for bad status codes
|
| 53 |
-
|
| 54 |
-
# Convert the HTML content to Markdown
|
| 55 |
-
markdown_content = markdownify.markdownify(response.text).strip()
|
| 56 |
-
|
| 57 |
-
# Remove multiple line breaks
|
| 58 |
-
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 59 |
-
|
| 60 |
-
return markdown_content
|
| 61 |
-
|
| 62 |
-
except RequestException as e:
|
| 63 |
-
return f"Error fetching the webpage: {str(e)}"
|
| 64 |
-
except Exception as e:
|
| 65 |
-
return f"An unexpected error occurred: {str(e)}"
|
| 66 |
|
| 67 |
@tool
|
| 68 |
def get_btc_price() -> str:
|
| 69 |
"""Fetches the current Bitcoin price in USD using the CoinGecko API."""
|
| 70 |
url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
try:
|
| 72 |
-
|
|
|
|
| 73 |
response.raise_for_status()
|
| 74 |
-
data = response.json()
|
| 75 |
-
price = data.get('bitcoin', {}).get('usd')
|
| 76 |
-
|
| 77 |
-
if price is None:
|
| 78 |
-
return '{"error": "Bitcoin price not found"}'
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
except RequestException as e:
|
| 83 |
-
return f"Error fetching Bitcoin price: {str(e)}"
|
| 84 |
except Exception as e:
|
| 85 |
-
return f"
|
| 86 |
|
| 87 |
# Initialize the FinalAnswerTool
|
| 88 |
final_answer = FinalAnswerTool()
|
| 89 |
|
| 90 |
-
# Initialize the model
|
| 91 |
model = HfApiModel(
|
| 92 |
-
max_tokens=
|
| 93 |
temperature=0.5,
|
| 94 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 95 |
custom_role_conversions=None,
|
| 96 |
)
|
| 97 |
|
| 98 |
-
# Create the agent
|
| 99 |
agent = CodeAgent(
|
| 100 |
model=model,
|
| 101 |
-
tools=[
|
| 102 |
max_steps=6,
|
| 103 |
verbosity_level=1,
|
| 104 |
-
grammar=None,
|
| 105 |
-
planning_interval=None,
|
| 106 |
-
name=None,
|
| 107 |
-
description=None,
|
| 108 |
-
prompt_templates=None # Set to None if not using prompt templates
|
| 109 |
)
|
| 110 |
|
| 111 |
# Launch the Gradio interface
|
| 112 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from markdownify import markdownify
|
| 5 |
+
import re
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@tool
|
| 10 |
def get_btc_price() -> str:
|
| 11 |
"""Fetches the current Bitcoin price in USD using the CoinGecko API."""
|
| 12 |
url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd"
|
| 13 |
+
response = requests.get(url)
|
| 14 |
+
if response.status_code != 200:
|
| 15 |
+
return '{"error": "Failed to fetch Bitcoin price"}'
|
| 16 |
+
data = response.json()
|
| 17 |
+
price = data.get('bitcoin', {}).get('usd')
|
| 18 |
+
if price is None:
|
| 19 |
+
return '{"error": "Bitcoin price not found"}'
|
| 20 |
+
return f"The current Bitcoin price is ${price:.2f} USD."
|
| 21 |
+
|
| 22 |
+
@tool
|
| 23 |
+
def get_btc_technical_analysis(url: str) -> str:
|
| 24 |
+
"""Fetches technical analysis summary from TradingView BTCUSD page."""
|
| 25 |
try:
|
| 26 |
+
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
|
| 27 |
+
response = requests.get(url, headers=headers)
|
| 28 |
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 31 |
+
|
| 32 |
+
# Extract technical summary section
|
| 33 |
+
summary_section = soup.find('div', class_='technicals-summary__section-content')
|
| 34 |
+
if not summary_section:
|
| 35 |
+
return "Technical analysis summary not found"
|
| 36 |
+
|
| 37 |
+
# Extract key technical indicators
|
| 38 |
+
indicators = soup.find_all('div', class_='item-1O4B2P_P')
|
| 39 |
+
indicator_data = "\n".join([i.get_text(separator=" ", strip=True) for i in indicators[:3]])
|
| 40 |
+
|
| 41 |
+
# Convert to markdown and clean up
|
| 42 |
+
content = f"Summary: {summary_section.get_text()}\nKey Indicators:\n{indicator_data}"
|
| 43 |
+
markdown_content = markdownify(str(content)).strip()
|
| 44 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 45 |
+
|
| 46 |
+
return markdown_content[:1500] # Truncate to ensure token limit
|
| 47 |
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
+
return f"Error fetching technical analysis: {str(e)}"
|
| 50 |
|
| 51 |
# Initialize the FinalAnswerTool
|
| 52 |
final_answer = FinalAnswerTool()
|
| 53 |
|
| 54 |
+
# Initialize the model with adjusted parameters
|
| 55 |
model = HfApiModel(
|
| 56 |
+
max_tokens=800, # Reduced from 1000
|
| 57 |
temperature=0.5,
|
| 58 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 59 |
custom_role_conversions=None,
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Create the agent with optimized tools
|
| 63 |
agent = CodeAgent(
|
| 64 |
model=model,
|
| 65 |
+
tools=[get_btc_price, get_btc_technical_analysis, final_answer],
|
| 66 |
max_steps=6,
|
| 67 |
verbosity_level=1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
# Launch the Gradio interface
|
| 71 |
+
GradioUI(agent).launch()
|