Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
# -----------------------------------------
|
| 5 |
+
# GLOBALS for Bag-of-Words ML Simulation
|
| 6 |
+
# -----------------------------------------
|
| 7 |
+
positive_word_counts = {}
|
| 8 |
+
negative_word_counts = {}
|
| 9 |
+
training_data = []
|
| 10 |
+
|
| 11 |
+
# -----------------------------------------
|
| 12 |
+
# 1) Training Function
|
| 13 |
+
# -----------------------------------------
|
| 14 |
+
def train_model(statement, label):
|
| 15 |
+
"""
|
| 16 |
+
Splits a statement into words, increments counts in
|
| 17 |
+
positive_word_counts or negative_word_counts depending on label.
|
| 18 |
+
Returns HTML feedback showing what's been learned.
|
| 19 |
+
"""
|
| 20 |
+
global positive_word_counts, negative_word_counts, training_data
|
| 21 |
+
|
| 22 |
+
# Basic error check
|
| 23 |
+
statement = statement.strip()
|
| 24 |
+
if not statement:
|
| 25 |
+
return "<p style='color:red;'>Please enter a valid training statement.</p>"
|
| 26 |
+
|
| 27 |
+
# Tokenize by letters only
|
| 28 |
+
words = re.findall(r"[a-zA-Z]+", statement.lower())
|
| 29 |
+
|
| 30 |
+
# Update dictionary
|
| 31 |
+
if label == "Positive":
|
| 32 |
+
for w in words:
|
| 33 |
+
positive_word_counts[w] = positive_word_counts.get(w, 0) + 1
|
| 34 |
+
else:
|
| 35 |
+
for w in words:
|
| 36 |
+
negative_word_counts[w] = negative_word_counts.get(w, 0) + 1
|
| 37 |
+
|
| 38 |
+
training_data.append((statement, label))
|
| 39 |
+
|
| 40 |
+
# Construct feedback
|
| 41 |
+
pos_count = len(positive_word_counts)
|
| 42 |
+
neg_count = len(negative_word_counts)
|
| 43 |
+
response = (
|
| 44 |
+
f"<p><b>Trained:</b> '{statement}' as {label}</p>"
|
| 45 |
+
f"<p>Learned {pos_count} unique positive words "
|
| 46 |
+
f"and {neg_count} unique negative words so far.</p>"
|
| 47 |
+
)
|
| 48 |
+
return response
|
| 49 |
+
|
| 50 |
+
# -----------------------------------------
|
| 51 |
+
# 2) Classification Function
|
| 52 |
+
# -----------------------------------------
|
| 53 |
+
def classify_text(statement):
|
| 54 |
+
"""
|
| 55 |
+
Counts how many times each word appears in positive vs. negative.
|
| 56 |
+
Returns the classification result + an explanation.
|
| 57 |
+
"""
|
| 58 |
+
statement = statement.strip()
|
| 59 |
+
if not statement:
|
| 60 |
+
return "<p style='color:red;'>Please enter a statement to classify.</p>"
|
| 61 |
+
|
| 62 |
+
words = re.findall(r"[a-zA-Z]+", statement.lower())
|
| 63 |
+
pos_score = 0
|
| 64 |
+
neg_score = 0
|
| 65 |
+
|
| 66 |
+
# Accumulate scores
|
| 67 |
+
for w in words:
|
| 68 |
+
pos_score += positive_word_counts.get(w, 0)
|
| 69 |
+
neg_score += negative_word_counts.get(w, 0)
|
| 70 |
+
|
| 71 |
+
# Decide label
|
| 72 |
+
label = "Positive" if pos_score >= neg_score else "Negative"
|
| 73 |
+
|
| 74 |
+
explanation = f"""
|
| 75 |
+
<p><b>Classification:</b> '{statement}' → {label}</p>
|
| 76 |
+
<p>Pos score: {pos_score}, Neg score: {neg_score}</p>
|
| 77 |
+
<h4>How This Simulated ML Works</h4>
|
| 78 |
+
<ul>
|
| 79 |
+
<li>When you train a statement, each word is counted as either positive or negative.</li>
|
| 80 |
+
<li>When classifying, we sum how many times those words appeared in positive vs. negative examples.</li>
|
| 81 |
+
<li>If there are more 'positive' occurrences, we predict Positive; otherwise Negative.</li>
|
| 82 |
+
</ul>
|
| 83 |
+
<p>This is a basic 'Bag-of-Words' approach. Real ML uses more sophisticated methods and bigger datasets.</p>
|
| 84 |
+
"""
|
| 85 |
+
return explanation
|
| 86 |
+
|
| 87 |
+
# -----------------------------------------
|
| 88 |
+
# 3) Gradio Interface
|
| 89 |
+
# -----------------------------------------
|
| 90 |
+
with gr.Blocks() as demo:
|
| 91 |
+
gr.Markdown("## Simulated Machine Learning (Bag-of-Words) Demo")
|
| 92 |
+
gr.Markdown("Train a simple word-count-based model, then classify new statements.")
|
| 93 |
+
|
| 94 |
+
gr.Markdown("### Training Section")
|
| 95 |
+
with gr.Row():
|
| 96 |
+
train_statement_input = gr.Textbox(
|
| 97 |
+
label="Training Statement",
|
| 98 |
+
placeholder="e.g. I love this place"
|
| 99 |
+
)
|
| 100 |
+
train_label_dropdown = gr.Dropdown(
|
| 101 |
+
choices=["Positive", "Negative"],
|
| 102 |
+
value="Positive",
|
| 103 |
+
label="Label"
|
| 104 |
+
)
|
| 105 |
+
train_button = gr.Button("Train Statement")
|
| 106 |
+
train_output = gr.HTML()
|
| 107 |
+
|
| 108 |
+
train_button.click(
|
| 109 |
+
fn=train_model,
|
| 110 |
+
inputs=[train_statement_input, train_label_dropdown],
|
| 111 |
+
outputs=train_output
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
gr.Markdown("### Classification Section")
|
| 115 |
+
with gr.Row():
|
| 116 |
+
classify_statement_input = gr.Textbox(
|
| 117 |
+
label="Test Statement",
|
| 118 |
+
placeholder="e.g. I really love these tacos"
|
| 119 |
+
)
|
| 120 |
+
classify_button = gr.Button("Classify")
|
| 121 |
+
classify_output = gr.HTML()
|
| 122 |
+
|
| 123 |
+
classify_button.click(
|
| 124 |
+
fn=classify_text,
|
| 125 |
+
inputs=[classify_statement_input],
|
| 126 |
+
outputs=classify_output
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
demo.launch()
|