Spaces:
Sleeping
Sleeping
jvroo
commited on
Commit
Β·
f6feac2
1
Parent(s):
76fa04f
Final UI Changes
Browse files
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"
|
| 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 |
-
#
|
| 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 |
-
#
|
| 113 |
custom_css = """
|
| 114 |
body {
|
| 115 |
-
background-color: #
|
| 116 |
-
color: #
|
| 117 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
|
| 120 |
.gr-textbox, .gr-radio {
|
| 121 |
margin-bottom: 20px;
|
| 122 |
-
|
| 123 |
-
padding: 10px;
|
| 124 |
border-radius: 8px;
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
|
| 129 |
.gr-button {
|
| 130 |
-
background-color: #
|
| 131 |
color: white;
|
| 132 |
border: none;
|
| 133 |
-
padding:
|
| 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: #
|
| 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:
|
| 167 |
-
margin-bottom:
|
| 168 |
-
color: #
|
| 169 |
}
|
| 170 |
"""
|
| 171 |
|
| 172 |
# Gradio interface
|
| 173 |
with gr.Blocks(css=custom_css) as demo:
|
| 174 |
-
gr.Markdown("<h1
|
| 175 |
|
| 176 |
-
with gr.
|
| 177 |
-
with gr.
|
| 178 |
movie_input = gr.Textbox(
|
| 179 |
-
label="
|
| 180 |
)
|
| 181 |
-
|
| 182 |
-
with gr.Row():
|
| 183 |
review_input = gr.Textbox(
|
| 184 |
-
label="
|
| 185 |
)
|
| 186 |
-
|
| 187 |
-
with gr.Row():
|
| 188 |
mode_input = gr.Radio(
|
| 189 |
-
["Local Pipeline", "Inference API"], label="
|
| 190 |
)
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
| 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__":
|