Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,53 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
-
from
|
| 4 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
@tool
|
| 7 |
def get_btc_price() -> str:
|
| 8 |
"""Fetches the current Bitcoin price in USD using the CoinGecko API."""
|
|
@@ -16,54 +61,81 @@ def get_btc_price() -> str:
|
|
| 16 |
return '{"error": "Bitcoin price not found"}'
|
| 17 |
return f"The current Bitcoin price is ${price:.2f} USD."
|
| 18 |
|
| 19 |
-
|
| 20 |
-
def get_btc_data(query: str) -> str:
|
| 21 |
-
"""
|
| 22 |
-
Fetches Bitcoin data using the CoinGecko API.
|
| 23 |
-
|
| 24 |
-
Args:
|
| 25 |
-
query (str): The type of data to fetch. This can be one of the following: "price", "market_cap", or "circulating_supply".
|
| 26 |
-
|
| 27 |
-
Returns:
|
| 28 |
-
str: The requested Bitcoin data.
|
| 29 |
-
"""
|
| 30 |
-
url = f"https://api.coingecko.com/api/v3/coins/bitcoin?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false"
|
| 31 |
-
response = requests.get(url)
|
| 32 |
-
if response.status_code != 200:
|
| 33 |
-
return '{"error": "Failed to fetch Bitcoin data"}'
|
| 34 |
-
data = response.json()
|
| 35 |
-
if query == "price":
|
| 36 |
-
return f"The current Bitcoin price is ${data['market_data']['current_price']['usd']:.2f} USD."
|
| 37 |
-
elif query == "market_cap":
|
| 38 |
-
return f"The current Bitcoin market capitalization is ${data['market_data']['market_cap']['usd']:.2f} USD."
|
| 39 |
-
elif query == "circulating_supply":
|
| 40 |
-
return f"The current Bitcoin circulating supply is {data['market_data']['circulating_supply']:.2f} BTC."
|
| 41 |
-
else:
|
| 42 |
-
return '{"error": "Invalid query"}'
|
| 43 |
-
|
| 44 |
-
# Initialize the FinalAnswerTool
|
| 45 |
-
final_answer = FinalAnswerTool()
|
| 46 |
-
|
| 47 |
-
# Initialize the model
|
| 48 |
model = HfApiModel(
|
| 49 |
max_tokens=2096,
|
| 50 |
temperature=0.5,
|
| 51 |
-
model_id=
|
| 52 |
custom_role_conversions=None,
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# Create the
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
model=model,
|
| 58 |
-
tools=[get_btc_price,
|
| 59 |
-
max_steps=
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
-
name=
|
| 64 |
-
description=
|
| 65 |
prompt_templates=None # Set to None if not using prompt templates
|
| 66 |
)
|
| 67 |
|
| 68 |
# Launch the Gradio interface
|
| 69 |
-
GradioUI
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import login
|
| 2 |
+
from smolagents import (
|
| 3 |
+
CodeAgent,
|
| 4 |
+
ToolCallingAgent,
|
| 5 |
+
HfApiModel,
|
| 6 |
+
DuckDuckGoSearchTool,
|
| 7 |
+
tool
|
| 8 |
+
)
|
| 9 |
+
import re
|
| 10 |
import requests
|
| 11 |
+
from markdownify import markdownify
|
| 12 |
+
from requests.exceptions import RequestException
|
| 13 |
+
from transformers import pipeline
|
| 14 |
+
|
| 15 |
+
# Login to Hugging Face Hub
|
| 16 |
+
login()
|
| 17 |
+
|
| 18 |
+
# Define the model ID
|
| 19 |
+
model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 20 |
+
|
| 21 |
+
# Define the visit_webpage tool
|
| 22 |
+
@tool
|
| 23 |
+
def visit_webpage(url: str) -> str:
|
| 24 |
+
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
url: The URL of the webpage to visit.
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
The content of the webpage converted to Markdown, or an error message if the request fails.
|
| 31 |
+
"""
|
| 32 |
+
try:
|
| 33 |
+
# Send a GET request to the URL
|
| 34 |
+
response = requests.get(url)
|
| 35 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
| 36 |
+
|
| 37 |
+
# Convert the HTML content to Markdown
|
| 38 |
+
markdown_content = markdownify(response.text).strip()
|
| 39 |
|
| 40 |
+
# Remove multiple line breaks
|
| 41 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
| 42 |
+
|
| 43 |
+
return markdown_content
|
| 44 |
+
|
| 45 |
+
except RequestException as e:
|
| 46 |
+
return f"Error fetching the webpage: {str(e)}"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"An unexpected error occurred: {str(e)}"
|
| 49 |
+
|
| 50 |
+
# Define the get_btc_price tool
|
| 51 |
@tool
|
| 52 |
def get_btc_price() -> str:
|
| 53 |
"""Fetches the current Bitcoin price in USD using the CoinGecko API."""
|
|
|
|
| 61 |
return '{"error": "Bitcoin price not found"}'
|
| 62 |
return f"The current Bitcoin price is ${price:.2f} USD."
|
| 63 |
|
| 64 |
+
# Initialize the Hugging Face model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
model = HfApiModel(
|
| 66 |
max_tokens=2096,
|
| 67 |
temperature=0.5,
|
| 68 |
+
model_id=model_id,
|
| 69 |
custom_role_conversions=None,
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# Create the Web Search Agent
|
| 73 |
+
web_search_agent = ToolCallingAgent(
|
| 74 |
+
tools=[DuckDuckGoSearchTool(), visit_webpage],
|
| 75 |
+
model=model,
|
| 76 |
+
max_steps=10,
|
| 77 |
+
name="web_search",
|
| 78 |
+
description="Runs web searches for you. Give it your query as an argument.",
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Create the Manager Agent
|
| 82 |
+
manager_agent = CodeAgent(
|
| 83 |
+
tools=[get_btc_price],
|
| 84 |
+
model=model,
|
| 85 |
+
managed_agents=[web_search_agent],
|
| 86 |
+
additional_authorized_imports=["time", "numpy", "pandas", "transformers"],
|
| 87 |
+
max_steps=10,
|
| 88 |
+
verbosity_level=1,
|
| 89 |
+
grammar=None,
|
| 90 |
+
planning_interval=None,
|
| 91 |
+
name="btc_manager",
|
| 92 |
+
description="Manages BTC-related tasks, including web searches and price fetching.",
|
| 93 |
+
prompt_templates=None # Set to None if not using prompt templates
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Define a function to summarize the content using a Hugging Face model
|
| 97 |
+
def summarize_content(content: str) -> str:
|
| 98 |
+
"""Summarizes the given content using a Hugging Face summarization model."""
|
| 99 |
+
summarizer = pipeline("summarization")
|
| 100 |
+
summary = summarizer(content, max_length=130, min_length=30, do_sample=False)
|
| 101 |
+
return summary[0]['summary_text']
|
| 102 |
+
|
| 103 |
+
# Define a function to orchestrate the search and summarization
|
| 104 |
+
def search_and_summarize(query: str) -> str:
|
| 105 |
+
"""Searches for the given query, visits the first result, and summarizes the content."""
|
| 106 |
+
# Run the web search agent
|
| 107 |
+
search_result = manager_agent.run(f"Search for '{query}' and visit the first result.")
|
| 108 |
+
|
| 109 |
+
# Extract the URL from the search result
|
| 110 |
+
try:
|
| 111 |
+
url = re.search(r'https?://[^\s]+', search_result).group(0)
|
| 112 |
+
except AttributeError:
|
| 113 |
+
return "Error: Could not extract URL from search result."
|
| 114 |
+
|
| 115 |
+
# Visit the webpage and get the content
|
| 116 |
+
webpage_content = visit_webpage(url)
|
| 117 |
+
|
| 118 |
+
# Summarize the content
|
| 119 |
+
summary = summarize_content(webpage_content)
|
| 120 |
+
|
| 121 |
+
return f"Title: {url}\nSummary: {summary}"
|
| 122 |
+
|
| 123 |
+
# Initialize the FinalAnswerTool
|
| 124 |
+
final_answer = FinalAnswerTool()
|
| 125 |
+
|
| 126 |
+
# Create the BTC Agent
|
| 127 |
+
btc_agent = CodeAgent(
|
| 128 |
model=model,
|
| 129 |
+
tools=[get_btc_price, final_answer, search_and_summarize],
|
| 130 |
+
max_steps=10,
|
| 131 |
verbosity_level=1,
|
| 132 |
grammar=None,
|
| 133 |
planning_interval=None,
|
| 134 |
+
name="btc_agent",
|
| 135 |
+
description="Handles BTC-related tasks, including price fetching and news summarization.",
|
| 136 |
prompt_templates=None # Set to None if not using prompt templates
|
| 137 |
)
|
| 138 |
|
| 139 |
# Launch the Gradio interface
|
| 140 |
+
from Gradio_UI import GradioUI
|
| 141 |
+
GradioUI(btc_agent).launch()
|