Abid Ali Awan commited on
Commit
e90303f
·
1 Parent(s): 09851a7

weather and news MCP

Browse files
Files changed (3) hide show
  1. README.md +3 -3
  2. app.py +123 -0
  3. requirements.txt +1 -0
README.md CHANGED
@@ -1,7 +1,7 @@
1
  ---
2
  title: Live City Mcp
3
- emoji: 🐨
4
- colorFrom: gray
5
  colorTo: green
6
  sdk: gradio
7
  sdk_version: 5.32.0
@@ -11,4 +11,4 @@ license: apache-2.0
11
  short_description: Get the latest weather and news on the major cities.
12
  ---
13
 
14
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Live City Mcp
3
+ emoji: 🏙️
4
+ colorFrom: yellow
5
  colorTo: green
6
  sdk: gradio
7
  sdk_version: 5.32.0
 
11
  short_description: Get the latest weather and news on the major cities.
12
  ---
13
 
14
+
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from tavily import TavilyClient
3
+
4
+
5
+ def get_city_weather_info(city_name):
6
+ """
7
+ Search for current weather information about a city using Tavily search API
8
+
9
+ Args:
10
+ city_name (str): Name of the city
11
+ api_key (str): Your Tavily API key
12
+
13
+ Returns:
14
+ dict: Search results containing weather information
15
+ """
16
+ # Initialize Tavily client
17
+ client = TavilyClient()
18
+
19
+ # Search for current weather information
20
+ search_query = f"current weather in {city_name} temperature humidity conditions"
21
+
22
+ try:
23
+ # Perform the search
24
+ response = client.search(
25
+ query=search_query, search_depth="basic", max_results=5
26
+ )
27
+
28
+ # Extract relevant information from search results
29
+ weather_info = {"city": city_name, "search_query": search_query, "results": []}
30
+
31
+ # Process each search result
32
+ for result in response.get("results", []):
33
+ weather_info["results"].append(
34
+ {
35
+ "title": result.get("title", ""),
36
+ "content": result.get("content", ""),
37
+ "url": result.get("url", ""),
38
+ }
39
+ )
40
+
41
+ # Add the answer if available
42
+ if "answer" in response:
43
+ weather_info["summary"] = response["answer"]
44
+
45
+ return weather_info
46
+
47
+ except Exception as e:
48
+ return {
49
+ "city": city_name,
50
+ "error": f"Failed to fetch weather information: {str(e)}",
51
+ }
52
+
53
+
54
+ def get_city_news(city_name):
55
+ """
56
+ Fetches the top 5 news articles for a given city using the Tavily API.
57
+
58
+ Args:
59
+ city_name (str): Name of the city
60
+
61
+ Returns:
62
+ dict: News results containing the top 5 articles or an error message
63
+ """
64
+ # Initialize Tavily client
65
+ client = TavilyClient()
66
+
67
+ # Search query for news about the city
68
+ search_query = f"latest news in {city_name}"
69
+
70
+ try:
71
+ # Perform the search
72
+ response = client.search(
73
+ query=search_query,
74
+ search_depth="basic",
75
+ max_results=5, # Limit to top 5 results
76
+ )
77
+
78
+ # Extract relevant information from search results
79
+ news_info = {"city": city_name, "search_query": search_query, "articles": []}
80
+
81
+ # Process each search result
82
+ for result in response.get("results", []):
83
+ news_info["articles"].append(
84
+ {
85
+ "title": result.get("title", ""),
86
+ "content": result.get("content", ""),
87
+ "url": result.get("url", ""),
88
+ }
89
+ )
90
+
91
+ # Add the answer if available
92
+ if "answer" in response:
93
+ news_info["summary"] = response["answer"]
94
+
95
+ return news_info
96
+
97
+ except Exception as e:
98
+ return {"city": city_name, "error": f"Failed to fetch news: {str(e)}"}
99
+
100
+
101
+ # Create Gradio interfaces
102
+ weather_demo = gr.Interface(
103
+ fn=get_city_weather_info,
104
+ inputs=gr.Textbox(label="Enter City Name", placeholder="e.g., New York"),
105
+ outputs=gr.JSON(label="Weather Information"),
106
+ description="Get current weather details for a city.",
107
+ )
108
+
109
+ news_demo = gr.Interface(
110
+ fn=get_city_news,
111
+ inputs=gr.Textbox(label="Enter City Name", placeholder="e.g., London"),
112
+ outputs=gr.JSON(label="News Articles"),
113
+ description="Get the latest news articles for a city.",
114
+ )
115
+
116
+ # Create tabbed interface
117
+ demo = gr.TabbedInterface(
118
+ [weather_demo, news_demo], ["Weather", "News"], title="City Information Hub"
119
+ )
120
+
121
+ if __name__ == "__main__":
122
+ # Launch without MCP server to avoid initialization issues
123
+ demo.launch(share=False, mcp_server=True, debug=True)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ tavily-python==0.7.3