Sandy-Techie commited on
Commit
3eccb5c
verified
1 Parent(s): b802c79

Created app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as ui
2
+ from transformers import pipeline
3
+
4
+ # 1. Load the sentiment analysis pipeline
5
+ # (Hugging Face Spaces will cache this so it only loads once on startup)
6
+ pipe = pipeline(
7
+ "text-classification", model="tabularisai/multilingual-sentiment-analysis"
8
+ )
9
+
10
+
11
+ # 2. Define the prediction function
12
+ def analyze_sentiment(text):
13
+ if not text.strip():
14
+ return "Please enter some text to analyze."
15
+
16
+ # Run the pipeline
17
+ result = pipe(text)[0]
18
+
19
+ # Extract label and score
20
+ label = result["label"]
21
+ score = result["score"]
22
+
23
+ # Return a cleanly formatted string
24
+ return f"Prediction: {label} | Confidence: {score:.2%}"
25
+
26
+
27
+ # 3. Create the Gradio Interface
28
+ demo = ui.Interface(
29
+ fn=analyze_sentiment,
30
+ inputs=ui.Textbox(
31
+ lines=3, placeholder="Enter text here...", label="Input Text"
32
+ ),
33
+ outputs=ui.Textbox(label="Sentiment Analysis Result"),
34
+ title="Multilingual Sentiment Analysis",
35
+ description="Enter text in various languages to detect the underlying sentiment using the `tabularisai/multilingual-sentiment-analysis` model.",
36
+ examples=[
37
+ ["I love this product! It's amazing and works perfectly."],
38
+ ["Ce produit est terrible, je d茅teste 莽a."],
39
+ ["Este producto es incre铆ble y funciona a la perfecci贸n."],
40
+ ],
41
+ )
42
+
43
+ # 4. Launch the app
44
+ if __name__ == "__main__":
45
+ demo.launch()