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