Muhaimin60 commited on
Commit
f3992a9
·
verified ·
1 Parent(s): 1346978

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from sympy import symbols, Eq, solve, log, sin, cos, tan, diff, integrate
3
+ from sympy.parsing.sympy_parser import parse_expr
4
+ from transformers import pipeline
5
+
6
+ # Initialize the Hugging Face model for math question answering
7
+ math_model = pipeline("text2text-generation", model="google/t5-small-ssm-nq")
8
+
9
+ def solve_math_problem(problem):
10
+ try:
11
+ # Attempt to parse and solve mathematical expressions
12
+ expr = parse_expr(problem)
13
+ # Solve algebraic equations or expressions
14
+ if isinstance(expr, Eq):
15
+ return solve(expr)
16
+ else:
17
+ return expr
18
+ except:
19
+ # Handle complex cases or when sympy can't solve the problem
20
+ result = math_model(problem)
21
+ return result[0]['generated_text']
22
+
23
+ def solve_trigonometric(problem):
24
+ # Process trigonometric expressions
25
+ try:
26
+ if 'sin' in problem or 'cos' in problem or 'tan' in problem:
27
+ expr = parse_expr(problem)
28
+ return expr
29
+ else:
30
+ return "This doesn't seem to be a trigonometric expression."
31
+ except:
32
+ return "Unable to solve the trigonometric expression."
33
+
34
+ def solve_de_morgan_law(problem):
35
+ # Example function to handle de Morgan's law
36
+ if "not" in problem and "or" in problem or "and" in problem:
37
+ # This is a basic check, for detailed handling we can implement a parser
38
+ return f"De Morgan's transformation for: {problem}."
39
+ return "No de Morgan's law expression found."
40
+
41
+ # Streamlit UI
42
+ st.title("Math Solver App")
43
+
44
+ st.write("This app can solve mathematical problems like algebra, trigonometry, and logic.")
45
+
46
+ question = st.text_input("Enter your math question here:")
47
+
48
+ if question:
49
+ if "log" in question:
50
+ # Handle logarithmic expressions
51
+ result = solve_math_problem(question)
52
+ st.write(f"Logarithmic result: {result}")
53
+ elif "sin" in question or "cos" in question or "tan" in question:
54
+ result = solve_trigonometric(question)
55
+ st.write(f"Trigonometric result: {result}")
56
+ elif "not" in question or "and" in question or "or" in question:
57
+ result = solve_de_morgan_law(question)
58
+ st.write(result)
59
+ else:
60
+ # For algebraic or other mathematical expressions
61
+ result = solve_math_problem(question)
62
+ st.write(f"Result: {result}")