Spaces:
Sleeping
Sleeping
Update app.py
Browse filesimports and debugging again
app.py
CHANGED
|
@@ -7,8 +7,13 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
|
|
|
|
|
|
| 12 |
def get_movie_recommendation(mood: str, location: str, user_preferences: dict) -> dict:
|
| 13 |
"""
|
| 14 |
Recommend a movie based on the user's mood, current weather at the given location,
|
|
@@ -25,14 +30,12 @@ def get_movie_recommendation(mood: str, location: str, user_preferences: dict) -
|
|
| 25 |
|
| 26 |
# Fetch weather data from a free API (wttr.in)
|
| 27 |
weather_url = f"https://wttr.in/{location}?format=%C"
|
| 28 |
-
|
| 29 |
response = requests.get(weather_url)
|
|
|
|
| 30 |
if response.status_code != 200 or "Unknown location" in response.text:
|
| 31 |
return {"error": f"Could not fetch weather data for {location}."}
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
weather_condition = response.text.lower()
|
| 36 |
|
| 37 |
# Define mood & weather-based genre mapping
|
| 38 |
genre_mapping = {
|
|
@@ -50,29 +53,36 @@ def get_movie_recommendation(mood: str, location: str, user_preferences: dict) -
|
|
| 50 |
"thunderstorm": "Thriller"
|
| 51 |
}
|
| 52 |
|
| 53 |
-
# Determine recommended
|
| 54 |
recommended_genres = genre_mapping.get(mood, ["Drama"])
|
| 55 |
for key in weather_mapping:
|
| 56 |
if key in weather_condition:
|
| 57 |
recommended_genres.append(weather_mapping[key])
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
import requests
|
| 11 |
+
from imdb import Cinemagoer
|
| 12 |
+
|
| 13 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 14 |
@tool
|
| 15 |
+
|
| 16 |
+
|
| 17 |
def get_movie_recommendation(mood: str, location: str, user_preferences: dict) -> dict:
|
| 18 |
"""
|
| 19 |
Recommend a movie based on the user's mood, current weather at the given location,
|
|
|
|
| 30 |
|
| 31 |
# Fetch weather data from a free API (wttr.in)
|
| 32 |
weather_url = f"https://wttr.in/{location}?format=%C"
|
|
|
|
| 33 |
response = requests.get(weather_url)
|
| 34 |
+
|
| 35 |
if response.status_code != 200 or "Unknown location" in response.text:
|
| 36 |
return {"error": f"Could not fetch weather data for {location}."}
|
|
|
|
| 37 |
|
| 38 |
+
weather_condition = response.text.strip().lower()
|
|
|
|
| 39 |
|
| 40 |
# Define mood & weather-based genre mapping
|
| 41 |
genre_mapping = {
|
|
|
|
| 53 |
"thunderstorm": "Thriller"
|
| 54 |
}
|
| 55 |
|
| 56 |
+
# Determine recommended genres
|
| 57 |
recommended_genres = genre_mapping.get(mood, ["Drama"])
|
| 58 |
for key in weather_mapping:
|
| 59 |
if key in weather_condition:
|
| 60 |
recommended_genres.append(weather_mapping[key])
|
| 61 |
|
| 62 |
+
# Initialize IMDbPY
|
| 63 |
+
ia = Cinemagoer()
|
| 64 |
+
|
| 65 |
+
# Fetch top movies from IMDb by genre
|
| 66 |
+
genre_movies = []
|
| 67 |
+
for genre in recommended_genres:
|
| 68 |
+
try:
|
| 69 |
+
top_movies = ia.get_top50_movies_by_genre(genre)
|
| 70 |
+
genre_movies.extend(top_movies)
|
| 71 |
+
except Exception:
|
| 72 |
+
continue # Skip if genre is unavailable
|
| 73 |
+
|
| 74 |
+
if not genre_movies:
|
| 75 |
+
return {"error": "No suitable movie found."}
|
| 76 |
+
|
| 77 |
+
best_movie = genre_movies[0] # Pick the first movie
|
| 78 |
+
|
| 79 |
+
return {
|
| 80 |
+
"title": best_movie.get("title", "Unknown"),
|
| 81 |
+
"genre": recommended_genres,
|
| 82 |
+
"description": best_movie.get("plot outline", "No description available."),
|
| 83 |
+
"rating": best_movie.get("rating", "No rating available."),
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
|
| 87 |
|
| 88 |
|