Health / app.py
Muhaimin60's picture
Create app.py
f3992a9 verified
import streamlit as st
from sympy import symbols, Eq, solve, log, sin, cos, tan, diff, integrate
from sympy.parsing.sympy_parser import parse_expr
from transformers import pipeline
# Initialize the Hugging Face model for math question answering
math_model = pipeline("text2text-generation", model="google/t5-small-ssm-nq")
def solve_math_problem(problem):
try:
# Attempt to parse and solve mathematical expressions
expr = parse_expr(problem)
# Solve algebraic equations or expressions
if isinstance(expr, Eq):
return solve(expr)
else:
return expr
except:
# Handle complex cases or when sympy can't solve the problem
result = math_model(problem)
return result[0]['generated_text']
def solve_trigonometric(problem):
# Process trigonometric expressions
try:
if 'sin' in problem or 'cos' in problem or 'tan' in problem:
expr = parse_expr(problem)
return expr
else:
return "This doesn't seem to be a trigonometric expression."
except:
return "Unable to solve the trigonometric expression."
def solve_de_morgan_law(problem):
# Example function to handle de Morgan's law
if "not" in problem and "or" in problem or "and" in problem:
# This is a basic check, for detailed handling we can implement a parser
return f"De Morgan's transformation for: {problem}."
return "No de Morgan's law expression found."
# Streamlit UI
st.title("Math Solver App")
st.write("This app can solve mathematical problems like algebra, trigonometry, and logic.")
question = st.text_input("Enter your math question here:")
if question:
if "log" in question:
# Handle logarithmic expressions
result = solve_math_problem(question)
st.write(f"Logarithmic result: {result}")
elif "sin" in question or "cos" in question or "tan" in question:
result = solve_trigonometric(question)
st.write(f"Trigonometric result: {result}")
elif "not" in question or "and" in question or "or" in question:
result = solve_de_morgan_law(question)
st.write(result)
else:
# For algebraic or other mathematical expressions
result = solve_math_problem(question)
st.write(f"Result: {result}")