File size: 5,741 Bytes
46dc99d d02661f 0dc8345 46dc99d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
import gradio as gr
import re
# ---------- Dictionaries of words for each "layer" ----------
# Layer 1: basic positive/negative detection
positive_words = {"love", "great", "good", "amazing", "awesome", "happy"}
negative_words = {"hate", "terrible", "awful", "bad", "horrible", "sad"}
# Layer 2: sarcasm or contradictory cues
sarcasm_cues = {"not!", "jk", "/s"} # e.g. "The food was great, not!"
# Layer 3: intensifiers
intensifiers = {"very", "extremely", "so", "super", "really"}
# ----------------------------------------------------------------
# 1) Classification Layers
# ----------------------------------------------------------------
def layer_1_basic(text):
"""
Layer 1:
- Count how many known positive words vs. negative words.
- If pos >= neg, label = "Positive", else "Negative"
"""
words = re.findall(r"[a-zA-Z]+", text.lower())
pos_count = sum(1 for w in words if w in positive_words)
neg_count = sum(1 for w in words if w in negative_words)
label = "Positive" if pos_count >= neg_count else "Negative"
return label, pos_count, neg_count
def layer_2_sarcasm_or_flip(text, prev_label):
"""
Layer 2:
- Check for sarcasm cues (like "not!", "jk", "/s").
- If found, flip the classification from Layer 1.
"""
text_lower = text.lower()
if any(cue in text_lower for cue in sarcasm_cues):
flipped = "Positive" if prev_label == "Negative" else "Negative"
return flipped + " (sarcasm-corrected)"
else:
return prev_label
def layer_3_intensifiers(text, prev_label):
"""
Layer 3:
- If there's an intensifier ("very", "extremely", "so", "super", "really"),
we add "(HIGH CONFIDENCE)" to the label.
"""
text_lower = text.lower()
if any(i in text_lower for i in intensifiers):
return prev_label + " (HIGH CONFIDENCE)"
else:
return prev_label
def deep_classify(text):
"""
Pass text through 3 layers, returning a final classification
plus details about each layer.
"""
# LAYER 1
layer1_label, pos_count, neg_count = layer_1_basic(text)
# LAYER 2
layer2_label = layer_2_sarcasm_or_flip(text, layer1_label)
# LAYER 3
final_label = layer_3_intensifiers(text, layer2_label)
return layer1_label, layer2_label, final_label, pos_count, neg_count
# ----------------------------------------------------------------
# 2) Gradio Demo
# ----------------------------------------------------------------
# Some sample statements from your original dropdown
sample_statements = [
"I love this movie!",
"This place is awful.",
"The food was amazing, not!",
"I'm so happy right now.",
"Everything was terrible... JK!",
"I really hate slow service.",
"This is so good, I'm super impressed.",
]
def simulate_layers(sample_choice, custom_text):
"""
1) If the user typed something in 'custom_text', use that.
2) Otherwise, use the selected 'sample_choice'.
3) Run the text through the 3-layer classification.
4) Return an HTML block describing each layer's result.
"""
text_to_classify = ""
if custom_text.strip():
text_to_classify = custom_text.strip()
elif sample_choice:
text_to_classify = sample_choice
else:
return "<p style='color:red;'>Please provide a sentence.</p>"
layer1_label, layer2_label, final_label, pos_count, neg_count = deep_classify(text_to_classify)
# Build HTML explanation
html_output = f"""
<h3>Deep Learning Simulation</h3>
<p><b>Input Text:</b> {text_to_classify}</p>
<ol>
<li><b>Layer 1</b> checks positive vs. negative words (simple count).
<br>Positive words found: {pos_count}, Negative words found: {neg_count}
<br>Layer 1 Classification: <i>{layer1_label}</i>
</li>
<li><b>Layer 2</b> checks for sarcasm cues (like "not!", "jk", "/s").
<br>Result after sarcasm check: <i>{layer2_label}</i>
</li>
<li><b>Layer 3</b> looks for intensifiers ("very", "extremely", "so", "super", "really").
<br>Final Classification: <span style="color:blue; font-weight:bold;">{final_label}</span>
</li>
</ol>
<h4>How this relates to Deep Learning</h4>
<ul>
<li>In actual deep learning, each layer learns more complex
<i>features</i> automatically from data, rather than
using manually defined checks.</li>
<li>However, the <b>concept</b> of passing through multiple
layers to refine the result <i>is</i> how neural networks work.</li>
</ul>
<p><b>Observation:</b> This layered approach can handle nuances
like sarcasm or intensifiers better than a single rule or single-layer model.</p>
"""
return html_output
with gr.Blocks(css="footer{display:none !important}") as demo:
gr.Markdown("Deep Learning (Layered Approach) Simulation")
gr.Markdown("Pick a sample statement **or** type your own, then click **Simulate**.")
with gr.Row():
sample_choice = gr.Dropdown(
label="Sample Statement",
choices=["(None)"] + sample_statements,
value="(None)",
)
custom_text = gr.Textbox(
label="Custom Text",
placeholder="Type your own statement here..."
)
simulate_button = gr.Button("Simulate Layers")
output = gr.HTML()
def simulate_wrapper(smpl, cstm):
# If the user picks "(None)", interpret that as an empty choice
if smpl == "(None)":
smpl = ""
return simulate_layers(smpl, cstm)
simulate_button.click(
fn=simulate_wrapper,
inputs=[sample_choice, custom_text],
outputs=[output]
)
demo.launch()
|