quocviethere
commited on
Commit
β’
7a1c2c4
1
Parent(s):
33759e3
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,72 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def analyze_sentiment(text):
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
#
|
|
|
|
|
16 |
iface = gr.Interface(
|
17 |
fn=analyze_sentiment,
|
18 |
inputs=gr.Textbox(
|
19 |
lines=5,
|
20 |
-
placeholder="Enter a review here...",
|
21 |
-
label="Review"
|
22 |
),
|
23 |
outputs=[
|
24 |
gr.Textbox(label="Sentiment"),
|
25 |
gr.Textbox(label="Confidence")
|
26 |
],
|
27 |
-
title="Sentiment Analysis",
|
28 |
description="Analyze the sentiment of movie reviews using a fine-tuned RoBERTa model.",
|
29 |
examples=[
|
30 |
["I loved the cinematography and the story was captivating."],
|
@@ -33,5 +75,7 @@ iface = gr.Interface(
|
|
33 |
theme="default"
|
34 |
)
|
35 |
|
36 |
-
#
|
37 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
# -----------------------------
|
6 |
+
# Configuration Section
|
7 |
+
# -----------------------------
|
8 |
+
MODEL_NAME = "quocviethere/imdb-roberta" # Replace with your actual model ID
|
9 |
|
10 |
+
# -----------------------------
|
11 |
+
# Model Loading Section
|
12 |
+
# -----------------------------
|
13 |
+
try:
|
14 |
+
# Load tokenizer and model from Hugging Face Hub
|
15 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
16 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
17 |
+
|
18 |
+
# Initialize the sentiment analysis pipeline
|
19 |
+
sentiment_pipeline = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
20 |
+
|
21 |
+
# Verify label mapping
|
22 |
+
label_mapping = model.config.id2label
|
23 |
+
print(f"Model label mapping: {label_mapping}")
|
24 |
+
except Exception as e:
|
25 |
+
print(f"Error loading model: {e}")
|
26 |
+
raise
|
27 |
+
|
28 |
+
# -----------------------------
|
29 |
+
# Sentiment Analysis Function
|
30 |
+
# -----------------------------
|
31 |
def analyze_sentiment(text):
|
32 |
+
try:
|
33 |
+
# Perform sentiment analysis
|
34 |
+
result = sentiment_pipeline(text)[0]
|
35 |
+
|
36 |
+
# Extract label and score
|
37 |
+
label = result['label']
|
38 |
+
score = result['score']
|
39 |
+
|
40 |
+
# Map label to sentiment
|
41 |
+
if label in label_mapping.values():
|
42 |
+
sentiment = "Positive π" if label == "POSITIVE" else "Negative π"
|
43 |
+
else:
|
44 |
+
# Handle unexpected labels
|
45 |
+
sentiment = label
|
46 |
+
print(f"Unexpected label received: {label}")
|
47 |
+
|
48 |
+
confidence = f"Confidence: {round(score * 100, 2)}%"
|
49 |
+
|
50 |
+
return sentiment, confidence
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Error during sentiment analysis: {e}")
|
53 |
+
return "Error", "Could not process the input."
|
54 |
|
55 |
+
# -----------------------------
|
56 |
+
# Gradio Interface Section
|
57 |
+
# -----------------------------
|
58 |
iface = gr.Interface(
|
59 |
fn=analyze_sentiment,
|
60 |
inputs=gr.Textbox(
|
61 |
lines=5,
|
62 |
+
placeholder="Enter a movie review here...",
|
63 |
+
label="Movie Review"
|
64 |
),
|
65 |
outputs=[
|
66 |
gr.Textbox(label="Sentiment"),
|
67 |
gr.Textbox(label="Confidence")
|
68 |
],
|
69 |
+
title="IMDb Sentiment Analysis with RoBERTa",
|
70 |
description="Analyze the sentiment of movie reviews using a fine-tuned RoBERTa model.",
|
71 |
examples=[
|
72 |
["I loved the cinematography and the story was captivating."],
|
|
|
75 |
theme="default"
|
76 |
)
|
77 |
|
78 |
+
# -----------------------------
|
79 |
+
# Launch the Interface
|
80 |
+
# -----------------------------
|
81 |
+
iface.launch()
|