Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import torch
|
4 |
+
|
5 |
+
# Load models and tokenizers
|
6 |
+
sarcasm_tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-v3-base")
|
7 |
+
sarcasm_model = AutoModelForSequenceClassification.from_pretrained("dnzblgn/Sarcasm-Detection-Customer-Reviews")
|
8 |
+
sentiment_tokenizer = AutoTokenizer.from_pretrained("facebook/roberta-base")
|
9 |
+
sentiment_model = AutoModelForSequenceClassification.from_pretrained("dnzblgn/Sentiment-Analysis-Customer-Reviews")
|
10 |
+
|
11 |
+
def process_text_pipeline(user_input):
|
12 |
+
sentences = user_input.split("\n")
|
13 |
+
results = []
|
14 |
+
for sentence in sentences:
|
15 |
+
# Sentiment analysis
|
16 |
+
sentiment_inputs = sentiment_tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
17 |
+
with torch.no_grad():
|
18 |
+
sentiment_outputs = sentiment_model(**sentiment_inputs)
|
19 |
+
sentiment_logits = sentiment_outputs.logits
|
20 |
+
sentiment_class = torch.argmax(sentiment_logits, dim=-1).item()
|
21 |
+
sentiment = "Positive" if sentiment_class == 0 else "Negative"
|
22 |
+
|
23 |
+
# Sarcasm detection for positive sentences
|
24 |
+
if sentiment == "Positive":
|
25 |
+
sarcasm_inputs = sarcasm_tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
26 |
+
with torch.no_grad():
|
27 |
+
sarcasm_outputs = sarcasm_model(**sarcasm_inputs)
|
28 |
+
sarcasm_logits = sarcasm_outputs.logits
|
29 |
+
sarcasm_class = torch.argmax(sarcasm_logits, dim=-1).item()
|
30 |
+
if sarcasm_class == 1: # Sarcasm detected
|
31 |
+
sentiment = "Negative (Sarcasm detected)"
|
32 |
+
|
33 |
+
results.append(f"{sentence}: {sentiment}")
|
34 |
+
return "\n".join(results)
|
35 |
+
|
36 |
+
# Gradio UI
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=process_text_pipeline,
|
39 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter one or more sentences, each on a new line."),
|
40 |
+
outputs="text",
|
41 |
+
title="Sarcasm Detection for Customer Reviews",
|
42 |
+
description="This web app analyzes the sentiment of customer reviews and detects sarcasm for positive reviews.",
|
43 |
+
)
|
44 |
+
|
45 |
+
# Run interface
|
46 |
+
if __name__ == "__main__":
|
47 |
+
interface.launch()
|