dnzblgn commited on
Commit
635a223
Β·
verified Β·
1 Parent(s): fb35de0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -16
app.py CHANGED
@@ -31,29 +31,61 @@ def detect_sarcasm(sentence):
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 == "Negative":
38
- processed_sentences.append(f"'{sentence}' -> Sentiment: Negative")
39
  else:
40
- sarcasm_result = detect_sarcasm(sentence.strip())
41
  if sarcasm_result == "Sarcasm":
42
- processed_sentences.append(f"'{sentence}' -> Sentiment: Negative (Sarcastic Positive)")
43
  else:
44
- processed_sentences.append(f"'{sentence}' -> Sentiment: Positive")
45
-
46
  return "\n".join(processed_sentences)
47
 
48
- # Gradio UI
49
- interface = gr.Interface(
50
- fn=process_text_pipeline,
51
- inputs=gr.Textbox(lines=10, placeholder="Enter one or more sentences, each on a new line."),
52
- outputs="text",
53
- title="Sarcasm Detection for Customer Reviews",
54
- description="This web app analyzes customer reviews for sentiment and detects sarcasm for positive reviews.",
55
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Run the interface
58
  if __name__ == "__main__":
59
- interface.launch()
 
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
+ sentence = sentence.strip()
37
+ if not sentence:
38
+ continue # Skip empty lines
39
+
40
+ sentiment = analyze_sentiment(sentence)
41
  if sentiment == "Negative":
42
+ processed_sentences.append(f"❌ '{sentence}' -> Sentiment: Negative")
43
  else:
44
+ sarcasm_result = detect_sarcasm(sentence)
45
  if sarcasm_result == "Sarcasm":
46
+ processed_sentences.append(f"⚠️ '{sentence}' -> Sentiment: Negative (Sarcastic Positive)")
47
  else:
48
+ processed_sentences.append(f"βœ… '{sentence}' -> Sentiment: Positive")
49
+
50
  return "\n".join(processed_sentences)
51
 
52
+ # Improved Gradio UI
53
+ with gr.Blocks(css=".gradio-container {background-color: #f3f4f6; color: #333; font-family: 'Arial';}") as interface:
54
+ gr.Markdown(
55
+ """
56
+ <h1 style='text-align: center; font-size: 36px;'>🌟 Sarcasm Detection for Customer Reviews 🌟</h1>
57
+ <p style='text-align: center; font-size: 18px;'>This web app analyzes customer reviews for sentiment and detects sarcasm in positive reviews.</p>
58
+ """
59
+ )
60
+
61
+ with gr.Tab("Text Input"):
62
+ with gr.Row():
63
+ text_input = gr.Textbox(
64
+ lines=10,
65
+ label="Enter Sentences",
66
+ placeholder="Enter one or more sentences, each on a new line."
67
+ )
68
+ result_output = gr.Textbox(label="Results", lines=10, interactive=False)
69
+ analyze_button = gr.Button("πŸ” Analyze", elem_id="analyze-button")
70
+
71
+ analyze_button.click(process_text_pipeline, inputs=text_input, outputs=result_output)
72
+
73
+ with gr.Tab("Upload Text File"):
74
+ file_input = gr.File(label="Upload Text File")
75
+ file_output = gr.Textbox(label="Results", lines=10, interactive=False)
76
+
77
+ def process_file(file):
78
+ text = file.read().decode("utf-8")
79
+ return process_text_pipeline(text)
80
+
81
+ file_input.change(process_file, inputs=file_input, outputs=file_output)
82
+
83
+ gr.Markdown(
84
+ """
85
+ <p style='text-align: center;'>Made with ❀️ by <a href='https://huggingface.co/dnzblgn' target='_blank'>dnzblgn</a></p>
86
+ """
87
+ )
88
 
89
  # Run the interface
90
  if __name__ == "__main__":
91
+ interface.launch()