Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,63 @@
|
|
1 |
-
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
-
# Load
|
5 |
-
|
6 |
-
model = TFAutoModelForSequenceClassification.from_pretrained("raphgonda/FilipinoShopping")
|
7 |
-
|
8 |
-
# Create a pipeline
|
9 |
-
pipe = pipeline("text-classification", model=model, tokenizer=tokenizer, framework="tf")
|
10 |
|
11 |
# Define the sentiment analysis function
|
12 |
def analyze_sentiment(text):
|
13 |
try:
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
17 |
return label, f"{score}%"
|
18 |
except Exception as e:
|
19 |
-
return "Error",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
|
|
29 |
interface.launch()
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
from transformers import pipeline
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Load the Filipino sentiment analysis model
|
6 |
+
pipe = pipeline("text-classification", model="raphgonda/FilipinoShopping")
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Define the sentiment analysis function
|
9 |
def analyze_sentiment(text):
|
10 |
try:
|
11 |
+
# Predict sentiment using the model
|
12 |
+
results = pipe(text)
|
13 |
+
# Extract label and score
|
14 |
+
label = results[0]["label"]
|
15 |
+
score = round(results[0]["score"] * 100, 2) # Convert score to percentage
|
16 |
return label, f"{score}%"
|
17 |
except Exception as e:
|
18 |
+
return "Error", "N/A"
|
19 |
+
|
20 |
+
# Create a Gradio interface with custom UI
|
21 |
+
with gr.Blocks() as interface:
|
22 |
+
gr.Markdown("<h1 style='text-align: center;'>Filipino Sentiment Analysis</h1>")
|
23 |
+
gr.Markdown("<p style='text-align: center;'>Enter text in Filipino to analyze its sentiment.</p>")
|
24 |
+
|
25 |
+
with gr.Row():
|
26 |
+
input_text = gr.Textbox(
|
27 |
+
label="Enter text to analyze its sentiment",
|
28 |
+
placeholder="Type your text here...",
|
29 |
+
)
|
30 |
+
|
31 |
+
with gr.Row():
|
32 |
+
submit_btn = gr.Button("Submit")
|
33 |
+
clear_btn = gr.Button("Clear")
|
34 |
+
|
35 |
+
sentiment_label = gr.Textbox(label="Sentiment Label", interactive=False, visible=True)
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
emotion_score = gr.Textbox(label="Emotion Score", interactive=False)
|
39 |
+
|
40 |
+
examples = gr.Examples(
|
41 |
+
examples=[
|
42 |
+
["Okay ang aesthetic"],
|
43 |
+
["Mabagal ang delivery"],
|
44 |
+
["Napakaganda ng serbisyo!"],
|
45 |
+
["Ang pangit ng produkto."]
|
46 |
+
],
|
47 |
+
inputs=input_text,
|
48 |
+
)
|
49 |
|
50 |
+
# Define the function connection
|
51 |
+
submit_btn.click(
|
52 |
+
analyze_sentiment,
|
53 |
+
inputs=[input_text],
|
54 |
+
outputs=[sentiment_label, emotion_score],
|
55 |
+
)
|
56 |
+
clear_btn.click(
|
57 |
+
lambda: ("", ""),
|
58 |
+
inputs=[],
|
59 |
+
outputs=[sentiment_label, emotion_score],
|
60 |
+
)
|
61 |
|
62 |
+
# Launch the app
|
63 |
interface.launch()
|