Ragai1 commited on
Commit
6e7fdba
·
verified ·
1 Parent(s): 2cd29b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -34
app.py CHANGED
@@ -9,45 +9,53 @@ from Gradio_UI import GradioUI
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
 
12
- OMDB_API_KEY = "OMDB_API_KEY"
13
-
14
  @tool
15
- def get_movies_by_mood(mood: str) -> str:
16
  """
17
- Suggests up to 5 movies based on user's mood using OMDb API.
18
-
19
  Args:
20
- mood: Current emotional status or feeling of a user (e.g., 'sad', 'lonely', 'romantic').
 
 
 
21
  """
22
- mood_keywords = {
23
- "happy": ["comedy", "feel good"],
24
- "sad": ["drama", "emotional"],
25
- "romantic": ["romance", "love"],
26
- "inspired": ["biography", "true story"],
27
- "bored": ["action", "thriller"],
28
- "stressed": ["family", "light"],
29
- "excited": ["adventure", "epic"],
30
- "lonely": ["friendship", "self discovery"]
31
- }
32
-
33
- fallback_keywords = mood_keywords.get(
34
- next((k for k in mood_keywords if k in mood.lower()), "sad"), ["drama"]
35
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- for keyword in fallback_keywords:
38
- res = requests.get(f"http://www.omdbapi.com/?s={keyword}&apikey={OMDB_API_KEY}&type=movie").json()
39
- if "Search" in res:
40
- movies = []
41
- for movie in res["Search"][:5]:
42
- info = requests.get(f"http://www.omdbapi.com/?i={movie['imdbID']}&apikey={OMDB_API_KEY}").json()
43
- title = info.get("Title", "Unknown")
44
- year = info.get("Year", "N/A")
45
- plot = info.get("Plot", "No plot available.")
46
- movies.append(f"{title} ({year}): {plot}")
47
- return "\n\n".join(movies)
48
-
49
- return "Sorry, I couldn't find any matching movies for that mood right now."
50
-
51
 
52
  @tool
53
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -86,7 +94,7 @@ with open("prompts.yaml", 'r') as stream:
86
 
87
  agent = CodeAgent(
88
  model=model,
89
- tools=[final_answer, get_current_time_in_timezone, get_movies_by_mood], ## add your tools here (don't remove final answer)
90
  max_steps=6,
91
  verbosity_level=1,
92
  grammar=None,
 
9
 
10
  # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
 
 
 
12
  @tool
13
+ def suggest_top_5_movies_by_mood(mood: str) -> list:
14
  """
15
+ Suggest the top 5 movies based on the user's mood using DuckDuckGoSearchTool.
16
+
17
  Args:
18
+ mood: The user's current mood (e.g., "happy", "sad", "romantic", "bored").
19
+
20
+ Returns:
21
+ list: A list of dictionaries, each containing 'title', 'description', and 'imdb_link'.
22
  """
23
+ query = f"top 10 movies to watch when feeling {mood} site:imdb.com"
24
+
25
+ temp_agent = CodeAgent(
26
+ tools=[DuckDuckGoSearchTool()],
27
+ model=model
 
 
 
 
 
 
 
 
28
  )
29
+
30
+ search_results = temp_agent.run(query)
31
+
32
+ # Use regex to extract IMDb links and nearby titles/descriptions
33
+ import re
34
+ pattern = r'https?://www\.imdb\.com/title/tt\d+[^ \n]*'
35
+ imdb_links = re.findall(pattern, search_results)
36
+
37
+ lines = [line.strip() for line in search_results.split('\n') if line.strip()]
38
+ movies = []
39
+
40
+ for i, link in enumerate(imdb_links[:5]):
41
+ # Try to find the line before or after the link as description
42
+ context = ""
43
+ for j, line in enumerate(lines):
44
+ if link in line:
45
+ # Look backward or forward for a potential title/description
46
+ context = lines[j-1] if j > 0 else lines[j+1]
47
+ break
48
+
49
+ title_match = re.search(r'“([^”]+)”', context) or re.search(r'"([^"]+)"', context)
50
+ title = title_match.group(1) if title_match else context[:60]
51
+
52
+ movies.append({
53
+ "title": title.strip(),
54
+ "description": context.strip(),
55
+ "imdb_link": link
56
+ })
57
 
58
+ return movies
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  @tool
61
  def get_current_time_in_timezone(timezone: str) -> str:
 
94
 
95
  agent = CodeAgent(
96
  model=model,
97
+ tools=[final_answer, get_current_time_in_timezone, suggest_top_5_movies_by_mood, DuckDuckGoSearchTool], ## add your tools here (don't remove final answer)
98
  max_steps=6,
99
  verbosity_level=1,
100
  grammar=None,