ML / app.py
kevin1911's picture
Update app.py
3337dca verified
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()