Spaces:
Sleeping
Sleeping
File size: 4,985 Bytes
526ae85 48c4a82 9b5bd69 48c4a82 9b5bd69 526ae85 48c4a82 9b5bd69 526ae85 9b5bd69 48c4a82 9b5bd69 526ae85 48c4a82 9b5bd69 526ae85 9b5bd69 526ae85 9b5bd69 48c4a82 9b5bd69 48c4a82 9b5bd69 48c4a82 9b5bd69 526ae85 9b5bd69 526ae85 9b5bd69 526ae85 9b5bd69 526ae85 9b5bd69 526ae85 9b5bd69 48c4a82 9b5bd69 526ae85 9b5bd69 526ae85 48c4a82 9b5bd69 526ae85 9b5bd69 48c4a82 526ae85 48c4a82 526ae85 9b5bd69 526ae85 48c4a82 9b5bd69 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | # import streamlit as st
# import sympy as sp
# import pandas as pd
# st.set_page_config(page_title="Logic Solver", layout="centered")
# st.title("π§© AI Logic Solver")
# st.write("Enter a logical expression using:")
# st.write("~ (NOT), & (AND), | (OR), >> (IMPLIES)")
# # User input
# expr_input = st.text_input("Enter expression (e.g., ~(p & q) >> r):")
# if expr_input:
# try:
# # Define symbols
# p, q, r = sp.symbols('p q r')
# # Convert string to expression
# expr = sp.sympify(expr_input)
# st.subheader("π Simplified Expression:")
# simplified = sp.simplify_logic(expr)
# st.write(simplified)
# # Truth table
# st.subheader("π Truth Table:")
# variables = sorted(expr.free_symbols, key=lambda x: str(x))
# rows = []
# for values in range(2**len(variables)):
# combination = list(map(int, bin(values)[2:].zfill(len(variables))))
# subs = dict(zip(variables, combination))
# result = expr.subs(subs)
# row = {str(var): val for var, val in subs.items()}
# row["Result"] = int(bool(result))
# rows.append(row)
# df = pd.DataFrame(rows)
# st.dataframe(df)
# except Exception as e:
# st.error("β Invalid expression. Please follow correct syntax.")
import streamlit as st
import sympy as sp
import pandas as pd
from sympy.logic.boolalg import Xor
# -----------------------
# Page Setup
# -----------------------
st.set_page_config(page_title="AI Logic Solver", layout="centered")
st.title("π§© AI Logic Solver")
st.markdown("Solve logical expressions easily (with truth table)")
st.info("Use: ~ (NOT), & (AND), | (OR), >> (IMPLIES), ^ (XOR)")
# -----------------------
# Session State
# -----------------------
if "expr" not in st.session_state:
st.session_state.expr = ""
# -----------------------
# BUTTON FUNCTIONS
# -----------------------
def add(val):
st.session_state.expr += val
def clear():
st.session_state.expr = ""
def backspace():
st.session_state.expr = st.session_state.expr[:-1]
# -----------------------
# SIMPLE BUTTON UI
# -----------------------
st.subheader("π Build Expression")
col1, col2, col3 = st.columns(3)
with col1:
if st.button("p"):
add("p")
if st.button("q"):
add("q")
if st.button("r"):
add("r")
with col2:
if st.button("AND (&)"):
add(" & ")
if st.button("OR (|)"):
add(" | ")
if st.button("NOT (~)"):
add("~")
with col3:
if st.button("IMPLIES (>>)"):
add(" >> ")
if st.button("XOR (^)"):
add(" ^ ")
if st.button("( )"):
add("()")
# Extra controls
col4, col5 = st.columns(2)
with col4:
if st.button("β¬
Backspace"):
backspace()
with col5:
if st.button("π Clear"):
clear()
# -----------------------
# INPUT BOX
# -----------------------
expr_input = st.text_input("βοΈ Your Expression:", value=st.session_state.expr)
# -----------------------
# PROCESSING
# -----------------------
if expr_input:
try:
# Clean input
expr_clean = expr_input.strip()
# Check parentheses
if expr_clean.count("(") != expr_clean.count(")"):
st.warning("β οΈ Unbalanced parentheses!")
# Replace XOR
expr_clean = expr_clean.replace("^", "Xor")
# Define symbols
p, q, r = sp.symbols('p q r')
# Parse expression
expr = sp.sympify(expr_clean, locals={"Xor": Xor})
st.success("β
Expression is valid!")
# -----------------------
# SIMPLIFY
# -----------------------
simplified = sp.simplify_logic(expr)
st.subheader("π Simplified Expression:")
st.write(simplified)
# -----------------------
# TRUTH TABLE
# -----------------------
st.subheader("π Truth Table")
variables = sorted(expr.free_symbols, key=lambda x: str(x))
rows = []
for values in range(2**len(variables)):
combo = list(map(int, bin(values)[2:].zfill(len(variables))))
subs = dict(zip(variables, combo))
result = bool(expr.subs(subs)) # stable
row = {str(var): val for var, val in subs.items()}
row["Result"] = int(result)
rows.append(row)
df = pd.DataFrame(rows)
st.dataframe(df, use_container_width=True)
# Download option
st.download_button(
"π₯ Download CSV",
df.to_csv(index=False),
file_name="truth_table.csv",
mime="text/csv"
)
except Exception as e:
st.error("β Invalid expression!")
st.write("π Error detail:")
st.code(str(e))
st.info("π‘ Try examples:")
st.code("~(p & q) >> r")
st.code("p | q")
st.code("p ^ q")
st.code("(p & q) | (r)") |