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 "

Please provide a sentence.

" layer1_label, layer2_label, final_label, pos_count, neg_count = deep_classify(text_to_classify) # Build HTML explanation html_output = f"""

Deep Learning Simulation

Input Text: {text_to_classify}

  1. Layer 1 checks positive vs. negative words (simple count).
    Positive words found: {pos_count}, Negative words found: {neg_count}
    Layer 1 Classification: {layer1_label}
  2. Layer 2 checks for sarcasm cues (like "not!", "jk", "/s").
    Result after sarcasm check: {layer2_label}
  3. Layer 3 looks for intensifiers ("very", "extremely", "so", "super", "really").
    Final Classification: {final_label}

How this relates to Deep Learning

Observation: This layered approach can handle nuances like sarcasm or intensifiers better than a single rule or single-layer model.

""" 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()