cicboy commited on
Commit
e40cdd9
·
1 Parent(s): 5a6f53f

Add application file

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Requires:
3
+ /usr/local/bin/python3.13 -m pip install python-dotenv pydantic-ai imdbpy
4
+ OpenAI API Key
5
+ """
6
+
7
+ from dataclasses import dataclass
8
+ import imdb
9
+ import asyncio
10
+ from pydantic import BaseModel
11
+ from pydantic_ai import Agent, RunContext
12
+ import gradio as gr
13
+ from pathlib import Path
14
+
15
+
16
+ from dotenv import load_dotenv
17
+ import os
18
+
19
+ script_folder = Path(__file__).parent
20
+ dotenv_path = script_folder/"Open_AI.env"
21
+ load_dotenv(dotenv_path=dotenv_path)
22
+ os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
23
+
24
+
25
+ # A pydantic basemodel for our output schema
26
+ class Review(BaseModel):
27
+ why: str
28
+ rating: int
29
+ recommended: bool
30
+
31
+ # A class to externally connect to the IMDB API to fetch movie information
32
+ class IMDbConnection:
33
+ def __init__(self):
34
+ self.ia = imdb.IMDb()
35
+
36
+ async def get_movie_info(self,title: str) -> dict:
37
+ # Search for the movie using IMDB API
38
+ print(f"Searching for movie: {title}")
39
+ movies = self.ia.search_movie(title)
40
+ movie = self.ia.get_movie(movies[0].movieID)
41
+ system_prompt = f"""
42
+ Title: {movie.get("title")}\n
43
+ Rating: {movie.get("rating")}\n
44
+ Plot: {movie.get("plot")[0] if movie.get("plot") else None}
45
+ """
46
+ print(f"System prompt: {system_prompt}\n\n")
47
+ return system_prompt
48
+
49
+ # A container containing dependencies for the agent
50
+ @dataclass
51
+ class MovieData:
52
+ title: str
53
+ imdb_conn: IMDbConnection
54
+
55
+ agent = Agent(
56
+ "openai:gpt-4o-mini",
57
+ deps_type=MovieData,
58
+ output_type=Review,
59
+ )
60
+
61
+ # Dynamically generate the system prompt
62
+ @agent.system_prompt
63
+ async def get_movie_info(ctx: RunContext[Review]):
64
+
65
+ # Given an input, the system prompt will be generated by querying the IMDB API
66
+ return await ctx.deps.imdb_conn.get_movie_info(ctx.deps.title) #Fetch movie info
67
+
68
+ # create a gradio wrapper
69
+
70
+ async def run_agent(user_query: str, movie_title: str):
71
+ deps = MovieData(title=movie_title, imdb_conn=IMDbConnection())
72
+ result = await agent.run(user_query, deps=deps)
73
+ return {
74
+ "Why": result.output.why,
75
+ "Rating": result.output.rating,
76
+ "Recommend": result.output.recommended
77
+ }
78
+
79
+ #run agent in gradio
80
+ def run_gradio(user_query, movie_title):
81
+ return asyncio.run(run_agent(user_query, movie_title))
82
+
83
+ # build gradio ui
84
+ with gr.Blocks() as demo:
85
+ gr.Markdown("## Movie Recommender Agent")
86
+ user_query = gr.Textbox(label="What is your preference? (e.g. 'I like supernatural horror movies')")
87
+ movie_title = gr.Textbox(label="Movie name (e.g. 'The Conjuring')")
88
+ output = gr.JSON(label = "Agent Recommendation")
89
+
90
+ submit_btn = gr.Button("Check Movie")
91
+ submit_btn.click(fn=run_gradio, inputs=[user_query, movie_title], outputs=output)
92
+
93
+ #launch
94
+ if __name__ == "__main__":
95
+ demo.launch(share=True)