Alptraum commited on
Commit
3d56fc2
·
verified ·
1 Parent(s): c5cc60e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -102
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
- def get_movie_recommendation(mood: str, location: str, user_preferences: dict) -> dict:
53
- """
54
- Recommend a movie based on the user's mood, current weather at the given location,
55
- and personal preferences.
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
- # Fetch weather data using DuckDuckGo search
72
- search_tool = DuckDuckGoSearchTool()
73
- weather_query = f"current weather in {location}"
74
- weather_results = search_tool.search(weather_query)
75
-
76
- if not weather_results:
77
- return {"error": "Could not fetch weather data."}
78
-
79
- weather_condition = weather_results[0].get("snippet", "").lower()
80
-
81
- # Define mood & weather-based genre mapping
82
- genre_mapping = {
83
- "happy": ["Comedy", "Romance", "Adventure"],
84
- "sad": ["Drama", "Indie", "Biography"],
85
- "nostalgic": ["Classic", "Drama", "Fantasy"],
86
- "adventurous": ["Action", "Sci-Fi", "Thriller"],
87
- }
88
-
89
- weather_mapping = {
90
- "rain": "Drama",
91
- "clear": "Adventure",
92
- "clouds": "Sci-Fi",
93
- "snow": "Fantasy",
94
- "thunderstorm": "Thriller"
95
- }
96
-
97
- # Determine recommended genre
98
- recommended_genres = genre_mapping.get(mood, ["Drama"])
99
- for key in weather_mapping:
100
- if key in weather_condition:
101
- recommended_genres.append(weather_mapping[key])
102
-
103
- # Search for a movie recommendation using DuckDuckGo
104
- search_query = f"best {recommended_genres[0]} movies"
105
- search_tool = DuckDuckGoSearchTool()
106
- results = search_tool.search(search_query)
107
-
108
- if not results:
109
- return {"error": "No suitable movie found."}
110
-
111
- best_movie = results[0]
112
- return {
113
- "title": best_movie.get("title", "Unknown"),
114
- "genre": recommended_genres,
115
- "description": best_movie.get("snippet", "No description available."),
116
- "link": best_movie.get("link", "No link available")
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