syeda-Rija20 commited on
Commit
526ae85
Β·
verified Β·
1 Parent(s): f46b89b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -14
app.py CHANGED
@@ -1,30 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import sympy as sp
3
  import pandas as pd
4
 
5
- st.set_page_config(page_title="Logic Solver", layout="centered")
 
6
 
 
7
  st.title("🧩 AI Logic Solver")
8
- st.write("Enter a logical expression using:")
9
- st.write("~ (NOT), & (AND), | (OR), >> (IMPLIES)")
 
 
 
 
 
10
 
11
- # User input
12
- expr_input = st.text_input("Enter expression (e.g., ~(p & q) >> r):")
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  if expr_input:
15
  try:
16
  # Define symbols
17
  p, q, r = sp.symbols('p q r')
18
 
19
- # Convert string to expression
20
  expr = sp.sympify(expr_input)
21
 
22
- st.subheader("πŸ“Œ Simplified Expression:")
23
- simplified = sp.simplify_logic(expr)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  st.write(simplified)
25
 
26
- # Truth table
27
- st.subheader("πŸ“Š Truth Table:")
 
 
28
 
29
  variables = sorted(expr.free_symbols, key=lambda x: str(x))
30
  rows = []
@@ -32,14 +136,24 @@ if expr_input:
32
  for values in range(2**len(variables)):
33
  combination = list(map(int, bin(values)[2:].zfill(len(variables))))
34
  subs = dict(zip(variables, combination))
35
- result = expr.subs(subs)
 
36
 
37
  row = {str(var): val for var, val in subs.items()}
38
- row["Result"] = int(bool(result))
39
  rows.append(row)
40
 
41
  df = pd.DataFrame(rows)
42
- st.dataframe(df)
 
 
 
 
 
 
 
 
43
 
44
  except Exception as e:
45
- st.error("❌ Invalid expression. Please follow correct syntax.")
 
 
1
+ # import streamlit as st
2
+ # import sympy as sp
3
+ # import pandas as pd
4
+
5
+ # st.set_page_config(page_title="Logic Solver", layout="centered")
6
+
7
+ # st.title("🧩 AI Logic Solver")
8
+ # st.write("Enter a logical expression using:")
9
+ # st.write("~ (NOT), & (AND), | (OR), >> (IMPLIES)")
10
+
11
+ # # User input
12
+ # expr_input = st.text_input("Enter expression (e.g., ~(p & q) >> r):")
13
+
14
+ # if expr_input:
15
+ # try:
16
+ # # Define symbols
17
+ # p, q, r = sp.symbols('p q r')
18
+
19
+ # # Convert string to expression
20
+ # expr = sp.sympify(expr_input)
21
+
22
+ # st.subheader("πŸ“Œ Simplified Expression:")
23
+ # simplified = sp.simplify_logic(expr)
24
+ # st.write(simplified)
25
+
26
+ # # Truth table
27
+ # st.subheader("πŸ“Š Truth Table:")
28
+
29
+ # variables = sorted(expr.free_symbols, key=lambda x: str(x))
30
+ # rows = []
31
+
32
+ # for values in range(2**len(variables)):
33
+ # combination = list(map(int, bin(values)[2:].zfill(len(variables))))
34
+ # subs = dict(zip(variables, combination))
35
+ # result = expr.subs(subs)
36
+
37
+ # row = {str(var): val for var, val in subs.items()}
38
+ # row["Result"] = int(bool(result))
39
+ # rows.append(row)
40
+
41
+ # df = pd.DataFrame(rows)
42
+ # st.dataframe(df)
43
+
44
+ # except Exception as e:
45
+ # st.error("❌ Invalid expression. Please follow correct syntax.")
46
  import streamlit as st
47
  import sympy as sp
48
  import pandas as pd
49
 
50
+ # Page config
51
+ st.set_page_config(page_title="AI Logic Solver", layout="centered")
52
 
53
+ # Title
54
  st.title("🧩 AI Logic Solver")
55
+ st.markdown("Solve logical expressions with **steps + truth table**")
56
+
57
+ # Instructions
58
+ st.info("Use: ~ (NOT), & (AND), | (OR), >> (IMPLIES)")
59
+
60
+ # Quick input buttons
61
+ col1, col2, col3, col4 = st.columns(4)
62
 
63
+ if "expr" not in st.session_state:
64
+ st.session_state.expr = ""
65
 
66
+ if col1.button("p"):
67
+ st.session_state.expr += "p"
68
+ if col2.button("q"):
69
+ st.session_state.expr += "q"
70
+ if col3.button("r"):
71
+ st.session_state.expr += "r"
72
+ if col4.button("Clear"):
73
+ st.session_state.expr = ""
74
+
75
+ col5, col6, col7, col8 = st.columns(4)
76
+
77
+ if col5.button("~"):
78
+ st.session_state.expr += "~"
79
+ if col6.button("&"):
80
+ st.session_state.expr += " & "
81
+ if col7.button("|"):
82
+ st.session_state.expr += " | "
83
+ if col8.button(">>"):
84
+ st.session_state.expr += " >> "
85
+
86
+ # Input box
87
+ expr_input = st.text_input("Enter expression:", value=st.session_state.expr)
88
+
89
+ # Process
90
  if expr_input:
91
  try:
92
  # Define symbols
93
  p, q, r = sp.symbols('p q r')
94
 
95
+ # Convert expression
96
  expr = sp.sympify(expr_input)
97
 
98
+ st.success("βœ… Expression parsed successfully!")
99
+
100
+ # =========================
101
+ # STEP-BY-STEP (Basic)
102
+ # =========================
103
+ st.subheader("🧠 Step-by-Step Simplification")
104
+
105
+ steps = []
106
+
107
+ # Step 1: Original
108
+ steps.append(f"Original: {expr}")
109
+
110
+ # Step 2: Remove implication
111
+ expr_no_impl = expr.replace(
112
+ lambda x: isinstance(x, sp.Implies),
113
+ lambda x: ~x.args[0] | x.args[1]
114
+ )
115
+ steps.append(f"Remove implication: {expr_no_impl}")
116
+
117
+ # Step 3: Simplify
118
+ simplified = sp.simplify_logic(expr_no_impl)
119
+ steps.append(f"Simplified: {simplified}")
120
+
121
+ for step in steps:
122
+ st.write("➑️", step)
123
+
124
+ # Final simplified
125
+ st.subheader("πŸ“Œ Final Simplified Expression:")
126
  st.write(simplified)
127
 
128
+ # =========================
129
+ # TRUTH TABLE
130
+ # =========================
131
+ st.subheader("πŸ“Š Truth Table")
132
 
133
  variables = sorted(expr.free_symbols, key=lambda x: str(x))
134
  rows = []
 
136
  for values in range(2**len(variables)):
137
  combination = list(map(int, bin(values)[2:].zfill(len(variables))))
138
  subs = dict(zip(variables, combination))
139
+
140
+ result = bool(expr.subs(subs)) # βœ… FIXED
141
 
142
  row = {str(var): val for var, val in subs.items()}
143
+ row["Result"] = int(result)
144
  rows.append(row)
145
 
146
  df = pd.DataFrame(rows)
147
+ st.dataframe(df, use_container_width=True)
148
+
149
+ # Download option
150
+ st.download_button(
151
+ "πŸ“₯ Download Truth Table",
152
+ df.to_csv(index=False),
153
+ file_name="truth_table.csv",
154
+ mime="text/csv"
155
+ )
156
 
157
  except Exception as e:
158
+ st.error(f"❌ Invalid expression: {str(e)}")
159
+ st.info("πŸ’‘ Example: ~(p & q) >> r")