Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,40 +8,67 @@ sentiment_model = AutoModelForSequenceClassification.from_pretrained("dnzblgn/Se
|
|
8 |
sarcasm_tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-v3-base", use_fast=False)
|
9 |
sentiment_tokenizer = AutoTokenizer.from_pretrained("FacebookAI/roberta-base", use_fast=False)
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
for sentence in sentences:
|
15 |
-
|
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 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
# Gradio UI
|
37 |
interface = gr.Interface(
|
38 |
-
fn=
|
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
|
43 |
)
|
44 |
|
45 |
-
# Run interface
|
46 |
if __name__ == "__main__":
|
47 |
-
interface.launch()
|
|
|
8 |
sarcasm_tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-v3-base", use_fast=False)
|
9 |
sentiment_tokenizer = AutoTokenizer.from_pretrained("FacebookAI/roberta-base", use_fast=False)
|
10 |
|
11 |
+
# Function to analyze sentiment
|
12 |
+
def analyze_sentiment(sentence):
|
13 |
+
inputs = sentiment_tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
14 |
+
with torch.no_grad():
|
15 |
+
outputs = sentiment_model(**inputs)
|
16 |
+
logits = outputs.logits
|
17 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
18 |
+
sentiment_mapping = {1: "Negative", 0: "Positive"}
|
19 |
+
return sentiment_mapping[predicted_class]
|
20 |
+
|
21 |
+
# Function to detect sarcasm
|
22 |
+
def detect_sarcasm(sentence):
|
23 |
+
inputs = sarcasm_tokenizer(sentence, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
24 |
+
with torch.no_grad():
|
25 |
+
outputs = sarcasm_model(**inputs)
|
26 |
+
logits = outputs.logits
|
27 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
28 |
+
return "Sarcasm" if predicted_class == 1 else "Not Sarcasm"
|
29 |
+
|
30 |
+
# Combined function for text file pipeline
|
31 |
+
def process_text_pipeline(text):
|
32 |
+
sentences = text.split("\n") # Split text into multiple sentences
|
33 |
+
processed_sentences = []
|
34 |
+
|
35 |
for sentence in sentences:
|
36 |
+
sentiment = analyze_sentiment(sentence.strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
if sentiment == "Positive":
|
38 |
+
sarcasm_result = detect_sarcasm(sentence.strip())
|
39 |
+
if sarcasm_result == "Sarcasm":
|
40 |
+
processed_sentences.append(f"'{sentence}' -> Sentiment: Negative (due to sarcasm)")
|
41 |
+
else:
|
42 |
+
processed_sentences.append(f"'{sentence}' -> Sentiment: Positive")
|
43 |
+
else:
|
44 |
+
processed_sentences.append(f"'{sentence}' -> Sentiment: Negative")
|
45 |
+
|
46 |
+
return "\n".join(processed_sentences)
|
47 |
+
|
48 |
+
# Simple user interface for sarcasm detection
|
49 |
+
def sarcasm_detection_interface(input_text):
|
50 |
+
sentences = input_text.split("\n")
|
51 |
+
predictions = []
|
52 |
+
|
53 |
+
for sentence in sentences:
|
54 |
+
sentiment = analyze_sentiment(sentence.strip())
|
55 |
+
if sentiment == "Negative":
|
56 |
+
predictions.append(f"'{sentence}' -> Not Sarcastic (Direct Negative Sentiment)")
|
57 |
+
else:
|
58 |
+
sarcasm_result = detect_sarcasm(sentence.strip())
|
59 |
+
predictions.append(f"'{sentence}' -> {sarcasm_result}")
|
60 |
+
|
61 |
+
return "\n".join(predictions)
|
62 |
|
63 |
# Gradio UI
|
64 |
interface = gr.Interface(
|
65 |
+
fn=sarcasm_detection_interface,
|
66 |
inputs=gr.Textbox(lines=10, placeholder="Enter one or more sentences, each on a new line."),
|
67 |
outputs="text",
|
68 |
title="Sarcasm Detection for Customer Reviews",
|
69 |
+
description="This web app analyzes customer reviews for sentiment and detects sarcasm for positive reviews.",
|
70 |
)
|
71 |
|
72 |
+
# Run the interface
|
73 |
if __name__ == "__main__":
|
74 |
+
interface.launch()
|