Spaces:
Runtime error
Runtime error
Update app.py
Browse filesAdding the code line
app.py
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
|
| 3 |
+
|
| 4 |
+
# Load model
|
| 5 |
+
MODEL = "cardiffnlp/twitter-roberta-base-sentiment-latest"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
| 8 |
+
sentiment_model = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
|
| 9 |
+
|
| 10 |
+
# Function for Gradio
|
| 11 |
+
def analyze_sentiment(text):
|
| 12 |
+
result = sentiment_model(text)[0]
|
| 13 |
+
return {
|
| 14 |
+
"Sentiment": result["label"],
|
| 15 |
+
"Confidence": f"{result['score']:.2f}"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Example texts
|
| 19 |
+
examples = [
|
| 20 |
+
["I absolutely love this new phone, the camera is stunning!"],
|
| 21 |
+
["I hate the way this app keeps crashing."],
|
| 22 |
+
["It’s fine, nothing special but not terrible either."],
|
| 23 |
+
]
|
| 24 |
+
# Gradio UI
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=analyze_sentiment,
|
| 27 |
+
inputs=gr.Textbox(lines=3, placeholder="Type a sentence here..."),
|
| 28 |
+
outputs="label",
|
| 29 |
+
examples=examples,
|
| 30 |
+
title="Tweet Sentiment Analyzer",
|
| 31 |
+
description=""
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch()
|