jvroo commited on
Commit
f6feac2
Β·
1 Parent(s): 76fa04f

Final UI Changes

Browse files
Files changed (1) hide show
  1. app.py +53 -62
app.py CHANGED
@@ -1,26 +1,23 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  import matplotlib.pyplot as plt
4
- import numpy as np
5
  import requests
6
  from huggingface_hub import InferenceClient
7
  import os
8
 
9
  # Define models for local and remote inference
10
  local_model = "distilbert-base-uncased-finetuned-sst-2-english"
11
- remote_model = "siebert/sentiment-roberta-large-english" # You can use the same model for both for now
12
 
13
  # Load the local sentiment analysis pipeline with the specified model
14
  local_pipeline = pipeline("sentiment-analysis", model=local_model)
15
 
16
  # Initialize the inference client
17
- remote_inference_client = InferenceClient(remote_model)
18
 
19
  # OMDb API key (replace with your own API key)
20
  OMDB_API_URL = 'http://www.omdbapi.com/'
21
- # This is secret on Huggingface
22
- api_key = os.getenv("OMDB")
23
- OMDB_API_KEY = api_key
24
 
25
  # Function to fetch movie information from OMDb API
26
  def fetch_movie_info(movie_name):
@@ -82,13 +79,13 @@ def analyze_sentiment(movie_name, review, mode):
82
 
83
  # Format the sentiment result
84
  result_text = f"Sentiment: {sentiment}, Confidence: {score:.2f}\n{model_info}"
85
-
86
  # Extract movie description
87
  movie_description = movie_info.get('Description', 'N/A')
88
-
89
  # Enhanced plot
90
  fig, ax = plt.subplots(figsize=(8, 5))
91
-
92
  categories = ['POSITIVE', 'NEGATIVE']
93
  sentiment_scores = [score if sentiment == 'POSITIVE' else (1 - score), score if sentiment == 'NEGATIVE' else (1 - score)]
94
  colors = ['#4CAF50', '#F44336']
@@ -109,96 +106,90 @@ def analyze_sentiment(movie_name, review, mode):
109
 
110
  return result_text, movie_description, movie_info, fig # Return the Matplotlib figure directly
111
 
112
- # Custom CSS for styling
113
  custom_css = """
114
  body {
115
- background-color: #2c2f33;
116
- color: #f0f0f0;
117
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  }
119
 
120
  .gr-textbox, .gr-radio {
121
  margin-bottom: 20px;
122
- border: 1px solid #444;
123
- padding: 10px;
124
  border-radius: 8px;
125
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
126
- background-color: #3a3d41;
 
 
127
  }
128
 
129
  .gr-button {
130
- background-color: #7289da;
131
  color: white;
132
  border: none;
133
- padding: 10px 20px;
134
  font-size: 16px;
135
  cursor: pointer;
136
  transition: 0.3s;
137
  border-radius: 8px;
138
  margin-top: 10px;
 
139
  }
140
 
141
  .gr-button:hover {
142
- background-color: #5b6eae;
143
- }
144
-
145
- #component-2 {
146
- font-size: 18px;
147
- margin-bottom: 20px;
148
- }
149
-
150
- #component-3 {
151
- font-size: 18px;
152
- margin-bottom: 20px;
153
- }
154
-
155
- #component-4 {
156
- font-size: 16px;
157
- padding: 15px;
158
- background-color: #3a3d41;
159
- border: 1px solid #444;
160
- border-radius: 8px;
161
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
162
  }
163
 
164
  h1 {
165
  text-align: center;
166
- font-size: 32px;
167
- margin-bottom: 40px;
168
- color: #7289da;
169
  }
170
  """
171
 
172
  # Gradio interface
173
  with gr.Blocks(css=custom_css) as demo:
174
- gr.Markdown("<h1>Movie Review Sentiment Analysis</h1>")
175
 
176
- with gr.Column():
177
- with gr.Row():
178
  movie_input = gr.Textbox(
179
- label="Enter Movie Name", placeholder="Type the movie name here...", lines=1
180
  )
181
-
182
- with gr.Row():
183
  review_input = gr.Textbox(
184
- label="Enter Movie Review", placeholder="Type your movie review here...", lines=4
185
  )
186
-
187
- with gr.Row():
188
  mode_input = gr.Radio(
189
- ["Local Pipeline", "Inference API"], label="Select Processing Mode", value="Inference API"
190
  )
191
-
192
- with gr.Row():
193
- analyze_button = gr.Button("Analyze Sentiment")
194
-
195
- # Output boxes
196
- sentiment_output = gr.Textbox(label="Sentiment Analysis Result", interactive=False)
197
- movie_description_output = gr.Textbox(label="Movie Description", interactive=False)
198
- movie_info_output = gr.JSON(label="Movie Information")
199
- plot_output = gr.Plot(label="Sentiment Score Graph")
200
-
201
- analyze_button.click(analyze_sentiment, [movie_input, review_input, mode_input], [sentiment_output, movie_description_output, movie_info_output, plot_output])
 
 
202
 
203
  # Run the Gradio app
204
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from transformers import pipeline
3
  import matplotlib.pyplot as plt
 
4
  import requests
5
  from huggingface_hub import InferenceClient
6
  import os
7
 
8
  # Define models for local and remote inference
9
  local_model = "distilbert-base-uncased-finetuned-sst-2-english"
10
+ remote_model = "siebert/sentiment-roberta-large-english"
11
 
12
  # Load the local sentiment analysis pipeline with the specified model
13
  local_pipeline = pipeline("sentiment-analysis", model=local_model)
14
 
15
  # Initialize the inference client
16
+ remote_inference_client = InferenceClient(remote_model)
17
 
18
  # OMDb API key (replace with your own API key)
19
  OMDB_API_URL = 'http://www.omdbapi.com/'
20
+ OMDB_API_KEY = os.getenv("OMDB") # Fetching API key from environment variables
 
 
21
 
22
  # Function to fetch movie information from OMDb API
23
  def fetch_movie_info(movie_name):
 
79
 
80
  # Format the sentiment result
81
  result_text = f"Sentiment: {sentiment}, Confidence: {score:.2f}\n{model_info}"
82
+
83
  # Extract movie description
84
  movie_description = movie_info.get('Description', 'N/A')
85
+
86
  # Enhanced plot
87
  fig, ax = plt.subplots(figsize=(8, 5))
88
+
89
  categories = ['POSITIVE', 'NEGATIVE']
90
  sentiment_scores = [score if sentiment == 'POSITIVE' else (1 - score), score if sentiment == 'NEGATIVE' else (1 - score)]
91
  colors = ['#4CAF50', '#F44336']
 
106
 
107
  return result_text, movie_description, movie_info, fig # Return the Matplotlib figure directly
108
 
109
+ # Enhanced CSS for a modern, clean look
110
  custom_css = """
111
  body {
112
+ background-color: #1e1e2f;
113
+ color: #ffffff;
114
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
115
+ margin: 0;
116
+ padding: 0;
117
+ display: flex;
118
+ justify-content: center;
119
+ align-items: center;
120
+ min-height: 100vh;
121
+ }
122
+
123
+ .gradio-container {
124
+ border-radius: 10px;
125
+ background-color: #2c2f48;
126
+ padding: 20px;
127
+ box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
128
  }
129
 
130
  .gr-textbox, .gr-radio {
131
  margin-bottom: 20px;
132
+ padding: 12px;
 
133
  border-radius: 8px;
134
+ border: none;
135
+ box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
136
+ background-color: #3b3e56;
137
+ color: #ffffff;
138
  }
139
 
140
  .gr-button {
141
+ background-color: #4CAF50;
142
  color: white;
143
  border: none;
144
+ padding: 12px 24px;
145
  font-size: 16px;
146
  cursor: pointer;
147
  transition: 0.3s;
148
  border-radius: 8px;
149
  margin-top: 10px;
150
+ box-shadow: 0 5px 10px rgba(0, 0, 0, 0.2);
151
  }
152
 
153
  .gr-button:hover {
154
+ background-color: #388e3c;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
  }
156
 
157
  h1 {
158
  text-align: center;
159
+ font-size: 34px;
160
+ margin-bottom: 20px;
161
+ color: #00bcd4;
162
  }
163
  """
164
 
165
  # Gradio interface
166
  with gr.Blocks(css=custom_css) as demo:
167
+ gr.Markdown("<h1>🎬 Movie Review Sentiment Analysis</h1>")
168
 
169
+ with gr.Row(equal_height=True):
170
+ with gr.Column(scale=1):
171
  movie_input = gr.Textbox(
172
+ label="πŸŽ₯ Movie Name", placeholder="Enter the movie name...", lines=1
173
  )
 
 
174
  review_input = gr.Textbox(
175
+ label="πŸ“ Movie Review", placeholder="Enter your movie review...", lines=4
176
  )
 
 
177
  mode_input = gr.Radio(
178
+ ["Local Pipeline", "Inference API"], label="πŸ” Processing Mode", value="Inference API"
179
  )
180
+ analyze_button = gr.Button("πŸ” Analyze Sentiment")
181
+
182
+ with gr.Column(scale=2):
183
+ sentiment_output = gr.Textbox(label="πŸ—¨οΈ Sentiment Analysis Result", interactive=False)
184
+ movie_description_output = gr.Textbox(label="πŸ“ƒ Movie Description", interactive=False)
185
+ movie_info_output = gr.JSON(label="ℹ️ Movie Information")
186
+ plot_output = gr.Plot(label="πŸ“Š Sentiment Score Graph")
187
+
188
+ analyze_button.click(
189
+ analyze_sentiment,
190
+ [movie_input, review_input, mode_input],
191
+ [sentiment_output, movie_description_output, movie_info_output, plot_output]
192
+ )
193
 
194
  # Run the Gradio app
195
  if __name__ == "__main__":