Spaces:
Running
Running
Abid Ali Awan
Refine search query in get_city_news function to include specific topics and enhance relevance
1cad718
import gradio as gr | |
from tavily import TavilyClient | |
def get_city_weather_info(city_name): | |
""" | |
Search for current weather information about a city using Tavily search API | |
Args: | |
city_name (str): Name of the city | |
api_key (str): Your Tavily API key | |
Returns: | |
dict: Search results containing weather information | |
""" | |
# Initialize Tavily client | |
client = TavilyClient() | |
# Search for current weather information | |
search_query = f"current weather in {city_name} temperature humidity conditions" | |
try: | |
# Perform the search | |
response = client.search( | |
query=search_query, search_depth="basic", max_results=5 | |
) | |
# Extract relevant information from search results | |
weather_info = {"city": city_name, "search_query": search_query, "results": []} | |
# Process each search result | |
for result in response.get("results", []): | |
weather_info["results"].append( | |
{ | |
"title": result.get("title", ""), | |
"content": result.get("content", ""), | |
"url": result.get("url", ""), | |
} | |
) | |
# Add the answer if available | |
if "answer" in response: | |
weather_info["summary"] = response["answer"] | |
return weather_info | |
except Exception as e: | |
return { | |
"city": city_name, | |
"error": f"Failed to fetch weather information: {str(e)}", | |
} | |
def get_city_news(city_name): | |
""" | |
Fetches the top 5 news articles for a given city using the Tavily API. | |
Args: | |
city_name (str): Name of the city | |
Returns: | |
dict: News results containing the top 5 articles or an error message | |
""" | |
# Initialize Tavily client | |
client = TavilyClient() | |
# Search query for news about the city | |
search_query = f"Top 5 latest news articles about {city_name}, including major events, politics, economy, and local updates." | |
try: | |
# Perform the search | |
response = client.search( | |
query=search_query, | |
search_depth="advanced", | |
max_results=5, | |
) | |
# Extract relevant information from search results | |
news_info = {"city": city_name, "search_query": search_query, "articles": []} | |
# Process each search result | |
for result in response.get("results", []): | |
news_info["articles"].append( | |
{ | |
"title": result.get("title", ""), | |
"content": result.get("content", ""), | |
"url": result.get("url", ""), | |
} | |
) | |
# Add the answer if available | |
if "answer" in response: | |
news_info["summary"] = response["answer"] | |
return news_info | |
except Exception as e: | |
return {"city": city_name, "error": f"Failed to fetch news: {str(e)}"} | |
# Create Gradio interfaces | |
weather_demo = gr.Interface( | |
fn=get_city_weather_info, | |
inputs=gr.Textbox(label="Enter City Name", placeholder="e.g., New York"), | |
outputs=gr.JSON(label="Weather Information"), | |
description="Get current weather details for a city.", | |
) | |
news_demo = gr.Interface( | |
fn=get_city_news, | |
inputs=gr.Textbox(label="Enter City Name", placeholder="e.g., London"), | |
outputs=gr.JSON(label="News Articles"), | |
description="Get the latest news articles for a city.", | |
) | |
# Create tabbed interface | |
demo = gr.TabbedInterface( | |
[weather_demo, news_demo], ["Weather", "News"], title="City Information Hub" | |
) | |
if __name__ == "__main__": | |
# Launch without MCP server to avoid initialization issues | |
demo.launch(share=False, mcp_server=True, debug=True) | |