import gradio as gr from transformers import pipeline # Load domain-specific sentiment pipelines models={ "General":pipeline("sentiment-analysis",model="distilbert-base-uncased-finetuned-sst-2-english"), "Product Reviews":pipeline("sentiment-analysis",model="nlptown/bert-base-multilingual-uncased-sentiment"), "Movie Reviews":pipeline("sentiment-analysis",model="textattack/distilbert-base-uncased-imdb"), "Food/Restaurent":pipeline("sentiment-analysis",model="siebert/sentiment-roberta-large-english"), } # Add label mapping for IMDB model (Movie Reviews) imdb_label_map={ "LABEL_0":"Negative", "LABEL_1":"Positive" } # Define sentiment analysis function def analyze_sentiment(domain,review): if not review.strip(): return "Please enter a review." clf=models[domain] result=clf(review)[0] # Apply mapping only for Movie Reviews domain as it labesl as LABEL_0 and LABEL_1 if domain=="Movie Reviews": result['label']=imdb_label_map[result['label']] return f"Model: {domain}\n\nLabel: {result['label']}\n\nConfidence: {result['score']:.2f}" # Create Gradio interface iface=gr.Interface( fn=analyze_sentiment, inputs=[ gr.Dropdown(list(models.keys()),label="Choose Domain",value="General"), gr.Textbox(lines=4,placeholder="Type a review....",label="Review"), ], outputs=gr.Textbox(label="Prediction"), title="Multi-Domain Sentiment Analysis", description="Pic a domian and analyze sentiment.", allow_flagging="never", ) # Launch the interface iface.launch()