Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,112 +10,81 @@ from Gradio_UI import GradioUI
|
|
| 10 |
|
| 11 |
|
| 12 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 13 |
-
'''
|
| 14 |
-
@tool
|
| 15 |
-
|
| 16 |
-
#def fetch_movie_from_omdb(genre: str) -> dict:
|
| 17 |
-
"""
|
| 18 |
-
Fetch a movie recommendation from the OMDb API based on a given genre.
|
| 19 |
|
| 20 |
-
Args:
|
| 21 |
-
genre (str): The movie genre to search for (e.g., "Comedy", "Drama", "Sci-Fi").
|
| 22 |
-
|
| 23 |
-
Returns:
|
| 24 |
-
dict: A dictionary containing movie details:
|
| 25 |
-
- title (str): The title of the recommended movie.
|
| 26 |
-
- year (str): The release year of the movie.
|
| 27 |
-
- genre (str): The requested genre.
|
| 28 |
-
- imdb_id (str): The IMDb ID of the movie.
|
| 29 |
-
- error (str, optional): An error message if no results are found.
|
| 30 |
-
"""
|
| 31 |
-
|
| 32 |
-
url = f"http://www.omdbapi.com/?i=tt3896198&apikey=59341a1e"
|
| 33 |
-
|
| 34 |
-
response = requests.get(url)
|
| 35 |
-
if response.status_code != 200:
|
| 36 |
-
return {"error": "Failed to fetch movie data."}
|
| 37 |
-
|
| 38 |
-
data = response.json()
|
| 39 |
-
if "Search" not in data:
|
| 40 |
-
return {"error": "No movies found for this genre."}
|
| 41 |
-
|
| 42 |
-
best_movie = data["Search"][0] # Pick the first result
|
| 43 |
-
return {
|
| 44 |
-
"title": best_movie["Title"],
|
| 45 |
-
"year": best_movie["Year"],
|
| 46 |
-
"genre": genre,
|
| 47 |
-
"imdb_id": best_movie["imdbID"]
|
| 48 |
-
}
|
| 49 |
-
'''
|
| 50 |
@tool
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
Parameters:
|
| 58 |
-
- mood (str): The user's current mood, such as "happy," "sad," "nostalgic," or "adventurous."
|
| 59 |
-
- location (str): The name of the user's location, used to fetch real-time weather data.
|
| 60 |
-
- user_preferences (dict): A dictionary containing user-specific preferences,
|
| 61 |
-
including favorite genres and actors.
|
| 62 |
-
|
| 63 |
-
Returns:
|
| 64 |
-
- dict: A dictionary containing recommended movie details:
|
| 65 |
-
- "title" (str): The name of the recommended movie.
|
| 66 |
-
- "genre" (list): A list of genres the movie falls under.
|
| 67 |
-
- "description" (str): A brief summary of the movie.
|
| 68 |
-
- "link" (str): A link to more information about the movie.
|
| 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 |
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@tool
|
| 15 |
+
def get_movie_recommendation(mood: str, location: str) -> str:
|
| 16 |
+
"""A tool that recommends movies based on user's mood and local weather conditions.
|
| 17 |
+
Args:
|
| 18 |
+
mood: The user's current mood (e.g., 'happy', 'sad', 'excited', 'relaxed', 'nostalgic')
|
| 19 |
+
location: The user's location to check weather (e.g., 'London', 'New York')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"""
|
| 21 |
+
try:
|
| 22 |
+
# Get weather data using wttr.in (free weather API)
|
| 23 |
+
weather_url = f"https://wttr.in/{location}?format=%C"
|
| 24 |
+
weather_response = requests.get(weather_url)
|
| 25 |
+
if weather_response.status_code != 200:
|
| 26 |
+
return f"Error: Could not fetch weather for {location}"
|
| 27 |
+
|
| 28 |
+
weather_condition = weather_response.text.strip().lower()
|
| 29 |
+
|
| 30 |
+
# Define mood and weather based movie genre mappings
|
| 31 |
+
mood_genres = {
|
| 32 |
+
"happy": ["Comedy", "Musical", "Adventure"],
|
| 33 |
+
"sad": ["Drama", "Romance", "Independent"],
|
| 34 |
+
"excited": ["Action", "Thriller", "Sci-Fi"],
|
| 35 |
+
"relaxed": ["Documentary", "Animation", "Family"],
|
| 36 |
+
"nostalgic": ["Classic", "Drama", "Romance"]
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
weather_genres = {
|
| 40 |
+
"clear": ["Adventure", "Comedy", "Action"],
|
| 41 |
+
"rain": ["Drama", "Noir", "Mystery"],
|
| 42 |
+
"clouds": ["Sci-Fi", "Fantasy", "Mystery"],
|
| 43 |
+
"snow": ["Romance", "Family", "Fantasy"],
|
| 44 |
+
"thunderstorm": ["Horror", "Thriller", "Mystery"]
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Get recommended genres based on mood and weather
|
| 48 |
+
selected_genres = mood_genres.get(mood.lower(), ["Drama"])
|
| 49 |
+
for weather_key in weather_genres:
|
| 50 |
+
if weather_key in weather_condition:
|
| 51 |
+
selected_genres.extend(weather_genres[weather_key])
|
| 52 |
+
break
|
| 53 |
+
|
| 54 |
+
# Use OMDB API to fetch a movie recommendation
|
| 55 |
+
omdb_api_key = "59341a1e" # Free tier API key
|
| 56 |
+
primary_genre = selected_genres[0]
|
| 57 |
+
movie_url = f"http://www.omdbapi.com/?apikey={omdb_api_key}&s={primary_genre}&type=movie"
|
| 58 |
+
|
| 59 |
+
movie_response = requests.get(movie_url)
|
| 60 |
+
if movie_response.status_code != 200:
|
| 61 |
+
return f"Error: Could not fetch movie recommendation"
|
| 62 |
+
|
| 63 |
+
movie_data = movie_response.json()
|
| 64 |
+
if movie_data.get("Response") == "False":
|
| 65 |
+
return f"No movies found for your current mood and weather"
|
| 66 |
+
|
| 67 |
+
# Get first movie's details
|
| 68 |
+
first_movie = movie_data["Search"][0]
|
| 69 |
+
detail_url = f"http://www.omdbapi.com/?apikey={omdb_api_key}&i={first_movie['imdbID']}"
|
| 70 |
+
detail_response = requests.get(detail_url)
|
| 71 |
+
movie_details = detail_response.json()
|
| 72 |
+
|
| 73 |
+
# Format the recommendation response
|
| 74 |
+
recommendation = (
|
| 75 |
+
f"Based on your mood ({mood}) and the weather in {location} ({weather_condition}), "
|
| 76 |
+
f"I recommend watching:\n\n"
|
| 77 |
+
f"Title: {movie_details['Title']}\n"
|
| 78 |
+
f"Year: {movie_details['Year']}\n"
|
| 79 |
+
f"Genre: {movie_details['Genre']}\n"
|
| 80 |
+
f"Plot: {movie_details['Plot']}\n"
|
| 81 |
+
f"IMDb Rating: {movie_details['imdbRating']}"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
return recommendation
|
| 85 |
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return f"Error getting movie recommendation: {str(e)}"
|
| 88 |
|
| 89 |
|
| 90 |
|