File size: 4,297 Bytes
7030d5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3337dca
7030d5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import re

# -----------------------------------------
# GLOBALS for Bag-of-Words ML Simulation
# -----------------------------------------
positive_word_counts = {}
negative_word_counts = {}
training_data = []

# -----------------------------------------
# 1) Training Function
# -----------------------------------------
def train_model(statement, label):
    """
    Splits a statement into words, increments counts in
    positive_word_counts or negative_word_counts depending on label.
    Returns HTML feedback showing what's been learned.
    """
    global positive_word_counts, negative_word_counts, training_data

    # Basic error check
    statement = statement.strip()
    if not statement:
        return "<p style='color:red;'>Please enter a valid training statement.</p>"

    # Tokenize by letters only
    words = re.findall(r"[a-zA-Z]+", statement.lower())

    # Update dictionary
    if label == "Positive":
        for w in words:
            positive_word_counts[w] = positive_word_counts.get(w, 0) + 1
    else:
        for w in words:
            negative_word_counts[w] = negative_word_counts.get(w, 0) + 1

    training_data.append((statement, label))

    # Construct feedback
    pos_count = len(positive_word_counts)
    neg_count = len(negative_word_counts)
    response = (
        f"<p><b>Trained:</b> '{statement}' as {label}</p>"
        f"<p>Learned {pos_count} unique positive words "
        f"and {neg_count} unique negative words so far.</p>"
    )
    return response

# -----------------------------------------
# 2) Classification Function
# -----------------------------------------
def classify_text(statement):
    """
    Counts how many times each word appears in positive vs. negative.
    Returns the classification result + an explanation.
    """
    statement = statement.strip()
    if not statement:
        return "<p style='color:red;'>Please enter a statement to classify.</p>"

    words = re.findall(r"[a-zA-Z]+", statement.lower())
    pos_score = 0
    neg_score = 0

    # Accumulate scores
    for w in words:
        pos_score += positive_word_counts.get(w, 0)
        neg_score += negative_word_counts.get(w, 0)

    # Decide label
    label = "Positive" if pos_score >= neg_score else "Negative"

    explanation = f"""
    <p><b>Classification:</b> '{statement}' → {label}</p>
    <p>Pos score: {pos_score}, Neg score: {neg_score}</p>
    <h4>How This Simulated ML Works</h4>
    <ul>
      <li>When you train a statement, each word is counted as either positive or negative.</li>
      <li>When classifying, we sum how many times those words appeared in positive vs. negative examples.</li>
      <li>If there are more 'positive' occurrences, we predict Positive; otherwise Negative.</li>
    </ul>
    <p>This is a basic 'Bag-of-Words' approach. Real ML uses more sophisticated methods and bigger datasets.</p>
    """
    return explanation

# -----------------------------------------
# 3) Gradio Interface
# -----------------------------------------
with gr.Blocks(css="footer{display:none !important}") as demo:
    gr.Markdown("## Simulated Machine Learning (Bag-of-Words) Demo")
    gr.Markdown("Train a simple word-count-based model, then classify new statements.")

    gr.Markdown("### Training Section")
    with gr.Row():
        train_statement_input = gr.Textbox(
            label="Training Statement",
            placeholder="e.g. I love this place"
        )
        train_label_dropdown = gr.Dropdown(
            choices=["Positive", "Negative"],
            value="Positive",
            label="Label"
        )
        train_button = gr.Button("Train Statement")
    train_output = gr.HTML()

    train_button.click(
        fn=train_model,
        inputs=[train_statement_input, train_label_dropdown],
        outputs=train_output
    )

    gr.Markdown("### Classification Section")
    with gr.Row():
        classify_statement_input = gr.Textbox(
            label="Test Statement",
            placeholder="e.g. I really love these tacos"
        )
        classify_button = gr.Button("Classify")
    classify_output = gr.HTML()

    classify_button.click(
        fn=classify_text,
        inputs=[classify_statement_input],
        outputs=classify_output
    )

demo.launch()