File size: 919 Bytes
1d68b97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer

# Load model and tokenizer
model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
model = AutoModelForSequenceClassification.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Define pipeline for sentiment analysis
classifier = pipeline('text-classification', model=model, tokenizer=tokenizer)

# Define Gradio interface
def predict_sentiment(text):
    result = classifier(text)[0]
    label = result['label']
    score = result['score']
    return f"Prediction: {label} with confidence {score}"

iface = gr.Interface(fn=predict_sentiment, inputs="text", outputs="text", 
                     title="Sentiment Analysis with Hugging Face and Gradio", 
                     description="Enter text to get sentiment prediction.")

# Launch Gradio interface
iface.launch()