File size: 11,989 Bytes
883c056
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21aeb68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d0b08e
9f84710
21aeb68
883c056
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import requests
import gradio as gr
from dotenv import load_dotenv
import re
from typing import Optional
import base64
from time import sleep


def get_book_recommendations(prompt: str) -> str:
    """
    Fetches a book recommendation from Google Books API.
    Args:
        prompt: User search query, e.g., "thriller novel by Gillian Flynn"
    Returns:
        Top book recommendation in formatted text.
    """
    load_dotenv()  # Make sure this is called before accessing environment variables

    api_key = os.getenv("GOOGLE_BOOKS_API_KEY")
    if not api_key:
        return "API key not found."

    search_query = prompt.strip()
    url = f"https://www.googleapis.com/books/v1/volumes?q={search_query}&key={api_key}"

    try:
        response = requests.get(url, timeout=10)
        response.raise_for_status()
        books = response.json().get("items", [])
        if not books:
            return "No books found."

        volume_info = books[0].get("volumeInfo", {})
        title = volume_info.get("title", "Unknown Title")
        authors = ", ".join(volume_info.get("authors", ["Unknown Author"]))
        description = volume_info.get("description", "No description available.")

        return f"πŸ“š **{title}** by *{authors}*\n\n{description}"
    except Exception as e:
        return f"Error: {str(e)}"
    

def get_random_recipe(tags: str = "", exclude_ingredients: str = "") -> str:
    """
    Fetches a random recipe using the Spoonacular API and returns it in a formatted string.

    Args:
        tags (str): Comma-separated tags like 'vegetarian,dessert'.
        exclude_ingredients (str): Comma-separated ingredients to exclude.

    Returns:
        str: A formatted string with title, preparation time, ingredients, and cooking steps.
    
    """
    load_dotenv()

    recipe_api_key = os.getenv("SPOONACULAR_API_KEY")
    if not recipe_api_key:
        return "❌ API key not found in environment."

    url = "https://api.spoonacular.com/recipes/random"
    params = {
        "number": 1,
        "tags": tags,
        "excludeIngredients": exclude_ingredients,
        "apiKey": recipe_api_key
    }

    try:
        response = requests.get(url, params=params, timeout=10)
        data = response.json()
    except Exception:
        return "❌ Failed to parse JSON from API response."

    if response.status_code != 200:
        return f"❌ API Error: {data.get('message', 'Unknown error')}"

    if not data.get("recipes"):
        return "❌ No recipes returned."

    recipe = data["recipes"][0]

    title = recipe.get("title", "Unknown")
    ready_in = recipe.get("readyInMinutes", "?")
    ingredients = [i["name"] for i in recipe.get("extendedIngredients", [])]

    steps = []
    for block in recipe.get("analyzedInstructions", []):
        for step in block.get("steps", []):
            steps.append(step["step"])

    formatted = f"🍽️ **Title**: {title}\n"
    formatted += f"πŸ•’ **Ready in**: {ready_in} minutes\n"
    formatted += f"πŸ“‹ **Ingredients**: {', '.join(ingredients) if ingredients else 'N/A'}\n"
    formatted += "πŸ‘¨β€πŸ³ **Steps**:\n"
    for idx, step in enumerate(steps, 1):
        formatted += f"  {idx}. {step.strip()}\n"

    return formatted.strip()




def get_now_playing_movies(retries=3) -> str:
    api_key = os.getenv("TMDB_API_KEY")
    url = f"https://api.themoviedb.org/3/movie/now_playing?api_key={api_key}&language=en-US&page=1"

    for attempt in range(1, retries + 1):
        try:
            response = requests.get(url, timeout=10)
            response.raise_for_status()
            movies = response.json().get("results", [])
            if not movies:
                return "No movies currently playing."

            formatted = []
            for movie in movies[:5]:
                title = movie.get("title", "Untitled")
                overview = movie.get("overview", "No description available.")
                formatted.append(f"🎬 **{title}**\n{overview}\n")
            return "\n".join(formatted)

        except Exception as e:
            if attempt == retries:
                return f"❌ Failed after {retries} attempts: {e}"
            sleep(1)  # Wait before retrying

def get_music_recommendations(prompt: str, num_songs: int = 3, min_popularity: int = 0, year: str = "") -> str:
    """
    Fetches multiple music recommendations from Spotify API.
    
    Args:
        prompt: Search query (genre, mood, etc.)
        num_songs: Number of song recommendations to return.
        min_popularity: Minimum popularity score (0–100).
        year: Optional release year filter, e.g., '2022' or '2010-2020'.
    
    Returns:
        Markdown-formatted string of top song recommendations.
    """
    load_dotenv()
    client_id = os.getenv("SPOTIFY_CLIENT_ID")
    client_secret = os.getenv("SPOTIFY_CLIENT_SECRET")

    if not client_id or not client_secret:
        return "Spotify API credentials not found."

    # Encode client_id:client_secret
    auth_str = f"{client_id}:{client_secret}"
    b64_auth_str = base64.b64encode(auth_str.encode()).decode()

    try:
        token_response = requests.post(
            "https://accounts.spotify.com/api/token",
            data={"grant_type": "client_credentials"},
            headers={"Authorization": f"Basic {b64_auth_str}"}
        )
        token_response.raise_for_status()
        access_token = token_response.json().get("access_token")
    except Exception as e:
        return f"Token Error: {str(e)}"

    try:
        # Append year filter if provided
        search_query = prompt.strip()
        if year:
            search_query += f" year:{year}"

        url = f"https://api.spotify.com/v1/search?q={search_query}&type=track&limit={num_songs}"
        headers = {"Authorization": f"Bearer {access_token}"}

        response = requests.get(url, headers=headers, timeout=10)
        response.raise_for_status()
        tracks = response.json().get("tracks", {}).get("items", [])

        if not tracks:
            return "No songs found."

        # Filter by popularity and format results
        filtered_tracks = [
            track for track in tracks if track["popularity"] >= min_popularity
        ]

        if not filtered_tracks:
            return f"No songs found with popularity β‰₯ {min_popularity}."

        output_lines = []
        for track in filtered_tracks:
            name = track["name"]
            artist = ", ".join(artist["name"] for artist in track["artists"])
            popularity = track["popularity"]
            spotify_url = track["external_urls"]["spotify"]
            preview_url = track.get("preview_url")

            entry = f"🎡 **{name}** by *{artist}* (Popularity: {popularity})\n[Listen on Spotify]({spotify_url})"
            if preview_url:
                entry += f"\nPreview: {preview_url}"
            output_lines.append(entry)

        return "\n\n---\n\n".join(output_lines)
    except Exception as e:
        return f"Error: {str(e)}"
    


def get_current_weather(location: str) -> str:
    """
    Fetches current weather for a given city name or coordinates using OpenWeatherMap API.

    Args:
        location: A city name (e.g., "New York") or "lat,lon" format (e.g., "44.34,10.99")

    Returns:
        A formatted weather report string.
    """
    load_dotenv()

    api_key = os.getenv("OPENWEATHER_API_KEY")
    if not api_key:
        return "OpenWeatherMap API key not found."

    try:
        # Check if input is coordinates
        if "," in location:
            lat, lon = map(str.strip, location.split(","))
        else:
            # Use geocoding API to get coordinates from city name
            geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={location}&limit=1&appid={api_key}"
            geo_resp = requests.get(geo_url, timeout=10)
            geo_resp.raise_for_status()
            geo_data = geo_resp.json()
            if not geo_data:
                return f"Could not find location: {location}"
            lat = geo_data[0]["lat"]
            lon = geo_data[0]["lon"]

        # Get weather
        weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={api_key}&units=metric"
        weather_resp = requests.get(weather_url, timeout=10)
        weather_resp.raise_for_status()
        data = weather_resp.json()

        city = f"{data.get('name', 'Unknown')}, {data['sys'].get('country', '')}"
        condition = data["weather"][0]["description"].capitalize()
        temp = data["main"]["temp"]
        feels_like = data["main"]["feels_like"]
        humidity = data["main"]["humidity"]
        wind = data["wind"]["speed"]

        return (
            f"🌀 **Weather in {city}**\n"
            f"Condition: {condition}\n"
            f"🌑 Temperature: {temp}°C (Feels like {feels_like}°C)\n"
            f"πŸ’§ Humidity: {humidity}%\n"
            f"🌬 Wind Speed: {wind} m/s"
        )

    except Exception as e:
        return f"Error fetching weather: {str(e)}"


# Define Gradio interface
book_interface = gr.Interface(
    fn=get_book_recommendations,
    inputs=gr.Textbox(label="Enter your book query"),
    outputs=gr.Markdown(label="Top Recommendation"),
    api_name="get_book_recommendations"
)

recipe_interface = gr.Interface(
    fn=get_random_recipe,
    inputs=[
        gr.Textbox(label="Tags (e.g., vegetarian,dessert)", placeholder="Enter tags or leave blank"),
        gr.Textbox(label="Exclude Ingredients", placeholder="e.g., nuts,gluten")
    ],
    outputs=gr.Markdown(label="Random Recipe"),
    api_name="get_random_recipe"  # This is critical for Hugging Face MCP endpoints
)

movie_interface = gr.Interface(
    fn=get_now_playing_movies,
    inputs=[],  # No input needed for now-playing movies
    outputs=gr.Markdown(label="Now Playing Movies"),
    api_name="get_now_playing_movies"
)

music_interface = gr.Interface(
    fn=get_music_recommendations,
    inputs=[
        gr.Textbox(label="Enter your music query (e.g., 'lofi beats', 'happy pop')"),
        gr.Slider(minimum=1, maximum=10, value=3, label="Number of Songs"),
        gr.Slider(minimum=0, maximum=100, value=0, label="Minimum Popularity"),
        gr.Textbox(label="Release Year (e.g., 2022 or 2015-2020)", placeholder="Optional")
    ],
    outputs=gr.Markdown(label="Song Recommendations"),
    api_name="get_music_recommendations"
)

weather_interface = gr.Interface(
    fn=get_current_weather,
    inputs=gr.Textbox(label="Enter a location (e.g., 'New York' or '44.34,10.99')"),
    outputs=gr.Markdown(label="Current Weather"),
    api_name="get_current_weather"
)


with gr.Blocks(title="DailyPal: Your Daily Discovery Assistant") as demo:
    gr.Markdown("""
    # 🌟 DailyPal  
    Welcome to **DailyPal**, your all-in-one assistant to discover:
    
    - πŸ“š Great books
    - 🍳 Random recipes
    - 🎬 Movies now playing
    - 🎡 Music recommendations
    - ☁️ Current weather updates

    ---
    βš™οΈ **How to use**:  
    Just select a tab and enter your query or location. Get instant suggestions to brighten your day!
    """)

    gr.TabbedInterface(
        [
            book_interface,
            recipe_interface,
            movie_interface,
            music_interface,
            weather_interface,
            # Add more tools here
        ],
        [
            "Book Finder",
            "Random Recipe",
            "Now Playing Movies",
            "Music Recommendations",
            "Current Weather"
        ]
    )
    gr.Markdown("**Example prompt using mcp client like claude desktop**: I am in new york, if the weather is good, then get me a recipe to grill outdoors, if not get me a book on mars to read, and also get me the songs by weekend to listen.")
    gr.Markdown("### πŸ”— [Watch Demo Video](https://drive.google.com/file/d/1DV0Plrhdr7kAWtLRQS91BnVuvRElPbOm/view?usp=drive_link)")


if __name__ == "__main__":
    demo.launch(mcp_server=True)