Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| # Load models and tokenizers | |
| sarcasm_model = AutoModelForSequenceClassification.from_pretrained("dnzblgn/Sarcasm-Detection-Customer-Reviews") | |
| sentiment_model = AutoModelForSequenceClassification.from_pretrained("dnzblgn/Sentiment-Analysis-Customer-Reviews") | |
| sarcasm_tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-v3-base", use_fast=False) | |
| sentiment_tokenizer = AutoTokenizer.from_pretrained("FacebookAI/roberta-base", use_fast=False) | |
| def process_text_pipeline(user_input): | |
| sentences = user_input.split("\n") | |
| results = [] | |
| for sentence in sentences: | |
| # Sentiment analysis | |
| sentiment_inputs = sentiment_tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=512) | |
| with torch.no_grad(): | |
| sentiment_outputs = sentiment_model(**sentiment_inputs) | |
| sentiment_logits = sentiment_outputs.logits | |
| sentiment_class = torch.argmax(sentiment_logits, dim=-1).item() | |
| sentiment = "Positive" if sentiment_class == 0 else "Negative" | |
| # Sarcasm detection for positive sentences | |
| if sentiment == "Positive": | |
| sarcasm_inputs = sarcasm_tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=512) | |
| with torch.no_grad(): | |
| sarcasm_outputs = sarcasm_model(**sarcasm_inputs) | |
| sarcasm_logits = sarcasm_outputs.logits | |
| sarcasm_class = torch.argmax(sarcasm_logits, dim=-1).item() | |
| if sarcasm_class == 1: # Sarcasm detected | |
| sentiment = "Negative (Sarcasm detected)" | |
| results.append(f"{sentence}: {sentiment}") | |
| return "\n".join(results) | |
| # Gradio UI | |
| interface = gr.Interface( | |
| fn=process_text_pipeline, | |
| inputs=gr.Textbox(lines=10, placeholder="Enter one or more sentences, each on a new line."), | |
| outputs="text", | |
| title="Sarcasm Detection for Customer Reviews", | |
| description="This web app analyzes the sentiment of customer reviews and detects sarcasm for positive reviews.", | |
| ) | |
| # Run interface | |
| if __name__ == "__main__": | |
| interface.launch() | |