Moiz commited on
Commit
8ac4279
·
1 Parent(s): 6085c9f

added OMdb api

Browse files
Files changed (1) hide show
  1. app.py +20 -7
app.py CHANGED
@@ -1,16 +1,29 @@
1
  import gradio as gr
 
2
 
3
- # Function to handle movie rating
4
- def rate_movie(movie_name, rating):
5
- if rating == "N/A":
6
- return f"You haven't watched '{movie_name}' yet."
 
 
 
 
 
 
 
 
 
 
 
 
7
  else:
8
- return f"You rated '{movie_name}' a {rating}/10."
9
 
10
  # Gradio interface
11
  with gr.Blocks(css="styles.css") as app:
12
  gr.Markdown("# Movie Rating App")
13
- movie_name = gr.Textbox(label="Movie Name", placeholder="Enter the movie name...")
14
  rating = gr.Slider(
15
  minimum=0,
16
  maximum=10,
@@ -20,6 +33,6 @@ with gr.Blocks(css="styles.css") as app:
20
  submit = gr.Button("Submit Rating")
21
  output = gr.Textbox(label="Your Feedback")
22
 
23
- submit.click(rate_movie, inputs=[movie_name, rating], outputs=output)
24
 
25
  app.launch()
 
1
  import gradio as gr
2
+ import requests
3
 
4
+ # Function to fetch movie details and handle rating
5
+ def fetch_and_rate_movie(movie_id, rating):
6
+ api_key = "f2443b04"
7
+ url = f"http://www.omdbapi.com/?apikey={api_key}&i={movie_id}"
8
+ response = requests.get(url)
9
+
10
+ if response.status_code == 200:
11
+ movie_data = response.json()
12
+ if movie_data.get("Response") == "True":
13
+ movie_title = movie_data.get("Title", "Unknown Movie")
14
+ if rating == "N/A":
15
+ return f"You haven't watched '{movie_title}' yet."
16
+ else:
17
+ return f"You rated '{movie_title}' a {rating}/10."
18
+ else:
19
+ return f"Error: {movie_data.get('Error', 'Movie not found.')}"
20
  else:
21
+ return f"Failed to fetch movie details. HTTP Status Code: {response.status_code}"
22
 
23
  # Gradio interface
24
  with gr.Blocks(css="styles.css") as app:
25
  gr.Markdown("# Movie Rating App")
26
+ movie_id = gr.Textbox(label="Movie ID", placeholder="Enter the movie ID (e.g., tt1305806)...")
27
  rating = gr.Slider(
28
  minimum=0,
29
  maximum=10,
 
33
  submit = gr.Button("Submit Rating")
34
  output = gr.Textbox(label="Your Feedback")
35
 
36
+ submit.click(fetch_and_rate_movie, inputs=[movie_id, rating], outputs=output)
37
 
38
  app.launch()