raphgonda commited on
Commit
52b004c
·
verified ·
1 Parent(s): 1f444ab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()