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 "
Please enter a valid training statement.
" # 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"Trained: '{statement}' as {label}
" f"Learned {pos_count} unique positive words " f"and {neg_count} unique negative words so far.
" ) 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 "Please enter a statement to classify.
" 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"""Classification: '{statement}' → {label}
Pos score: {pos_score}, Neg score: {neg_score}
This is a basic 'Bag-of-Words' approach. Real ML uses more sophisticated methods and bigger datasets.
""" 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()