File size: 1,721 Bytes
2665a06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# Load model and tokenizer
model_name = "usef310/flan-t5-small-sentiment"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

def predict_sentiment(text):
    '''Predict sentiment of input text'''
    if not text.strip():
        return "Please enter some text!"
    
    # Prepare input
    inputs = tokenizer("sentiment: " + text, return_tensors="pt", max_length=256, truncation=True)
    
    # Generate prediction
    outputs = model.generate(**inputs, max_length=8)
    prediction = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    # Format output
    sentiment = prediction.upper()
    emoji = "😊" if "positive" in prediction.lower() else "😞"
    
    return f"{emoji} {sentiment}"

# Create Gradio interface
demo = gr.Interface(
    fn=predict_sentiment,
    inputs=gr.Textbox(
        lines=5,
        placeholder="Enter a movie review or any text...",
        label="Text Input"
    ),
    outputs=gr.Textbox(label="Sentiment Prediction"),
    title="🎬 FLAN-T5 Sentiment Analysis",
    description="Fine-tuned FLAN-T5-Small for sentiment classification. Enter any text to get positive/negative prediction!",
    examples=[
        ["This movie was absolutely fantastic! I loved every minute of it."],
        ["Terrible film. Complete waste of time and money."],
        ["The acting was superb and the plot kept me engaged throughout."],
        ["I didn't enjoy this movie at all. Very disappointing."],
        ["An incredible masterpiece that everyone should watch!"]
    ],
    theme="soft"
)

if __name__ == "__main__":
    demo.launch()