Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
!pip install gradio --quiet
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import re
|
5 |
+
|
6 |
+
# Global storage: user-defined rule + classification history
|
7 |
+
positive_keywords = []
|
8 |
+
classification_history = []
|
9 |
+
|
10 |
+
def parse_rule(rule_text):
|
11 |
+
"""
|
12 |
+
Parse the comma-separated string of positive words, e.g. "amazing, good".
|
13 |
+
Store them in a global list as lowercase.
|
14 |
+
"""
|
15 |
+
global positive_keywords
|
16 |
+
|
17 |
+
if rule_text.strip():
|
18 |
+
words = [w.strip().lower() for w in rule_text.split(',')]
|
19 |
+
positive_keywords = words
|
20 |
+
else:
|
21 |
+
positive_keywords = []
|
22 |
+
|
23 |
+
def rule_based_classify(text, positive_words):
|
24 |
+
"""
|
25 |
+
If the text contains ANY of the words in positive_words,
|
26 |
+
classify as Positive; otherwise Negative.
|
27 |
+
"""
|
28 |
+
lowered = text.lower()
|
29 |
+
for word in positive_words:
|
30 |
+
if word in lowered:
|
31 |
+
return "Positive"
|
32 |
+
return "Negative"
|
33 |
+
|
34 |
+
def classify_with_rule(rule_input, statement):
|
35 |
+
"""
|
36 |
+
1) Parse the user-defined rule (comma-separated keywords).
|
37 |
+
2) Classify the statement with the updated rule.
|
38 |
+
3) Append to classification history and return an HTML table.
|
39 |
+
"""
|
40 |
+
global classification_history
|
41 |
+
|
42 |
+
# 1) Update our global 'positive_keywords'
|
43 |
+
parse_rule(rule_input)
|
44 |
+
|
45 |
+
# 2) Classify the user's statement
|
46 |
+
if statement.strip():
|
47 |
+
label = rule_based_classify(statement, positive_keywords)
|
48 |
+
classification_history.append((statement, label))
|
49 |
+
else:
|
50 |
+
# If no statement was provided, do nothing new
|
51 |
+
label = None
|
52 |
+
|
53 |
+
# Build an HTML table of the classification history
|
54 |
+
html_table = """
|
55 |
+
<table style="border-collapse: collapse;">
|
56 |
+
<tr>
|
57 |
+
<th style="border:1px solid #ccc; padding:8px;">Statement</th>
|
58 |
+
<th style="border:1px solid #ccc; padding:8px;">Classification</th>
|
59 |
+
</tr>
|
60 |
+
"""
|
61 |
+
for stmt, cls in classification_history:
|
62 |
+
color = "green" if cls == "Positive" else "red"
|
63 |
+
html_table += f"""
|
64 |
+
<tr>
|
65 |
+
<td style="border:1px solid #ccc; padding:8px;">{stmt}</td>
|
66 |
+
<td style="border:1px solid #ccc; padding:8px; color:{color};"><b>{cls}</b></td>
|
67 |
+
</tr>
|
68 |
+
"""
|
69 |
+
html_table += "</table>"
|
70 |
+
|
71 |
+
explanation = """
|
72 |
+
<br>
|
73 |
+
<h4>Discussion (Rule-Based AI):</h4>
|
74 |
+
<ul>
|
75 |
+
<li><b>Pros:</b> You decide the keywords. If they're present, it's "Positive." Otherwise "Negative."</li>
|
76 |
+
<li><b>Cons:</b> If your sentence doesn't contain <i>exactly</i> those keywords, it gets classified incorrectly.
|
77 |
+
Real-world language has many synonyms and nuances that this rule won't catch.</li>
|
78 |
+
</ul>
|
79 |
+
"""
|
80 |
+
|
81 |
+
# Return the combined HTML
|
82 |
+
return html_table + explanation
|
83 |
+
|
84 |
+
# Create a Gradio interface
|
85 |
+
demo = gr.Interface(
|
86 |
+
fn=classify_with_rule, # function to call
|
87 |
+
inputs=[
|
88 |
+
gr.Textbox(label="Define Rule (comma-separated positive words)", lines=1),
|
89 |
+
gr.Textbox(label="Statement to Classify", lines=2)
|
90 |
+
],
|
91 |
+
outputs=gr.HTML(label="Classification History"),
|
92 |
+
title="Traditional Rule-Based AI Demo",
|
93 |
+
description=(
|
94 |
+
"1) In the first box, type comma-separated words you consider 'positive'.\n"
|
95 |
+
"2) In the second box, type a statement to classify.\n"
|
96 |
+
"Click 'Submit' to see how the statement is labeled.\n\n"
|
97 |
+
"You'll see a growing table of all statements you've classified so far."
|
98 |
+
)
|
99 |
+
)
|
100 |
+
|
101 |
+
# Launch the Gradio app
|
102 |
+
demo.launch()
|