Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,37 +48,38 @@ def fact_checker(claim:str)-> str: #it's import to specify the return type
|
|
| 48 |
return f"Error during fact-checking: {str(e)}"
|
| 49 |
|
| 50 |
|
|
|
|
|
|
|
| 51 |
@tool
|
| 52 |
def stock_price(symbol: str) -> str:
|
| 53 |
-
"""A tool that retrieves the current stock price for a given stock symbol.
|
| 54 |
Args:
|
| 55 |
symbol: The ticker symbol of the stock (e.g., AAPL for Apple Inc.).
|
| 56 |
Returns:
|
| 57 |
A summary indicating the current price of the stock or an error message if unavailable.
|
| 58 |
"""
|
| 59 |
try:
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
ticker = yf.Ticker(symbol)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
except Exception as e:
|
| 80 |
return f"An error occurred while retrieving the stock price for {symbol}: {e}"
|
| 81 |
|
|
|
|
| 82 |
@tool
|
| 83 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 84 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 48 |
return f"Error during fact-checking: {str(e)}"
|
| 49 |
|
| 50 |
|
| 51 |
+
import re
|
| 52 |
+
|
| 53 |
@tool
|
| 54 |
def stock_price(symbol: str) -> str:
|
| 55 |
+
"""A tool that retrieves the current stock price for a given stock symbol using DuckDuckGo search.
|
| 56 |
Args:
|
| 57 |
symbol: The ticker symbol of the stock (e.g., AAPL for Apple Inc.).
|
| 58 |
Returns:
|
| 59 |
A summary indicating the current price of the stock or an error message if unavailable.
|
| 60 |
"""
|
| 61 |
try:
|
| 62 |
+
# Instantiate the DuckDuckGo search tool (ensure it's implemented or installed in your environment)
|
| 63 |
+
search_tool = DuckDuckGoSearchTool(max_results=5)
|
| 64 |
+
query = f"{symbol} current stock price"
|
|
|
|
| 65 |
|
| 66 |
+
# Fetch search results for the query
|
| 67 |
+
results = search_tool.forward(query)
|
| 68 |
|
| 69 |
+
# Use a simple regex to attempt extraction of a stock price (e.g., $123.45)
|
| 70 |
+
# Note: This regex is basic and may need adjustments for more robust extraction.
|
| 71 |
+
match = re.search(r"\$?(\d{1,3}(?:,\d{3})*(?:\.\d+)?)(?!\S)", results)
|
| 72 |
+
if match:
|
| 73 |
+
# Clean the extracted string and convert to a float
|
| 74 |
+
price_str = match.group(1).replace(",", "")
|
| 75 |
+
price = float(price_str)
|
| 76 |
+
return f"The current price for {symbol} is ${price:.2f}."
|
| 77 |
+
else:
|
| 78 |
+
return f"Could not extract the current price for {symbol} from search results."
|
|
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"An error occurred while retrieving the stock price for {symbol}: {e}"
|
| 81 |
|
| 82 |
+
|
| 83 |
@tool
|
| 84 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 85 |
"""A tool that fetches the current local time in a specified timezone.
|