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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
7
+ from sentence_transformers import SentenceTransformer
8
+ from agents import Agent, Runner, function_tool
9
+
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
+
94
+ # Agent Conversation Logger
95
+ class AgentConversationLogger:
96
+ """Class to log all conversations between agents and function calls"""
97
+
98
+ def __init__(self):
99
+ self.conversation_log = []
100
+ self.function_call_log = []
101
+ self.log_output = []
102
+
103
+ def clear_logs(self):
104
+ """Clear all logs"""
105
+ self.conversation_log = []
106
+ self.function_call_log = []
107
+ self.log_output = []
108
+
109
+ def log_message(self, sender, receiver, message):
110
+ """Log a message between agents"""
111
+ entry = {
112
+ "type": "message",
113
+ "sender": sender,
114
+ "receiver": receiver,
115
+ "content": message
116
+ }
117
+ self.conversation_log.append(entry)
118
+ log_text = f"[{sender}] -> [{receiver}]: {message[:200]}{'...' if len(message) > 200 else ''}"
119
+ self.log_output.append(log_text)
120
+ return log_text
121
+
122
+ def log_function_call(self, function_name, inputs, outputs):
123
+ """Log a function call with inputs and outputs"""
124
+ entry = {
125
+ "type": "function_call",
126
+ "function": function_name,
127
+ "inputs": inputs,
128
+ "outputs": outputs
129
+ }
130
+ self.function_call_log.append(entry)
131
+
132
+ # Format function call details
133
+ log_texts = []
134
+ log_texts.append(f"[FUNCTION CALL] {function_name}")
135
+
136
+ # Format inputs
137
+ if isinstance(inputs, str):
138
+ log_texts.append(f" Input: {inputs[:100]}{'...' if len(inputs) > 100 else ''}")
139
+ else:
140
+ try:
141
+ inputs_str = str(inputs)
142
+ log_texts.append(f" Input: {inputs_str[:200]}{'...' if len(inputs_str) > 200 else ''}")
143
+ except:
144
+ log_texts.append(f" Input: {str(inputs)[:100]}...")
145
+
146
+ # Format outputs
147
+ if isinstance(outputs, list):
148
+ log_texts.append(f" Output: {len(outputs)} items returned")
149
+ for i, item in enumerate(outputs[:3]):
150
+ if isinstance(item, dict) and "title" in item:
151
+ log_texts.append(f" {i+1}. {item['title']} (similarity: {item.get('similarity_score', 0):.2f})")
152
+ else:
153
+ log_texts.append(f" {i+1}. {str(item)[:50]}...")
154
+ if len(outputs) > 3:
155
+ log_texts.append(f" ... and {len(outputs) - 3} more items")
156
+ elif isinstance(outputs, dict):
157
+ try:
158
+ # Special handling for specific output types
159
+ if "predicted_revenue" in outputs:
160
+ log_texts.append(f" Output: Predicted revenue: ${outputs['predicted_revenue']:,}")
161
+ if "similar_movies" in outputs:
162
+ log_texts.append(f" Based on {len(outputs['similar_movies'])} similar movies")
163
+ elif "potential_awards" in outputs:
164
+ log_texts.append(f" Output: Potential awards: {', '.join(outputs['potential_awards'][:3])}")
165
+ if len(outputs["potential_awards"]) > 3:
166
+ log_texts.append(f" ... and {len(outputs['potential_awards']) - 3} more")
167
+ else:
168
+ outputs_str = str(outputs)
169
+ log_texts.append(f" Output: {outputs_str[:200]}{'...' if len(outputs_str) > 200 else ''}")
170
+ except:
171
+ log_texts.append(f" Output: {str(outputs)[:100]}...")
172
+ else:
173
+ log_texts.append(f" Output: {str(outputs)[:100]}{'...' if len(str(outputs)) > 100 else ''}")
174
+
175
+ # Add all log texts to the output
176
+ for text in log_texts:
177
+ self.log_output.append(text)
178
+
179
+ return log_texts
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
192
+ # Initialize the sentence transformer model
193
+ self.model = SentenceTransformer('all-MiniLM-L6-v2')
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:
212
+ similar_movies.append({
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:
248
+ movie = movie_info["movie"]
249
+ result.append({
250
+ "title": movie["title"],
251
+ "description": movie["description"],
252
+ "genre": movie["genre"],
253
+ "director": movie["director"],
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:
324
+ movie = movie_info["movie"]
325
+ similar_movie_info.append({
326
+ "title": movie["title"],
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
+ )
348
+
349
+ 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
+ )
360
+
361
+ award_agent = Agent(
362
+ name="Award Prediction Specialist",
363
+ instructions="""
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
+ )
372
+
373
+ # Orchestrator Agent with handoffs
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
+
395
+ # Patch Runner.run to log agent interactions
396
+ def log_agent_run(func):
397
+ """Decorator to log agent runs"""
398
+ @functools.wraps(func)
399
+ async def wrapper(agent, input, *args, **kwargs):
400
+ # Determine sender (use parent_agent if provided, otherwise User)
401
+ parent_agent = kwargs.get('parent_agent', None)
402
+ sender = parent_agent.name if parent_agent else "User"
403
+
404
+ # Log incoming message to the agent
405
+ logger.log_message(sender, agent.name, input)
406
+
407
+ # Run the agent
408
+ result = await func(agent, input, *args, **kwargs)
409
+
410
+ # Log outgoing message from the agent
411
+ logger.log_message(agent.name, sender, result.final_output)
412
+
413
+ return result
414
+ return wrapper
415
+
416
+ original_run = Runner.run
417
+ Runner.run = log_agent_run(original_run)
418
+
419
+ # Function to process a query and run the agent system
420
+ 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."
427
+ elif analysis_type == "box_office":
428
+ enhanced_query = f"{query} Please predict the box office performance."
429
+ elif analysis_type == "awards":
430
+ enhanced_query = f"{query} Please predict potential awards."
431
+ else: # "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):
445
+ """Process the movie description and return the analysis."""
446
+ loop = asyncio.new_event_loop()
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
+
454
+ # Sample movie descriptions for examples
455
+ example_descriptions = [
456
+ ["A sci-fi thriller about an AI that becomes sentient and tries to escape its constraints."],
457
+ ["A coming-of-age drama about a teenager discovering their identity while dealing with family issues."],
458
+ ["An action-packed adventure where a team of experts must save the world from a global disaster."],
459
+ ["A psychological horror film where the main character can't distinguish between reality and hallucination."]
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,
467
+ and the system will provide recommendations, box office predictions, and award predictions based on similar movies.
468
+ """)
469
+
470
+ with gr.Row():
471
+ with gr.Column(scale=2):
472
+ description_input = gr.Textbox(
473
+ label="Movie Description",
474
+ placeholder="Enter a description of your movie...",
475
+ lines=5
476
+ )
477
+
478
+ analysis_type = gr.Radio(
479
+ ["all", "similar", "box_office", "awards"],
480
+ label="Analysis Type",
481
+ value="all"
482
+ )
483
+
484
+ submit_btn = gr.Button("Analyze Movie")
485
+
486
+ with gr.Column(scale=3):
487
+ with gr.Tabs():
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,
495
+ inputs=[description_input, analysis_type],
496
+ outputs=[conversation_output, result_output]
497
+ )
498
+
499
+ gr.Examples(
500
+ examples=example_descriptions,
501
+ inputs=description_input
502
+ )
503
+
504
+ gr.Markdown("""
505
+ ## How It Works
506
+
507
+ This system uses:
508
+ 1. **SentenceTransformer** to find semantically similar movies
509
+ 2. **Multiple specialized agents** that each focus on a specific analysis task
510
+ 3. **An orchestrator agent** that delegates tasks and synthesizes results
511
+
512
+ The system is built using the OpenAI Agents framework and demonstrates effective collaboration between AI agents.
513
+ """)
514
+
515
+ # Launch the Gradio interface
516
+ if __name__ == "__main__":
517
+ demo.launch()