Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import shutil
|
|
| 4 |
import json
|
| 5 |
import torch
|
| 6 |
import re
|
|
|
|
| 7 |
import transformers
|
| 8 |
import chardet
|
| 9 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
@@ -444,6 +445,89 @@ def time_tool(query: str) -> str:
|
|
| 444 |
else:
|
| 445 |
return now.strftime(f"The local time in {location.title()} is %I:%M %p on %B %d, %Y.")
|
| 446 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
@tool("summarise")
|
| 448 |
def summarise_tool(query: str) -> str:
|
| 449 |
"""Summarise: Use document summarisation functionality."""
|
|
@@ -572,6 +656,14 @@ time_agent = Agent(
|
|
| 572 |
verbose=True
|
| 573 |
)
|
| 574 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 575 |
math_agent = Agent(
|
| 576 |
role="Math Assistant",
|
| 577 |
goal="Perform accurate arithmetic or logical calculations.",
|
|
@@ -590,7 +682,7 @@ router_agent = Agent(
|
|
| 590 |
role="Query Router",
|
| 591 |
goal="Determine the most suitable agent or tool to handle the user query.",
|
| 592 |
backstory="You are an intelligent query dispatcher that analyses the user's intent and chooses the best AI agent to answer.",
|
| 593 |
-
tools=[python_calc_tool, search_tool_func, csv_tool_func, uploaded_qa_tool_func, summarise_tool, general_chat_tool, time_tool],
|
| 594 |
verbose=True
|
| 595 |
)
|
| 596 |
router_task = Task(
|
|
@@ -602,6 +694,7 @@ Based on the user's query, decide which agent or tool is best suited to handle i
|
|
| 602 |
- If the user uploaded a CSV file and asks about table content, data trends, or uses words like 'data', 'table', 'csv', 'column', or 'row', send it to the **CSV Agent**.
|
| 603 |
- If the user asks about current events, trending topics, or online information (e.g., 'What is LangChain?', 'latest news'), send it to the **Search Agent**.
|
| 604 |
- If the query is about current date, time, or day of week (e.g., 'what is today's date?', 'what time is it?', 'what day is it?', '現在幾點', '今天幾號', '禮拜幾'), send it to the **Time Agent**.
|
|
|
|
| 605 |
- If the question is general and not related to documents, calculations, CSVs, or the internet (e.g., 'Who are you?', 'Tell me a fun fact'), send it to the **General Agent**.
|
| 606 |
- If none of these apply, use your best judgment to choose the most relevant agent.
|
| 607 |
""",
|
|
@@ -611,7 +704,7 @@ Based on the user's query, decide which agent or tool is best suited to handle i
|
|
| 611 |
)
|
| 612 |
|
| 613 |
crew = Crew(
|
| 614 |
-
agents=[general_agent, summarizer_agent, document_qa_agent, search_agent, math_agent, time_agent, csv_agent],
|
| 615 |
tasks=[router_task],
|
| 616 |
process=Process.sequential,
|
| 617 |
verbose=True,
|
|
@@ -631,7 +724,9 @@ def multi_agent_chat_advanced(query: str, file=None) -> str:
|
|
| 631 |
date_keywords = ["what date", "today", "what time", "what day", "current time", "date", "現在幾點", "今天幾號", "禮拜幾"]
|
| 632 |
if any(k in lower_query for k in date_keywords):
|
| 633 |
return get_time_tool(query)
|
| 634 |
-
|
|
|
|
|
|
|
| 635 |
search_keywords = ["latest", "news", "startup", "startups", "company", "companies", "top", "trending", "in 2025", "in 2024", "tell me"]
|
| 636 |
if any(k in lower_query for k in search_keywords):
|
| 637 |
return search_web(query)
|
|
|
|
| 4 |
import json
|
| 5 |
import torch
|
| 6 |
import re
|
| 7 |
+
import requests
|
| 8 |
import transformers
|
| 9 |
import chardet
|
| 10 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 445 |
else:
|
| 446 |
return now.strftime(f"The local time in {location.title()} is %I:%M %p on %B %d, %Y.")
|
| 447 |
|
| 448 |
+
weather_api_key = os.environ.get("WEATHER_API_KEY")
|
| 449 |
+
|
| 450 |
+
|
| 451 |
+
def weather_agent_tool(query: str) -> str:
|
| 452 |
+
try:
|
| 453 |
+
location_prompt = f"""
|
| 454 |
+
You are a location extractor. Given a user's query about weather, return the location mentioned in it. If not found, return "London".
|
| 455 |
+
|
| 456 |
+
Examples:
|
| 457 |
+
- "What's the weather in Tokyo now?" → Tokyo
|
| 458 |
+
- "紐約天氣如何?" → New York
|
| 459 |
+
- "今天台北下雨嗎?" → Taipei
|
| 460 |
+
- "Weather" → London
|
| 461 |
+
- "Is it hot today in ldn?" → London
|
| 462 |
+
|
| 463 |
+
Now process this query: "{query}"
|
| 464 |
+
"""
|
| 465 |
+
location_response = llm_gpt4.invoke(location_prompt)
|
| 466 |
+
if isinstance(location_response, AIMessage):
|
| 467 |
+
location = location_response.content.strip()
|
| 468 |
+
else:
|
| 469 |
+
location = str(location_response).strip()
|
| 470 |
+
|
| 471 |
+
url = f"http://api.weatherapi.com/v1/current.json?key={weather_api_key}&q={location}&aqi=no"
|
| 472 |
+
response = requests.get(url)
|
| 473 |
+
data = response.json()
|
| 474 |
+
|
| 475 |
+
if "current" not in data:
|
| 476 |
+
return f"Sorry, I couldn't find the weather info for '{location}'."
|
| 477 |
+
|
| 478 |
+
current = data["current"]
|
| 479 |
+
condition = current["condition"]["text"]
|
| 480 |
+
temp_c = current["temp_c"]
|
| 481 |
+
humidity = current["humidity"]
|
| 482 |
+
wind_kph = current["wind_kph"]
|
| 483 |
+
feelslike_c = current["feelslike_c"]
|
| 484 |
+
|
| 485 |
+
return f"The current weather in {location.title()} is {condition} with {temp_c}°C, feels like {feelslike_c}°C, humidity {humidity}%, wind {wind_kph} kph."
|
| 486 |
+
|
| 487 |
+
except Exception as e:
|
| 488 |
+
return f"Weather API error: {e}"
|
| 489 |
+
|
| 490 |
+
@tool("weather")
|
| 491 |
+
def weather_tool(query: str) -> str:
|
| 492 |
+
"""Weather Agent: Provide real-time weather info for any major city."""
|
| 493 |
+
try:
|
| 494 |
+
location_prompt = f"""
|
| 495 |
+
You are a location extractor. Given a user's query about weather, return the location mentioned in it. If not found, return "London".
|
| 496 |
+
|
| 497 |
+
Examples:
|
| 498 |
+
- "What's the weather in Tokyo now?" → Tokyo
|
| 499 |
+
- "紐約天氣如何?" → New York
|
| 500 |
+
- "今天台北下雨嗎?" → Taipei
|
| 501 |
+
- "Weather" → London
|
| 502 |
+
- "Is it hot today in ldn?" → London
|
| 503 |
+
|
| 504 |
+
Now process this query: "{query}"
|
| 505 |
+
"""
|
| 506 |
+
location_response = llm_gpt4.invoke(location_prompt)
|
| 507 |
+
if isinstance(location_response, AIMessage):
|
| 508 |
+
location = location_response.content.strip()
|
| 509 |
+
else:
|
| 510 |
+
location = str(location_response).strip()
|
| 511 |
+
|
| 512 |
+
url = f"http://api.weatherapi.com/v1/current.json?key={weather_api_key}&q={location}&aqi=no"
|
| 513 |
+
response = requests.get(url)
|
| 514 |
+
data = response.json()
|
| 515 |
+
|
| 516 |
+
if "current" not in data:
|
| 517 |
+
return f"Sorry, I couldn't find the weather info for '{location}'."
|
| 518 |
+
|
| 519 |
+
current = data["current"]
|
| 520 |
+
condition = current["condition"]["text"]
|
| 521 |
+
temp_c = current["temp_c"]
|
| 522 |
+
humidity = current["humidity"]
|
| 523 |
+
wind_kph = current["wind_kph"]
|
| 524 |
+
feelslike_c = current["feelslike_c"]
|
| 525 |
+
|
| 526 |
+
return f"The current weather in {location.title()} is {condition} with {temp_c}°C, feels like {feelslike_c}°C, humidity {humidity}%, wind {wind_kph} kph."
|
| 527 |
+
|
| 528 |
+
except Exception as e:
|
| 529 |
+
return f"Weather API error: {e}"
|
| 530 |
+
|
| 531 |
@tool("summarise")
|
| 532 |
def summarise_tool(query: str) -> str:
|
| 533 |
"""Summarise: Use document summarisation functionality."""
|
|
|
|
| 656 |
verbose=True
|
| 657 |
)
|
| 658 |
|
| 659 |
+
weather_agent = Agent(
|
| 660 |
+
role="Weather Expert",
|
| 661 |
+
goal="Answer global weather queries.",
|
| 662 |
+
backstory="You are a weather analyst who provides accurate and real-time weather information for any location.",
|
| 663 |
+
tools=[weather_tool],
|
| 664 |
+
verbose=True
|
| 665 |
+
)
|
| 666 |
+
|
| 667 |
math_agent = Agent(
|
| 668 |
role="Math Assistant",
|
| 669 |
goal="Perform accurate arithmetic or logical calculations.",
|
|
|
|
| 682 |
role="Query Router",
|
| 683 |
goal="Determine the most suitable agent or tool to handle the user query.",
|
| 684 |
backstory="You are an intelligent query dispatcher that analyses the user's intent and chooses the best AI agent to answer.",
|
| 685 |
+
tools=[python_calc_tool, search_tool_func, csv_tool_func, uploaded_qa_tool_func, summarise_tool, general_chat_tool, time_tool, weather_tool],
|
| 686 |
verbose=True
|
| 687 |
)
|
| 688 |
router_task = Task(
|
|
|
|
| 694 |
- If the user uploaded a CSV file and asks about table content, data trends, or uses words like 'data', 'table', 'csv', 'column', or 'row', send it to the **CSV Agent**.
|
| 695 |
- If the user asks about current events, trending topics, or online information (e.g., 'What is LangChain?', 'latest news'), send it to the **Search Agent**.
|
| 696 |
- If the query is about current date, time, or day of week (e.g., 'what is today's date?', 'what time is it?', 'what day is it?', '現在幾點', '今天幾號', '禮拜幾'), send it to the **Time Agent**.
|
| 697 |
+
- If the query is about weather, rain, temperature, or forecasts (e.g., "What's the weather in Paris?", "Will it rain tomorrow in London?"), send it to the **Weather Agent**.
|
| 698 |
- If the question is general and not related to documents, calculations, CSVs, or the internet (e.g., 'Who are you?', 'Tell me a fun fact'), send it to the **General Agent**.
|
| 699 |
- If none of these apply, use your best judgment to choose the most relevant agent.
|
| 700 |
""",
|
|
|
|
| 704 |
)
|
| 705 |
|
| 706 |
crew = Crew(
|
| 707 |
+
agents=[general_agent, summarizer_agent, document_qa_agent, search_agent, math_agent, time_agent, csv_agent, weather_agent],
|
| 708 |
tasks=[router_task],
|
| 709 |
process=Process.sequential,
|
| 710 |
verbose=True,
|
|
|
|
| 724 |
date_keywords = ["what date", "today", "what time", "what day", "current time", "date", "現在幾點", "今天幾號", "禮拜幾"]
|
| 725 |
if any(k in lower_query for k in date_keywords):
|
| 726 |
return get_time_tool(query)
|
| 727 |
+
weather_keywords = ["weather", "rain", "temperature", "forecast", "天氣", "會不會下雨", "冷嗎", "熱嗎"]
|
| 728 |
+
if any(k in lower_query for k in weather_keywords):
|
| 729 |
+
return weather_agent_tool(query)
|
| 730 |
search_keywords = ["latest", "news", "startup", "startups", "company", "companies", "top", "trending", "in 2025", "in 2024", "tell me"]
|
| 731 |
if any(k in lower_query for k in search_keywords):
|
| 732 |
return search_web(query)
|