raphgonda commited on
Commit
766540a
·
verified ·
1 Parent(s): ec2582a

Update app.py

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