kevinwang676 commited on
Commit
0d0493f
·
verified ·
1 Parent(s): d296e89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +755 -153
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import asyncio
3
- import json, os
4
  import functools
5
  import numpy as np
6
  from sklearn.metrics.pairwise import cosine_similarity
@@ -10,84 +10,550 @@ from agents import Agent, Runner, function_tool
10
  # Sample movie knowledge base
11
  movie_knowledge_base = [
12
  {
13
- "title": "The Shawshank Redemption",
14
- "description": "Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.",
15
- "genre": ["Drama"],
16
- "director": "Frank Darabont",
17
- "year": 1994,
18
- "box_office": 28341469,
19
- "awards": ["Oscar nominations for Best Picture", "Best Actor", "Best Screenplay"],
20
- "actors": ["Tim Robbins", "Morgan Freeman"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  },
22
  {
23
- "title": "The Godfather",
24
- "description": "The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.",
25
- "genre": ["Crime", "Drama"],
26
- "director": "Francis Ford Coppola",
27
- "year": 1972,
28
- "box_office": 134966411,
29
- "awards": ["Oscar for Best Picture", "Best Actor", "Best Adapted Screenplay"],
30
- "actors": ["Marlon Brando", "Al Pacino", "James Caan"]
 
 
31
  },
32
  {
33
- "title": "Pulp Fiction",
34
- "description": "The lives of two mob hitmen, a boxer, a gangster and his wife, and a pair of diner bandits intertwine in four tales of violence and redemption.",
35
- "genre": ["Crime", "Drama"],
36
- "director": "Quentin Tarantino",
 
37
  "year": 1994,
38
- "box_office": 107928762,
39
- "awards": ["Oscar for Best Original Screenplay", "Palme d'Or at Cannes"],
40
- "actors": ["John Travolta", "Uma Thurman", "Samuel L. Jackson"]
 
41
  },
42
  {
43
- "title": "The Dark Knight",
44
- "description": "When the menace known as the Joker wreaks havoc and chaos on the people of Gotham, Batman must accept one of the greatest psychological and physical tests of his ability to fight injustice.",
45
- "genre": ["Action", "Crime", "Drama"],
46
- "director": "Christopher Nolan",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  "year": 2008,
48
- "box_office": 1004558444,
49
- "awards": ["Oscar for Best Supporting Actor"],
50
- "actors": ["Christian Bale", "Heath Ledger", "Aaron Eckhart"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  },
52
  {
53
- "title": "Inception",
54
- "description": "A thief who steals corporate secrets through the use of dream-sharing technology is given the inverse task of planting an idea into the mind of a C.E.O.",
55
  "genre": ["Action", "Adventure", "Sci-Fi"],
56
- "director": "Christopher Nolan",
57
- "year": 2010,
58
- "box_office": 836836967,
59
- "awards": ["Oscar for Best Cinematography", "Best Visual Effects"],
60
- "actors": ["Leonardo DiCaprio", "Joseph Gordon-Levitt", "Ellen Page"]
 
 
61
  },
62
  {
63
  "title": "The Matrix",
64
  "description": "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.",
65
  "genre": ["Action", "Sci-Fi"],
66
- "director": "The Wachowskis",
 
67
  "year": 1999,
68
- "box_office": 463517383,
69
- "awards": ["Oscar for Best Visual Effects", "Best Film Editing"],
70
- "actors": ["Keanu Reeves", "Laurence Fishburne", "Carrie-Anne Moss"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  },
72
  {
73
- "title": "Parasite",
74
- "description": "Greed and class discrimination threaten the newly formed symbiotic relationship between the wealthy Park family and the destitute Kim clan.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  "genre": ["Drama", "Thriller"],
76
- "director": "Bong Joon Ho",
 
77
  "year": 2019,
78
- "box_office": 258773429,
79
- "awards": ["Oscar for Best Picture", "Best Director", "Best Original Screenplay", "Best International Feature Film"],
80
- "actors": ["Song Kang-ho", "Lee Sun-kyun", "Cho Yeo-jeong"]
81
- },
82
- {
83
- "title": "Ex Machina",
84
- "description": "A young programmer is selected to participate in a ground-breaking experiment in synthetic intelligence by evaluating the human qualities of a highly advanced humanoid A.I.",
85
- "genre": ["Sci-Fi", "Drama", "Thriller"],
86
- "director": "Alex Garland",
87
- "year": 2014,
88
- "box_office": 36869414,
89
- "awards": ["Oscar for Best Visual Effects"],
90
- "actors": ["Domhnall Gleeson", "Alicia Vikander", "Oscar Isaac"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  }
92
  ]
93
 
@@ -180,12 +646,23 @@ class AgentConversationLogger:
180
 
181
  def get_log_text(self):
182
  """Get all logs as a formatted string"""
183
- return "\n".join(self.log_output)
 
 
 
 
 
 
 
 
 
 
 
184
 
185
  # Create a global logger
186
  logger = AgentConversationLogger()
187
 
188
- # Create the MovieKnowledgeBase class
189
  class MovieKnowledgeBase:
190
  def __init__(self, movies):
191
  self.movies = movies
@@ -194,18 +671,18 @@ class MovieKnowledgeBase:
194
  # Precompute embeddings for all movie descriptions
195
  self.descriptions = [movie["description"] for movie in movies]
196
  self.embeddings = self.model.encode(self.descriptions)
197
-
198
  def find_similar_movies(self, description, top_n=3):
199
  """Find the top N similar movies to the given description using embeddings."""
200
  # Encode the query description
201
  query_embedding = self.model.encode([description])[0]
202
-
203
  # Calculate cosine similarity between query and all movies
204
  similarities = cosine_similarity([query_embedding], self.embeddings)[0]
205
-
206
  # Get indices of top N similar movies
207
  top_indices = np.argsort(similarities)[-top_n:][::-1]
208
-
209
  # Create the result with movies and similarity scores
210
  similar_movies = []
211
  for idx in top_indices:
@@ -213,35 +690,118 @@ class MovieKnowledgeBase:
213
  "movie": self.movies[idx],
214
  "similarity_score": float(similarities[idx])
215
  })
216
-
217
  return similar_movies
218
 
219
  # Initialize the knowledge base with embeddings
220
  movie_kb = MovieKnowledgeBase(movie_knowledge_base)
221
 
222
- # Function tools with logging
223
- def log_function_tool(func):
224
- """Decorator to log function tool calls"""
225
- @functools.wraps(func)
226
- def wrapper(*args, **kwargs):
227
- # Get function inputs
228
- func_name = func.__name__
229
- func_inputs = kwargs if kwargs else args[0] if args else {}
230
-
231
- # Run the function
232
- result = func(*args, **kwargs)
233
-
234
- # Log function call
235
- logger.log_function_call(func_name, func_inputs, result)
236
-
237
- return result
238
- return wrapper
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  @function_tool
241
- @log_function_tool
242
  def get_similar_movies(movie_description: str):
243
  """Find the top 3 movies most similar to the given movie description."""
244
- similar_movies = movie_kb.find_similar_movies(movie_description, top_n=3)
245
  # Convert to a more readable format for the agents
246
  result = []
247
  for movie_info in similar_movies:
@@ -254,70 +814,61 @@ def get_similar_movies(movie_description: str):
254
  "year": movie["year"],
255
  "box_office": movie["box_office"],
256
  "awards": movie["awards"],
 
 
257
  "similarity_score": movie_info["similarity_score"]
258
  })
259
  return result
260
 
261
  @function_tool
262
- @log_function_tool
263
  def get_box_office_prediction(movie_description: str):
264
  """Predict the box office revenue for a movie based on its description."""
265
- similar_movies = movie_kb.find_similar_movies(movie_description, top_n=3)
266
-
267
- # Calculate weighted average of box office revenues
268
- total_weight = sum(movie_info["similarity_score"] for movie_info in similar_movies)
269
- weighted_sum = sum(movie_info["similarity_score"] * movie_info["movie"]["box_office"] for movie_info in similar_movies)
270
-
271
- if total_weight > 0:
272
- predicted_revenue = weighted_sum / total_weight
 
273
  else:
274
- # Fallback to average of all movies
275
- predicted_revenue = sum(m["box_office"] for m in movie_knowledge_base) / len(movie_knowledge_base)
276
-
 
 
 
 
277
  # Convert to a more readable format
278
  similar_movie_info = []
279
  for movie_info in similar_movies:
280
  movie = movie_info["movie"]
281
  similar_movie_info.append({
282
  "title": movie["title"],
 
283
  "box_office": movie["box_office"],
284
  "similarity_score": movie_info["similarity_score"]
285
  })
286
-
287
  return {
288
- "predicted_revenue": round(predicted_revenue, 2),
 
 
 
 
 
289
  "similar_movies": similar_movie_info
290
  }
291
 
292
  @function_tool
293
- @log_function_tool
294
  def get_award_predictions(movie_description: str):
295
  """Predict potential awards for a movie based on its description."""
296
- similar_movies = movie_kb.find_similar_movies(movie_description, top_n=3)
297
-
298
- # Count awards in similar movies and recommend the most common ones
299
- award_counts = {}
300
-
301
- for movie_info in similar_movies:
302
- similarity = movie_info["similarity_score"]
303
- awards = movie_info["movie"]["awards"]
304
-
305
- for award in awards:
306
- if award in award_counts:
307
- award_counts[award] += similarity
308
- else:
309
- award_counts[award] = similarity
310
-
311
- # Sort awards by their weighted counts
312
- sorted_awards = sorted(award_counts.items(), key=lambda x: x[1], reverse=True)
313
-
314
- # Return top 3 potential awards
315
- potential_awards = [award for award, count in sorted_awards[:3]]
316
-
317
- # If no similar movie has awards, return a message
318
- if not potential_awards:
319
- potential_awards = ["No award predictions available based on similar movies"]
320
-
321
  # Convert to a more readable format
322
  similar_movie_info = []
323
  for movie_info in similar_movies:
@@ -327,21 +878,20 @@ def get_award_predictions(movie_description: str):
327
  "awards": movie["awards"],
328
  "similarity_score": movie_info["similarity_score"]
329
  })
330
-
331
  return {
332
  "potential_awards": potential_awards,
333
  "similar_movies": similar_movie_info
334
  }
335
 
336
- # Define the specialized agents
337
  similarity_agent = Agent(
338
  name="Movie Similarity Expert",
339
  instructions="""
340
  You are an expert in movie analysis and recommendations.
341
  Your task is to analyze the description of a new movie and find similar movies.
342
  Provide detailed recommendations with justifications for why these movies are similar.
343
- Consider plot elements, themes, genre, style, and mood in your analysis.
344
- Explain what makes each recommended movie similar to the query movie.
345
  """,
346
  tools=[get_similar_movies]
347
  )
@@ -350,10 +900,10 @@ revenue_agent = Agent(
350
  name="Box Office Analyst",
351
  instructions="""
352
  You are an expert in predicting movie box office performance.
353
- Your task is to predict potential box office revenue based on similar movies.
354
- Explain your prediction with reference to similar movies' performance.
355
- Consider genre popularity, comparable films' performance, and market trends.
356
- Provide a range of potential outcomes with justifications.
357
  """,
358
  tools=[get_box_office_prediction]
359
  )
@@ -364,8 +914,7 @@ award_agent = Agent(
364
  You are an expert in predicting movie awards and critical reception.
365
  Your task is to predict potential awards a movie might receive based on similar movies.
366
  Explain your predictions by referencing similar award-winning films.
367
- Consider elements like direction, acting, screenplay, and cinematography.
368
- Provide specific award categories the movie might compete in.
369
  """,
370
  tools=[get_award_predictions]
371
  )
@@ -374,21 +923,16 @@ award_agent = Agent(
374
  orchestrator_agent = Agent(
375
  name="Movie Analysis Orchestrator",
376
  instructions="""
377
- You are the central coordinator for movie analysis tasks.
378
-
379
  Your responsibilities include:
380
  1. Properly understanding the user's movie analysis request
381
- 2. First calling the get_similar_movies function to find similar movies to the query
382
- 3. Then delegating specific analysis tasks to the appropriate specialized agents based on the user's request:
383
  - Movie Similarity Expert for recommendation tasks
384
  - Box Office Analyst for revenue prediction tasks
385
  - Award Prediction Specialist for award prediction tasks
386
- 4. If the user doesn't specify what analysis they want, provide all three analyses
387
- 5. Synthesizing all information received into a coherent, comprehensive response
388
-
389
- Always provide a comprehensive summary of findings from all involved agents.
390
  """,
391
- tools=[get_similar_movies],
392
  handoffs=[similarity_agent, revenue_agent, award_agent]
393
  )
394
 
@@ -421,6 +965,11 @@ async def run_agent_analysis(query, analysis_type="all"):
421
  """Run the multi-agent system with the given query and analysis type."""
422
  logger.clear_logs()
423
 
 
 
 
 
 
424
  # Modify the query based on the analysis type
425
  if analysis_type == "similar":
426
  enhanced_query = f"{query} Please recommend similar movies."
@@ -432,13 +981,35 @@ async def run_agent_analysis(query, analysis_type="all"):
432
  enhanced_query = f"{query} Please provide a complete analysis including similar movies, box office potential, and award possibilities."
433
 
434
  # Run the orchestrator with the query
435
- result = await Runner.run(orchestrator_agent, input=enhanced_query)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
 
437
- # Return both the final output and the log
438
- return {
439
- "result": result.final_output,
440
- "log": logger.get_log_text()
441
- }
 
 
 
 
442
 
443
  # Gradio interface function (synchronous wrapper for the async function)
444
  def process_query(description, analysis_type):
@@ -447,7 +1018,22 @@ def process_query(description, analysis_type):
447
  asyncio.set_event_loop(loop)
448
  try:
449
  result = loop.run_until_complete(run_agent_analysis(description, analysis_type))
450
- return result["log"], result["result"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
  finally:
452
  loop.close()
453
 
@@ -460,7 +1046,14 @@ example_descriptions = [
460
  ]
461
 
462
  # Create the Gradio interface
463
- with gr.Blocks(title="Movie Analysis Multi-Agent System") as demo:
 
 
 
 
 
 
 
464
  gr.Markdown("# Movie Analysis Multi-Agent System")
465
  gr.Markdown("""
466
  This demo uses a multi-agent system to analyze movie descriptions. Enter a description of your movie idea,
@@ -476,7 +1069,7 @@ with gr.Blocks(title="Movie Analysis Multi-Agent System") as demo:
476
  )
477
 
478
  analysis_type = gr.Radio(
479
- ["similar", "box_office", "awards"],
480
  label="Analysis Type",
481
  value="all"
482
  )
@@ -488,7 +1081,16 @@ with gr.Blocks(title="Movie Analysis Multi-Agent System") as demo:
488
  with gr.TabItem("Analysis Result"):
489
  result_output = gr.Markdown(label="Analysis")
490
  with gr.TabItem("Agent Conversation Log"):
491
- conversation_output = gr.Textbox(label="Conversation Log", lines=20)
 
 
 
 
 
 
 
 
 
492
 
493
  submit_btn.click(
494
  fn=process_query,
 
1
  import gradio as gr
2
  import asyncio
3
+ import json
4
  import functools
5
  import numpy as np
6
  from sklearn.metrics.pairwise import cosine_similarity
 
10
  # Sample movie knowledge base
11
  movie_knowledge_base = [
12
  {
13
+ "title": "A Goofy Movie",
14
+ "description": "Max Goof, a teenager, embarks on a chaotic road trip with his bumbling father, Goofy, to bond and attend a concert, navigating teenage rebellion and family love.",
15
+ "genre": ["Animation", "Children", "Comedy", "Romance"],
16
+ "director": "Kevin Lima",
17
+ "actors": ["Bill Farmer", "Jason Marsden", "Jim Cummings"],
18
+ "year": 1995,
19
+ "box_office": 35348597,
20
+ "budget": 18000000,
21
+ "awards": [],
22
+ "rating": 4.5
23
+ },
24
+ {
25
+ "title": "Gumby: The Movie",
26
+ "description": "Gumby, a claymation character, and his friends must stop the evil Blockheads from replacing their town with robots during a benefit concert.",
27
+ "genre": ["Animation", "Children"],
28
+ "director": "Art Clokey",
29
+ "actors": ["Dal McKennon", "Art Clokey", "Gloria Clokey"],
30
+ "year": 1995,
31
+ "box_office": 57076,
32
+ "budget": 2800000,
33
+ "awards": [],
34
+ "rating": 3.5
35
  },
36
  {
37
+ "title": "The Swan Princess",
38
+ "description": "A princess cursed to become a swan must rely on true love to break the spell, facing an evil sorcerer in this animated fairy tale.",
39
+ "genre": ["Animation", "Children"],
40
+ "director": "Richard Rich",
41
+ "actors": ["Michelle Nicastro", "Howard McGillin", "Jack Palance"],
42
+ "year": 1994,
43
+ "box_office": 9771650,
44
+ "budget": 45000000,
45
+ "awards": [],
46
+ "rating": 3.0
47
  },
48
  {
49
+ "title": "The Lion King",
50
+ "description": "Simba, a young lion prince, flees after his father’s death but returns to reclaim his throne from his treacherous uncle Scar in this animated epic.",
51
+ "genre": ["Adventure", "Animation", "Children", "Drama", "Musical", "IMAX"],
52
+ "director": ["Roger Allers", "Rob Minkoff"],
53
+ "actors": ["Matthew Broderick", "Jeremy Irons", "James Earl Jones"],
54
  "year": 1994,
55
+ "box_office": 968483777,
56
+ "budget": 45000000,
57
+ "awards": ["Oscar Best Original Score", "Oscar Best Original Song", "Golden Globe Best Motion Picture - Musical or Comedy"],
58
+ "rating": 4.5
59
  },
60
  {
61
+ "title": "The Secret Adventures of Tom Thumb",
62
+ "description": "A tiny boy, born to normal parents, is kidnapped and navigates a dark, surreal world of science and survival in this stop-motion tale.",
63
+ "genre": ["Adventure", "Animation"],
64
+ "director": "Dave Borthwick",
65
+ "actors": ["Nick Upton", "Deborah Collard", "Frank Passingham"],
66
+ "year": 1993,
67
+ "box_office": 0,
68
+ "budget": 0,
69
+ "awards": [],
70
+ "rating": 2.5
71
+ },
72
+ {
73
+ "title": "This So-Called Disaster",
74
+ "description": "A documentary capturing actor Sam Shepard as he directs a play, blending rehearsals with personal reflections on art and life.",
75
+ "genre": ["Documentary", "Disaster"],
76
+ "director": "Michael Almereyda",
77
+ "actors": ["Sam Shepard", "Nick Nolte", "Sean Penn"],
78
+ "year": 2003,
79
+ "box_office": 46658,
80
+ "budget": 0,
81
+ "awards": [],
82
+ "rating": 3.0
83
+ },
84
+ {
85
+ "title": "Love and Other Disasters",
86
+ "description": "A fashion magazine assistant in London juggles romance, friendship, and chaos while trying to matchmake those around her.",
87
+ "genre": ["Comedy", "Romance", "Disaster"],
88
+ "director": "Alek Keshishian",
89
+ "actors": ["Brittany Murphy", "Matthew Rhys", "Santiago Cabrera"],
90
+ "year": 2006,
91
+ "box_office": 6743917,
92
+ "budget": 10000000,
93
+ "awards": [],
94
+ "rating": 3.0
95
+ },
96
+ {
97
+ "title": "Disaster Movie",
98
+ "description": "A group of friends faces absurd, over-the-top disasters in a parody of blockbuster films, filled with pop culture references.",
99
+ "genre": ["Comedy", "Disaster"],
100
+ "director": "Aaron Seltzer",
101
+ "actors": ["Matt Lanter", "Vanessa Minnillo", "Kim Kardashian"],
102
  "year": 2008,
103
+ "box_office": 34816824,
104
+ "budget": 20000000,
105
+ "awards": [],
106
+ "rating": 2.0
107
+ },
108
+ {
109
+ "title": "It's a Disaster",
110
+ "description": "A brunch among friends turns chaotic when they learn of an impending apocalyptic attack, testing their relationships and humor.",
111
+ "genre": ["Comedy", "Drama", "Disaster"],
112
+ "director": "Todd Berger",
113
+ "actors": ["Rachel Boston", "Kevin M. Brennan", "David Cross"],
114
+ "year": 2012,
115
+ "box_office": 60426,
116
+ "budget": 500000,
117
+ "awards": [],
118
+ "rating": 4.0
119
+ },
120
+ {
121
+ "title": "The Challenger Disaster",
122
+ "description": "A dramatization of the investigation into the 1986 Challenger space shuttle explosion, focusing on physicist Richard Feynman’s role.",
123
+ "genre": ["Drama", "Disaster"],
124
+ "director": "James Hawes", # Corrected from "Richard Feynman" (Feynman was a physicist, not the director)
125
+ "actors": ["William Hurt", "Bruce Greenwood", "Joanne Whalley"],
126
+ "year": 2013,
127
+ "box_office": 0,
128
+ "budget": 0,
129
+ "awards": [],
130
+ "rating": 3.0
131
+ },
132
+ {
133
+ "title": "Some Girl(s)",
134
+ "description": "A writer travels to confront past girlfriends before his wedding, unraveling emotional truths in this adaptation of Neil LaBute’s play.",
135
+ "genre": ["Comedy", "Drama"],
136
+ "director": "Daisy von Scherler Mayer", # Corrected from "Michael Hoffman"
137
+ "actors": ["Adam Brody", "Kristen Bell", "Zoe Kazan"],
138
+ "year": 2013,
139
+ "box_office": 0,
140
+ "budget": 0,
141
+ "awards": [],
142
+ "rating": 3.5
143
+ },
144
+ {
145
+ "title": "The Fourth Angel",
146
+ "description": "A grieving father becomes a vigilante after his family is killed in a terrorist hijacking, seeking justice outside the law.",
147
+ "genre": ["Action", "Drama", "Thriller"],
148
+ "director": "John Irvin",
149
+ "actors": ["Jeremy Irons", "Forest Whitaker", "Jason Priestley"],
150
+ "year": 2001,
151
+ "box_office": 0,
152
+ "budget": 0,
153
+ "awards": [],
154
+ "rating": 3.5
155
+ },
156
+ {
157
+ "title": "The Rat Race",
158
+ "description": "A struggling musician and a dancer form an unlikely partnership to survive the harsh realities of New York City in this poignant comedy-drama.",
159
+ "genre": ["Comedy", "Drama", "Romance"],
160
+ "director": "Robert Mulligan",
161
+ "actors": ["Tony Curtis", "Debbie Reynolds", "Jack Oakie"],
162
+ "year": 1960,
163
+ "box_office": 0,
164
+ "budget": 0,
165
+ "awards": [],
166
+ "rating": 3.5
167
+ },
168
+ {
169
+ "title": "Straight from the Heart",
170
+ "description": "A photographer from New York falls for a Wyoming rancher in this heartfelt romance, bridging two different worlds.",
171
+ "genre": ["Action", "Adventure", "Drama", "Romance", "Western"],
172
+ "director": "David S. Cass Sr.",
173
+ "actors": ["Teri Polo", "Andrew McCarthy", "Patricia Kalember"],
174
+ "year": 2003,
175
+ "box_office": 0,
176
+ "budget": 0,
177
+ "awards": [],
178
+ "rating": 4.0
179
+ },
180
+ {
181
+ "title": "Lone Survivor",
182
+ "description": "A Navy SEAL team faces overwhelming odds in a failed mission in Afghanistan, fighting for survival in this intense war drama.",
183
+ "genre": ["Action", "Drama", "Thriller", "War"],
184
+ "director": "Peter Berg",
185
+ "actors": ["Mark Wahlberg", "Taylor Kitsch", "Emile Hirsch"],
186
+ "year": 2013,
187
+ "box_office": 154802912,
188
+ "budget": 40000000,
189
+ "awards": [],
190
+ "rating": 3.0
191
+ },
192
+ # Sci-Fi Movies
193
+ {
194
+ "title": "2001: A Space Odyssey",
195
+ "description": "A space voyage to Jupiter with the sentient computer HAL after the discovery of a mysterious black monolith affecting human evolution.",
196
+ "genre": ["Adventure", "Sci-Fi"],
197
+ "director": "Stanley Kubrick",
198
+ "actors": ["Keir Dullea", "Gary Lockwood", "William Sylvester"],
199
+ "year": 1968,
200
+ "box_office": 146000000,
201
+ "budget": 10500000,
202
+ "awards": ["Oscar Best Visual Effects"],
203
+ "rating": 4.2
204
  },
205
  {
206
+ "title": "Star Wars: Episode IV - A New Hope",
207
+ "description": "Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a Wookiee, and two droids to save the galaxy from the Empire's world-destroying battle station.",
208
  "genre": ["Action", "Adventure", "Sci-Fi"],
209
+ "director": "George Lucas",
210
+ "actors": ["Mark Hamill", "Harrison Ford", "Carrie Fisher"],
211
+ "year": 1977,
212
+ "box_office": 775000000,
213
+ "budget": 11000000,
214
+ "awards": ["Oscar Best Original Score", "Oscar Best Costume Design", "Oscar Best Film Editing", "Oscar Best Sound", "Oscar Best Visual Effects", "Oscar Best Art Direction"],
215
+ "rating": 4.3
216
  },
217
  {
218
  "title": "The Matrix",
219
  "description": "A computer hacker learns from mysterious rebels about the true nature of his reality and his role in the war against its controllers.",
220
  "genre": ["Action", "Sci-Fi"],
221
+ "director": "Lana Wachowski, Lilly Wachowski",
222
+ "actors": ["Keanu Reeves", "Laurence Fishburne", "Carrie-Anne Moss"],
223
  "year": 1999,
224
+ "box_office": 467000000,
225
+ "budget": 63000000,
226
+ "awards": ["Oscar Best Visual Effects", "Oscar Best Film Editing", "Oscar Best Sound", "Oscar Best Sound Effects Editing"],
227
+ "rating": 4.4
228
+ },
229
+ {
230
+ "title": "Gravity",
231
+ "description": "Two astronauts work together to survive after an accident leaves them stranded in space.",
232
+ "genre": ["Adventure", "Drama", "Sci-Fi"],
233
+ "director": "Alfonso Cuarón",
234
+ "actors": ["Sandra Bullock", "George Clooney", "Ed Harris"],
235
+ "year": 2013,
236
+ "box_office": 723000000,
237
+ "budget": 100000000,
238
+ "awards": ["Oscar Best Director", "Oscar Best Cinematography", "Oscar Best Visual Effects", "Oscar Best Film Editing", "Oscar Best Original Score", "Oscar Best Sound Editing", "Oscar Best Sound Mixing"],
239
+ "rating": 3.9
240
+ },
241
+ {
242
+ "title": "Arrival",
243
+ "description": "A linguist works with the military to communicate with alien lifeforms after twelve mysterious spacecraft appear around the world.",
244
+ "genre": ["Drama", "Mystery", "Sci-Fi"],
245
+ "director": "Denis Villeneuve",
246
+ "actors": ["Amy Adams", "Jeremy Renner", "Forest Whitaker"],
247
+ "year": 2016,
248
+ "box_office": 203000000,
249
+ "budget": 47000000,
250
+ "awards": ["Oscar Best Sound Editing"],
251
+ "rating": 4.0
252
+ },
253
+ # Romance Movies
254
+ {
255
+ "title": "Titanic",
256
+ "description": "A seventeen-year-old aristocrat falls in love with a kind but poor artist aboard the luxurious, ill-fated R.M.S. Titanic.",
257
+ "genre": ["Drama", "Romance"],
258
+ "director": "James Cameron",
259
+ "actors": ["Leonardo DiCaprio", "Kate Winslet", "Billy Zane"],
260
+ "year": 1997,
261
+ "box_office": 2195000000,
262
+ "budget": 200000000,
263
+ "awards": ["Oscar Best Picture", "Oscar Best Director", "Oscar Best Cinematography", "Oscar Best Costume Design", "Oscar Best Film Editing", "Oscar Best Original Score", "Oscar Best Original Song", "Oscar Best Sound", "Oscar Best Sound Effects Editing", "Oscar Best Visual Effects", "Oscar Best Art Direction"],
264
+ "rating": 3.9
265
+ },
266
+ {
267
+ "title": "Casablanca",
268
+ "description": "A cynical expatriate American cafe owner struggles to decide whether or not to help his former lover and her fugitive husband escape the Nazis in French Morocco.",
269
+ "genre": ["Drama", "Romance", "War"],
270
+ "director": "Michael Curtiz",
271
+ "actors": ["Humphrey Bogart", "Ingrid Bergman", "Paul Henreid"],
272
+ "year": 1942,
273
+ "box_office": 10000000,
274
+ "budget": 950000,
275
+ "awards": ["Oscar Best Picture", "Oscar Best Director", "Oscar Best Adapted Screenplay"],
276
+ "rating": 4.3
277
+ },
278
+ {
279
+ "title": "Romeo and Juliet",
280
+ "description": "Shakespeare's famous play is updated to the hip modern suburb of Verona still retaining its original dialogue.",
281
+ "genre": ["Drama", "Romance"],
282
+ "director": "Franco Zeffirelli",
283
+ "actors": ["Leonard Whiting", "Olivia Hussey", "John McEnery"],
284
+ "year": 1968,
285
+ "box_office": 38000000,
286
+ "budget": 850000,
287
+ "awards": ["Oscar Best Cinematography", "Oscar Best Costume Design"],
288
+ "rating": 3.8
289
+ },
290
+ {
291
+ "title": "The English Patient",
292
+ "description": "At the close of WWII, a young nurse tends to a badly-burned plane crash victim. His past is shown in flashbacks, revealing an involvement in a fateful love affair.",
293
+ "genre": ["Drama", "Romance", "War"],
294
+ "director": "Anthony Minghella",
295
+ "actors": ["Ralph Fiennes", "Juliette Binoche", "Willem Dafoe"],
296
+ "year": 1996,
297
+ "box_office": 232000000,
298
+ "budget": 20000000,
299
+ "awards": ["Oscar Best Picture", "Oscar Best Director", "Oscar Best Supporting Actress", "Oscar Best Cinematography", "Oscar Best Film Editing", "Oscar Best Original Score", "Oscar Best Sound", "Oscar Best Art Direction", "Oscar Best Costume Design"],
300
+ "rating": 3.8
301
  },
302
  {
303
+ "title": "Out of Africa",
304
+ "description": "In 20th-century colonial Kenya, a Danish baroness/plantation owner has a passionate love affair with a free-spirited big-game hunter.",
305
+ "genre": ["Drama", "Romance"],
306
+ "director": "Sydney Pollack",
307
+ "actors": ["Meryl Streep", "Robert Redford", "Klaus Maria Brandauer"],
308
+ "year": 1985,
309
+ "box_office": 227000000,
310
+ "budget": 27000000,
311
+ "awards": ["Oscar Best Picture", "Oscar Best Director", "Oscar Best Adapted Screenplay", "Oscar Best Cinematography", "Oscar Best Original Score", "Oscar Best Sound", "Oscar Best Art Direction"],
312
+ "rating": 3.6
313
+ },
314
+ # History Movies
315
+ {
316
+ "title": "Schindler's List",
317
+ "description": "In German-occupied Poland during World War II, industrialist Oskar Schindler gradually becomes concerned for his Jewish workforce after witnessing their persecution by the Nazis.",
318
+ "genre": ["Drama", "History", "War"],
319
+ "director": "Steven Spielberg",
320
+ "actors": ["Liam Neeson", "Ben Kingsley", "Ralph Fiennes"],
321
+ "year": 1993,
322
+ "box_office": 322000000,
323
+ "budget": 22000000,
324
+ "awards": ["Oscar Best Picture", "Oscar Best Director", "Oscar Best Adapted Screenplay", "Oscar Best Cinematography", "Oscar Best Film Editing", "Oscar Best Original Score", "Oscar Best Art Direction"],
325
+ "rating": 4.5
326
+ },
327
+ {
328
+ "title": "The King's Speech",
329
+ "description": "The story of King George VI, his impromptu ascension to the throne of the British Empire in 1936, and the speech therapist who helped the unsure monarch overcome his stammer.",
330
+ "genre": ["Drama", "History"],
331
+ "director": "Tom Hooper",
332
+ "actors": ["Colin Firth", "Geoffrey Rush", "Helena Bonham Carter"],
333
+ "year": 2010,
334
+ "box_office": 414000000,
335
+ "budget": 15000000,
336
+ "awards": ["Oscar Best Picture", "Oscar Best Director", "Oscar Best Actor", "Oscar Best Original Screenplay"],
337
+ "rating": 4.0
338
+ },
339
+ {
340
+ "title": "Gandhi",
341
+ "description": "The life of the lawyer who became the famed leader of the Indian revolts against the British rule through his philosophy of nonviolent protest.",
342
+ "genre": ["Drama", "History"],
343
+ "director": "Richard Attenborough",
344
+ "actors": ["Ben Kingsley", "Candice Bergen", "Edward Fox"],
345
+ "year": 1982,
346
+ "box_office": 52000000,
347
+ "budget": 22000000,
348
+ "awards": ["Oscar Best Picture", "Oscar Best Director", "Oscar Best Actor", "Oscar Best Original Screenplay", "Oscar Best Cinematography", "Oscar Best Film Editing", "Oscar Best Costume Design", "Oscar Best Art Direction"],
349
+ "rating": 4.0
350
+ },
351
+ {
352
+ "title": "Apollo 13",
353
+ "description": "NASA must devise a strategy to return Apollo 13 to Earth safely after the spacecraft undergoes massive internal damage putting the lives of the three astronauts on board in jeopardy.",
354
+ "genre": ["Drama", "History"],
355
+ "director": "Ron Howard",
356
+ "actors": ["Tom Hanks", "Bill Paxton", "Kevin Bacon"],
357
+ "year": 1995,
358
+ "box_office": 355000000,
359
+ "budget": 40000000,
360
+ "awards": ["Oscar Best Film Editing", "Oscar Best Sound"],
361
+ "rating": 3.9
362
+ },
363
+ {
364
+ "title": "Braveheart",
365
+ "description": "Scottish warrior William Wallace leads his countrymen in a rebellion to free his homeland from the tyranny of King Edward I of England.",
366
+ "genre": ["Action", "Drama", "History", "War"],
367
+ "director": "Mel Gibson",
368
+ "actors": ["Mel Gibson", "Sophie Marceau", "Patrick McGoohan"],
369
+ "year": 1995,
370
+ "box_office": 210000000,
371
+ "budget": 70000000,
372
+ "awards": ["Oscar Best Picture", "Oscar Best Director", "Oscar Best Cinematography", "Oscar Best Sound Effects Editing", "Oscar Best Makeup"],
373
+ "rating": 4.2
374
+ },
375
+ # Comedy Movies
376
+ {
377
+ "title": "Intouchables",
378
+ "description": "A true story of two men who should never have met – a quadriplegic aristocrat who was injured in a paragliding accident and a young man from the projects.",
379
+ "genre": ["Drama", "Comedy"],
380
+ "director": "Olivier Nakache",
381
+ "actors": ["François Cluzet", "Omar Sy", "Anne Le Ny"],
382
+ "year": 2011,
383
+ "box_office": 426590315,
384
+ "budget": 13000000,
385
+ "awards": ["Golden Globe Award Best Foreign Language Film", "BAFTA Award Best Film Not in the English Language"],
386
+ "rating": 4.0
387
+ },
388
+ {
389
+ "title": "Inside Out",
390
+ "description": "When 11-year-old Riley moves to a new city, her Emotions team up to help her through the transition. Joy, Fear, Anger, Disgust and Sadness work together, but when Joy and Sadness get lost, they must journey through unfamiliar places to get back home.",
391
+ "genre": ["Drama", "Comedy", "Animation", "Family"],
392
+ "director": "Pete Docter",
393
+ "actors": ["Amy Poehler", "Phyllis Smith", "Richard Kind"],
394
+ "year": 2015,
395
+ "box_office": 859076254,
396
+ "budget": 175000000,
397
+ "awards": ["Oscar Best Animated Feature", "Golden Globe Award Best Animated Feature Film"],
398
+ "rating": 3.9
399
+ },
400
+ {
401
+ "title": "Paddington in Peru",
402
+ "description": "Paddington travels to Peru to visit his beloved Aunt Lucy, who now resides at the Home for Retired Bears. With the Brown Family in tow, a thrilling adventure ensues when a mystery plunges them into an unexpected journey through the Amazon rainforest and up to the mountain peaks of Peru.",
403
+ "genre": ["Comedy", "Family", "Adventure"],
404
+ "director": "Dougal Wilson",
405
+ "actors": ["Ben Whishaw", "Hugh Bonneville", "Emily Mortimer"],
406
+ "year": 2024,
407
+ "box_office": 175614864,
408
+ "budget": 90000000,
409
+ "awards": ["Visual Effects Society (VES) Award"],
410
+ "rating": 3.5
411
+ },
412
+ {
413
+ "title": "Free Guy",
414
+ "description": "A bank teller discovers he is actually a background player in an open-world video game, and decides to become the hero of his own story. Now, in a world where there are no limits, he is determined to be the guy who saves his world his way before it's too late.",
415
+ "genre": ["Comedy", "Sci-Fi", "Adventure"],
416
+ "director": "Dougal Wilson",
417
+ "actors": ["Ryan Reynolds", "Jodie Comer", "Joe Keery"],
418
+ "year": 2021,
419
+ "box_office": 331526598,
420
+ "budget": 100000000,
421
+ "awards": ["Critics' Choice Super Award", "People’s Choice Award People’s Choice Award"],
422
+ "rating": 3.5
423
+ },
424
+ {
425
+ "title": "Barbie",
426
+ "description": "Barbie and Ken are having the time of their lives in the colorful and seemingly perfect world of Barbie Land. However, when they get a chance to go to the real world, they soon discover the joys and perils of living among humans.",
427
+ "genre": ["Comedy", "Adventure"],
428
+ "director": "Greta Gerwig",
429
+ "actors": ["Margot Robbie", "Ryan Gosling", "America Ferrera"],
430
+ "year": 2023,
431
+ "box_office": 1447038421,
432
+ "budget": 145000000,
433
+ "awards": ["Golden Globe Award Best Motion Picture", "BAFTA Award Best Production Design"],
434
+ "rating": 3.5
435
+ },
436
+ # Action Movies
437
+ {
438
+ "title": "Avengers: Endgame",
439
+ "description": "After the devastating events of Avengers: Infinity War, the universe is in ruins due to the efforts of the Mad Titan, Thanos. With the help of remaining allies, the Avengers must assemble once more in order to undo Thanos' actions and restore order to the universe once and for all, no matter what consequences may be in store.",
440
+ "genre": ["Adventure", "Science Fiction", "Action"],
441
+ "director": "Anthony Russo",
442
+ "actors": ["Robert Downey Jr.", "Chris Evans", "Mark Ruffalo"],
443
+ "year": 2019,
444
+ "box_office": 2799439100,
445
+ "budget": 356000000,
446
+ "awards": ["Critics' Choice Movie Awards Best Action Movie", "MTV Movie & TV Awards Best Movie"],
447
+ "rating": 3.9
448
+ },
449
+ {
450
+ "title": "Mission: Impossible – Fallout",
451
+ "description": "When an IMF mission ends badly, the world is faced with dire consequences. As Ethan Hunt takes it upon himself to fulfill his original briefing, the CIA begin to question his loyalty and his motives. The IMF team find themselves in a race against time, hunted by assassins while trying to prevent a global catastrophe.",
452
+ "genre": ["Action", "Adventure"],
453
+ "director": "Christopher McQuarrie",
454
+ "actors": ["Tom Cruise", "Henry Cavill", "Ving Rhames"],
455
+ "year": 2018,
456
+ "box_office": 791658205,
457
+ "budget": 178000000,
458
+ "awards": [],
459
+ "rating": 3.8
460
+ },
461
+ {
462
+ "title": "Black Widow",
463
+ "description": "Natasha Romanoff, also known as Black Widow, confronts the darker parts of her ledger when a dangerous conspiracy with ties to her past arises. Pursued by a force that will stop at nothing to bring her down, Natasha must deal with her history as a spy and the broken relationships left in her wake long before she became an Avenger.",
464
+ "genre": ["Adventure", "Sci-Fi", "Action"],
465
+ "director": "Cate Shortland",
466
+ "actors": ["Scarlett Johansson", "Florence Pugh", "Rachel Weisz"],
467
+ "year": 2021,
468
+ "box_office": 379751655,
469
+ "budget": 200000000,
470
+ "awards": ["People's Choice Awards", "American Film Institute Movies of the Year"],
471
+ "rating": 3.1
472
+ },
473
+ {
474
+ "title": "Deadpool & Wolverine",
475
+ "description": "A listless Wade Wilson toils away in civilian life with his days as the morally flexible mercenary, Deadpool, behind him. But when his homeworld faces an existential threat, Wade must reluctantly suit-up again with an even more reluctant Wolverine.",
476
+ "genre": ["Comedy", "Sci-Fi", "Action"],
477
+ "director": "Shawn Levy",
478
+ "actors": ["Ryan Reynolds", "Hugh Jackman", "Emma Corrin"],
479
+ "year": 2024,
480
+ "box_office": 1338073645,
481
+ "budget": 200000000,
482
+ "awards": ["Critics' Choice Super Award Best Action Movie", "Golden Globe Awards Best Motion Picture – Musical or Comedy"],
483
+ "rating": 3.5
484
+ },
485
+ {
486
+ "title": "Top Gun: Maverick",
487
+ "description": "After more than thirty years of service as one of the Navy’s top aviators, and dodging the advancement in rank that would ground him, Pete “Maverick” Mitchell finds himself training a detachment of TOP GUN graduates for a specialized mission the likes of which no living pilot has ever seen.",
488
+ "genre": ["Action", "Drama"],
489
+ "director": "Joseph Kosinski",
490
+ "actors": ["Tom Cruise", "Miles Teller", "Monica Barbaro"],
491
+ "year": 2022,
492
+ "box_office": 1495696292,
493
+ "budget": 170000000,
494
+ "awards": ["Oscar Best Sound", "BAFTA Award Best Cinematography"],
495
+ "rating": 3.9
496
+ },
497
+ # Thriller or Horror Movies
498
+ {
499
+ "title": "Uncut Gems",
500
+ "description": "A charismatic New York City jeweler always on the lookout for the next big score makes a series of high-stakes bets that could lead to the windfall of a lifetime. Howard must perform a precarious high-wire act, balancing business, family, and encroaching adversaries on all sides in his relentless pursuit of the ultimate win.",
501
+ "genre": ["Drama", "Thriller", "Crime"],
502
+ "director": "Benny Safdie",
503
+ "actors": ["Adam Sandler", "LaKeith Stanfield", "Julia Fox"],
504
+ "year": 2019,
505
+ "box_office": 50023780,
506
+ "budget": 19000000,
507
+ "awards": ["National Board of Review Awards Best Actor"],
508
+ "rating": 3.8
509
+ },
510
+ {
511
+ "title": "Dark Waters",
512
+ "description": "A tenacious attorney uncovers a dark secret that connects a growing number of unexplained deaths to one of the world's largest corporations. In the process, he risks everything — his future, his family, and his own life — to expose the truth.",
513
  "genre": ["Drama", "Thriller"],
514
+ "director": "Todd Haynes",
515
+ "actors": ["Mark Ruffalo", "Anne Hathaway", "Tim Robbins"],
516
  "year": 2019,
517
+ "box_office": 23108017,
518
+ "budget": 30000000,
519
+ "awards": [],
520
+ "rating": 3.9
521
+ },
522
+ {
523
+ "title": "Promising Young Woman",
524
+ "description": "A young woman, traumatized by a tragic event in her past, seeks out vengeance against those who crossed her path.",
525
+ "genre": ["Drama", "Thriller", "Crime"],
526
+ "director": "Emerald Fennell",
527
+ "actors": ["Carey Mulligan", "Bo Burnham", "Alison Brie"],
528
+ "year": 2020,
529
+ "box_office": 18854166,
530
+ "budget": 10000000,
531
+ "awards": ["Oscar Best Original Screenplay", "BAFTA Awards Outstanding British Film"],
532
+ "rating": 3.7
533
+ },
534
+ {
535
+ "title": "The Substance",
536
+ "description": "A fading celebrity decides to use a black market drug, a cell-replicating substance that temporarily creates a younger, better version of herself.",
537
+ "genre": ["Comedy", "Sci-Fi", "Horror"],
538
+ "director": "Coralie Fargeat",
539
+ "actors": ["Demi Moore", "Margaret Qualley", "Dennis Quaid"],
540
+ "year": 2024,
541
+ "box_office": 77307177,
542
+ "budget": 17500000,
543
+ "awards": ["Oscar Best Picture", "Golden Globe Awards Best Actress"],
544
+ "rating": 3.5
545
+ },
546
+ {
547
+ "title": "Smile 2",
548
+ "description": "About to embark on a new world tour, global pop sensation Skye Riley begins experiencing increasingly terrifying and inexplicable events. Overwhelmed by the escalating horrors and the pressures of fame, Skye is forced to face her dark past to regain control of her life before it spirals out of control.",
549
+ "genre": ["Horror", "Mystery"],
550
+ "director": "Parker Finn",
551
+ "actors": ["Naomi Scott, Rosemarie DeWitt, Lukas Gage"],
552
+ "year": 2024,
553
+ "box_office": 138128854,
554
+ "budget": 28000000,
555
+ "awards": [],
556
+ "rating": 3.2
557
  }
558
  ]
559
 
 
646
 
647
  def get_log_text(self):
648
  """Get all logs as a formatted string"""
649
+ # Format logs with clear section dividers for readability
650
+ formatted_logs = []
651
+
652
+ # Add a header
653
+ formatted_logs.append("===== COMPLETE AGENT CONVERSATION LOG =====\n")
654
+
655
+ # Add all log entries with original formatting
656
+ for log_entry in self.log_output:
657
+ formatted_logs.append(log_entry)
658
+
659
+ # Join with double newlines for better readability
660
+ return "\n".join(formatted_logs)
661
 
662
  # Create a global logger
663
  logger = AgentConversationLogger()
664
 
665
+ # Create a class to handle the movie knowledge base with embeddings
666
  class MovieKnowledgeBase:
667
  def __init__(self, movies):
668
  self.movies = movies
 
671
  # Precompute embeddings for all movie descriptions
672
  self.descriptions = [movie["description"] for movie in movies]
673
  self.embeddings = self.model.encode(self.descriptions)
674
+
675
  def find_similar_movies(self, description, top_n=3):
676
  """Find the top N similar movies to the given description using embeddings."""
677
  # Encode the query description
678
  query_embedding = self.model.encode([description])[0]
679
+
680
  # Calculate cosine similarity between query and all movies
681
  similarities = cosine_similarity([query_embedding], self.embeddings)[0]
682
+
683
  # Get indices of top N similar movies
684
  top_indices = np.argsort(similarities)[-top_n:][::-1]
685
+
686
  # Create the result with movies and similarity scores
687
  similar_movies = []
688
  for idx in top_indices:
 
690
  "movie": self.movies[idx],
691
  "similarity_score": float(similarities[idx])
692
  })
693
+
694
  return similar_movies
695
 
696
  # Initialize the knowledge base with embeddings
697
  movie_kb = MovieKnowledgeBase(movie_knowledge_base)
698
 
699
+ def find_similar_movies(query_description, top_n=3):
700
+ """
701
+ Find movies similar to the query description using sentence embeddings and cosine similarity.
702
+
703
+ This implementation uses SentenceTransformer to create semantic embeddings
704
+ of the movie descriptions and calculates similarity using cosine similarity.
705
+ """
706
+ return movie_kb.find_similar_movies(query_description, top_n)
707
+
708
+ # Original functions for analysis
709
+ def predict_box_office(movie_description, similar_movies):
710
+
711
+ import numpy as np
712
+
713
+ def get_inflation_adjustment(year, current_year=2025):
714
+ return 1 + (current_year - year) * 0.03
715
+
716
+ def get_year_weight(year):
717
+ return 1.0 if year >= 2000 else 0.6
718
+
719
+ adjusted_box_offices = []
720
+ total_weight = 0
721
+ for movie_info in similar_movies:
722
+ movie = movie_info["movie"]
723
+ sim_score = movie_info["similarity_score"]
724
+ inflation_factor = get_inflation_adjustment(movie["year"])
725
+ year_weight = get_year_weight(movie["year"])
726
+ adjusted_bo = movie["box_office"] * year_weight * inflation_factor
727
+ adjusted_box_offices.append(sim_score * adjusted_bo)
728
+ total_weight += sim_score * year_weight
729
+
730
+ if total_weight > 0:
731
+ base_estimate = sum(adjusted_box_offices) / total_weight
732
+ else:
733
+ base_estimate = np.mean([m["box_office"] * get_inflation_adjustment(m["year"]) for m in movie_knowledge_base])
734
+ genres = set()
735
+ for movie_info in similar_movies:
736
+ movie = movie_info["movie"]
737
+ if isinstance(movie["genre"], list):
738
+ genres.update(movie["genre"])
739
+ else:
740
+ genres.add(movie["genre"])
741
+
742
+ gc_list = []
743
+ for genre in genres:
744
+ genre_movies = [m for m in movie_knowledge_base if genre in m["genre"] and m["budget"] > 0]
745
+ if not genre_movies:
746
+ continue
747
+ success_rate = sum(1 for m in genre_movies if (m["box_office"] / m["budget"]) >= 2.5) / len(genre_movies)
748
+ rois = [m["box_office"] / m["budget"] for m in genre_movies]
749
+ roi_median = np.median(rois)
750
+ revenues = [m["box_office"] for m in genre_movies]
751
+ mean_revenue = np.mean(revenues)
752
+ std_revenue = np.std(revenues)
753
+ cv = std_revenue / mean_revenue if mean_revenue > 0 else 0
754
+ gc = success_rate * roi_median * (1 / (1 + cv))
755
+ gc_list.append(gc)
756
+
757
+ final_gc = np.mean(gc_list) if gc_list else 1
758
+
759
+ def lhs_sampling(mean_value, lower_factor=0.9, upper_factor=1.1, num_samples=1000):
760
+ intervals = np.linspace(lower_factor, upper_factor, num_samples + 1)
761
+ samples = intervals[:-1] + np.diff(intervals) * np.random.rand(num_samples)
762
+ np.random.shuffle(samples)
763
+ return mean_value * samples
764
+
765
+ num_simulations = 1000
766
+ simulated_gc = lhs_sampling(final_gc, num_samples=num_simulations)
767
+ simulations = base_estimate * simulated_gc
768
+
769
+ return simulations
770
+
771
+
772
+ def predict_awards(movie_description, similar_movies):
773
+ """Predict potential awards based on similar movies."""
774
+ # Count awards in similar movies and recommend the most common ones
775
+ award_counts = {}
776
+
777
+ for movie_info in similar_movies:
778
+ similarity = movie_info["similarity_score"]
779
+ awards = movie_info["movie"]["awards"]
780
 
781
+ for award in awards:
782
+ if award in award_counts:
783
+ award_counts[award] += similarity
784
+ else:
785
+ award_counts[award] = similarity
786
+
787
+ # Sort awards by their weighted counts
788
+ sorted_awards = sorted(award_counts.items(), key=lambda x: x[1], reverse=True)
789
+
790
+ # Return top 3 potential awards
791
+ potential_awards = [award for award, count in sorted_awards[:3]]
792
+
793
+ # If no similar movie has awards, return a message
794
+ if not potential_awards:
795
+ return ["No award predictions available based on similar movies"]
796
+
797
+ return potential_awards
798
+
799
+ # Function tools with logging
800
  @function_tool
801
+ @log_function_tool(logger)
802
  def get_similar_movies(movie_description: str):
803
  """Find the top 3 movies most similar to the given movie description."""
804
+ similar_movies = find_similar_movies(movie_description, top_n=3)
805
  # Convert to a more readable format for the agents
806
  result = []
807
  for movie_info in similar_movies:
 
814
  "year": movie["year"],
815
  "box_office": movie["box_office"],
816
  "awards": movie["awards"],
817
+ "actors": movie["actors"],
818
+ "rating": movie["rating"],
819
  "similarity_score": movie_info["similarity_score"]
820
  })
821
  return result
822
 
823
  @function_tool
824
+ @log_function_tool(logger)
825
  def get_box_office_prediction(movie_description: str):
826
  """Predict the box office revenue for a movie based on its description."""
827
+ similar_movies = find_similar_movies(movie_description, top_n=3)
828
+ simulations = predict_box_office(movie_description, similar_movies)
829
+ baseline_prediction = np.median(simulations)
830
+ lower_bound = np.percentile(simulations, 25)
831
+ upper_bound = np.percentile(simulations, 75)
832
+
833
+ budgets = [movie_info["movie"]["budget"] for movie_info in similar_movies if movie_info["movie"]["budget"] > 0]
834
+ if budgets:
835
+ avg_budget = np.mean(budgets)
836
  else:
837
+ avg_budget = 1
838
+
839
+ threshold_exceed = 3 * avg_budget
840
+ threshold_below = 2 * avg_budget
841
+ prob_exceed = float(np.mean(simulations > threshold_exceed))
842
+ prob_below = float(np.mean(simulations < threshold_below))
843
+
844
  # Convert to a more readable format
845
  similar_movie_info = []
846
  for movie_info in similar_movies:
847
  movie = movie_info["movie"]
848
  similar_movie_info.append({
849
  "title": movie["title"],
850
+ "year": movie["year"],
851
  "box_office": movie["box_office"],
852
  "similarity_score": movie_info["similarity_score"]
853
  })
854
+
855
  return {
856
+ "predicted_revenue": round(baseline_prediction, 2),
857
+ "confidence_interval": (round(lower_bound, 2), round(upper_bound, 2)),
858
+ "risk_probabilities": {
859
+ "exceed_3x_budget": prob_exceed,
860
+ "below_2x_budget": prob_below
861
+ },
862
  "similar_movies": similar_movie_info
863
  }
864
 
865
  @function_tool
866
+ @log_function_tool(logger)
867
  def get_award_predictions(movie_description: str):
868
  """Predict potential awards for a movie based on its description."""
869
+ similar_movies = find_similar_movies(movie_description, top_n=3)
870
+ potential_awards = predict_awards(movie_description, similar_movies)
871
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
872
  # Convert to a more readable format
873
  similar_movie_info = []
874
  for movie_info in similar_movies:
 
878
  "awards": movie["awards"],
879
  "similarity_score": movie_info["similarity_score"]
880
  })
881
+
882
  return {
883
  "potential_awards": potential_awards,
884
  "similar_movies": similar_movie_info
885
  }
886
 
887
+ # Specialized agents
888
  similarity_agent = Agent(
889
  name="Movie Similarity Expert",
890
  instructions="""
891
  You are an expert in movie analysis and recommendations.
892
  Your task is to analyze the description of a new movie and find similar movies.
893
  Provide detailed recommendations with justifications for why these movies are similar.
894
+ Introduce you are similarity agent in your analysis.
 
895
  """,
896
  tools=[get_similar_movies]
897
  )
 
900
  name="Box Office Analyst",
901
  instructions="""
902
  You are an expert in predicting movie box office performance.
903
+ Your task is to estimate the potential gross worldwide revenue of a movie by comparing its description with similar movies.
904
+ In your analysis, you will adjust historical box office data for inflation and apply appropriate weightings based on release year.
905
+ Furthermore, you should account for risk factors by calculating the probability that the predicted revenue exceeds 3 times the average budget of similar movies or falls below 2 times the budget.
906
+ Introduce yourself as the revenue agent and explain your prediction methodology, referencing the performance of similar movies, the inflation adjustments, and the uncertainty derived from Monte Carlo simulations.
907
  """,
908
  tools=[get_box_office_prediction]
909
  )
 
914
  You are an expert in predicting movie awards and critical reception.
915
  Your task is to predict potential awards a movie might receive based on similar movies.
916
  Explain your predictions by referencing similar award-winning films.
917
+ Introduce you are award agent in your analysis.
 
918
  """,
919
  tools=[get_award_predictions]
920
  )
 
923
  orchestrator_agent = Agent(
924
  name="Movie Analysis Orchestrator",
925
  instructions="""
926
+ You are the central coordinator for movie analysis tasks and need to handoff to the appropriate agent.
927
+
928
  Your responsibilities include:
929
  1. Properly understanding the user's movie analysis request
930
+ 2. Handoff specific analysis tasks to the appropriate specialized agents based on the user's request:
 
931
  - Movie Similarity Expert for recommendation tasks
932
  - Box Office Analyst for revenue prediction tasks
933
  - Award Prediction Specialist for award prediction tasks
 
 
 
 
934
  """,
935
+ #tools=[get_similar_movies],
936
  handoffs=[similarity_agent, revenue_agent, award_agent]
937
  )
938
 
 
965
  """Run the multi-agent system with the given query and analysis type."""
966
  logger.clear_logs()
967
 
968
+ # Add a start marker to the log
969
+ logger.log_output.append(f"\n===== STARTING ANALYSIS: {analysis_type.upper()} =====")
970
+ logger.log_output.append(f"Query: {query}")
971
+ logger.log_output.append("=======================================")
972
+
973
  # Modify the query based on the analysis type
974
  if analysis_type == "similar":
975
  enhanced_query = f"{query} Please recommend similar movies."
 
981
  enhanced_query = f"{query} Please provide a complete analysis including similar movies, box office potential, and award possibilities."
982
 
983
  # Run the orchestrator with the query
984
+ try:
985
+ result = await Runner.run(orchestrator_agent, input=enhanced_query)
986
+
987
+ # Add a completion marker to the log
988
+ logger.log_output.append("\n===== ANALYSIS COMPLETE =====")
989
+
990
+ # Log the full raw conversation for debugging
991
+ conversation_summary = "\n\n===== CONVERSATION SUMMARY =====\n"
992
+ conversation_summary += f"Total messages: {len(logger.conversation_log)}\n"
993
+ conversation_summary += f"Total function calls: {len(logger.function_call_log)}\n"
994
+
995
+ # Add the conversation summary to the log output
996
+ logger.log_output.append(conversation_summary)
997
+
998
+ # Return both the final output and the log
999
+ return {
1000
+ "result": result.final_output,
1001
+ "log": logger.get_log_text()
1002
+ }
1003
 
1004
+ except Exception as e:
1005
+ # Log any errors
1006
+ error_message = f"\n===== ERROR DURING ANALYSIS =====\n{str(e)}"
1007
+ logger.log_output.append(error_message)
1008
+
1009
+ return {
1010
+ "result": f"An error occurred during analysis: {str(e)}",
1011
+ "log": logger.get_log_text()
1012
+ }
1013
 
1014
  # Gradio interface function (synchronous wrapper for the async function)
1015
  def process_query(description, analysis_type):
 
1018
  asyncio.set_event_loop(loop)
1019
  try:
1020
  result = loop.run_until_complete(run_agent_analysis(description, analysis_type))
1021
+
1022
+ # Ensure the conversation log captures everything by adding a summary at the end
1023
+ full_log = result["log"]
1024
+
1025
+ # Add a summary of all agents involved
1026
+ agent_names = set()
1027
+ for entry in logger.conversation_log:
1028
+ if entry["type"] == "message":
1029
+ agent_names.add(entry["sender"])
1030
+ agent_names.add(entry["receiver"])
1031
+
1032
+ if "User" in agent_names:
1033
+ agent_names.remove("User")
1034
+
1035
+ # Return the complete log and analysis result
1036
+ return full_log, result["result"]
1037
  finally:
1038
  loop.close()
1039
 
 
1046
  ]
1047
 
1048
  # Create the Gradio interface
1049
+ with gr.Blocks(title="Movie Analysis Multi-Agent System", css="""
1050
+ .monospace-text textarea {
1051
+ font-family: monospace !important;
1052
+ white-space: pre !important;
1053
+ overflow-x: auto !important;
1054
+ font-size: 0.9em !important;
1055
+ }
1056
+ """) as demo:
1057
  gr.Markdown("# Movie Analysis Multi-Agent System")
1058
  gr.Markdown("""
1059
  This demo uses a multi-agent system to analyze movie descriptions. Enter a description of your movie idea,
 
1069
  )
1070
 
1071
  analysis_type = gr.Radio(
1072
+ ["all", "similar", "box_office", "awards"],
1073
  label="Analysis Type",
1074
  value="all"
1075
  )
 
1081
  with gr.TabItem("Analysis Result"):
1082
  result_output = gr.Markdown(label="Analysis")
1083
  with gr.TabItem("Agent Conversation Log"):
1084
+ conversation_output = gr.Textbox(
1085
+ label="Conversation Log",
1086
+ lines=30,
1087
+ max_lines=100,
1088
+ show_copy_button=True,
1089
+ container=True,
1090
+ scale=2,
1091
+ autoscroll=False,
1092
+ elem_classes="monospace-text"
1093
+ )
1094
 
1095
  submit_btn.click(
1096
  fn=process_query,