Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
|
| 4 |
+
# Load model and tokenizer
|
| 5 |
+
model_name = "usef310/flan-t5-small-sentiment"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
def predict_sentiment(text):
|
| 10 |
+
'''Predict sentiment of input text'''
|
| 11 |
+
if not text.strip():
|
| 12 |
+
return "Please enter some text!"
|
| 13 |
+
|
| 14 |
+
# Prepare input
|
| 15 |
+
inputs = tokenizer("sentiment: " + text, return_tensors="pt", max_length=256, truncation=True)
|
| 16 |
+
|
| 17 |
+
# Generate prediction
|
| 18 |
+
outputs = model.generate(**inputs, max_length=8)
|
| 19 |
+
prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 20 |
+
|
| 21 |
+
# Format output
|
| 22 |
+
sentiment = prediction.upper()
|
| 23 |
+
emoji = "😊" if "positive" in prediction.lower() else "😞"
|
| 24 |
+
|
| 25 |
+
return f"{emoji} {sentiment}"
|
| 26 |
+
|
| 27 |
+
# Create Gradio interface
|
| 28 |
+
demo = gr.Interface(
|
| 29 |
+
fn=predict_sentiment,
|
| 30 |
+
inputs=gr.Textbox(
|
| 31 |
+
lines=5,
|
| 32 |
+
placeholder="Enter a movie review or any text...",
|
| 33 |
+
label="Text Input"
|
| 34 |
+
),
|
| 35 |
+
outputs=gr.Textbox(label="Sentiment Prediction"),
|
| 36 |
+
title="🎬 FLAN-T5 Sentiment Analysis",
|
| 37 |
+
description="Fine-tuned FLAN-T5-Small for sentiment classification. Enter any text to get positive/negative prediction!",
|
| 38 |
+
examples=[
|
| 39 |
+
["This movie was absolutely fantastic! I loved every minute of it."],
|
| 40 |
+
["Terrible film. Complete waste of time and money."],
|
| 41 |
+
["The acting was superb and the plot kept me engaged throughout."],
|
| 42 |
+
["I didn't enjoy this movie at all. Very disappointing."],
|
| 43 |
+
["An incredible masterpiece that everyone should watch!"]
|
| 44 |
+
],
|
| 45 |
+
theme="soft"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
demo.launch()
|