akazmi commited on
Commit
764f276
1 Parent(s): 874a942

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -36
app.py CHANGED
@@ -1,45 +1,50 @@
1
- import re
 
2
  import sympy as sp
3
- import gradio as gr
4
 
5
- # Function to evaluate the mathematical question
6
- def evaluate_math_question(question):
7
- try:
8
- # Remove non-mathematical content and keep equations
9
- equation = re.sub(r'[^\d\w\-\+\*/\=\^\.\s]', '', question)
10
-
11
- # Solve the equation using sympy
12
- if "=" in equation:
13
- lhs, rhs = equation.split("=")
14
- equation = sp.Eq(sp.sympify(lhs), sp.sympify(rhs))
15
- solution = sp.solve(equation)
16
- else:
17
- # Just evaluate the expression
18
- solution = sp.sympify(equation)
19
-
20
- return f"The solution is: {solution}"
21
 
22
- except Exception as e:
23
- return f"Error solving the equation: {str(e)}"
24
 
25
- # Function to restrict non-mathematical questions
26
  def is_math_question(question):
27
- if any(op in question for op in "+-*/="):
 
28
  return True
29
- return False
 
30
 
31
- def math_chatbot(question):
32
- if is_math_question(question):
33
- return evaluate_math_question(question)
34
- else:
35
- return "This chatbot only answers questions related to mathematics. Please ask a mathematical question."
 
 
 
 
 
 
 
36
 
37
- # Create the Gradio interface
38
- iface = gr.Interface(fn=math_chatbot,
39
- inputs=gr.Textbox(label="Enter your mathematical question"),
40
- outputs="text",
41
- title="Math Chatbot (Open Source)",
42
- description="Ask any mathematical question and get an answer. Non-mathematical questions will be restricted.")
43
 
44
- # Launch the Gradio app
45
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
  import sympy as sp
 
4
 
5
+ # Cache the model so it's loaded only once
6
+ @st.cache_resource
7
+ def load_model():
8
+ # Load an open-source Hugging Face model for natural language processing
9
+ return pipeline('text-classification', model='mrm8488/t5-base-finetuned-summarize-news')
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # Initialize the model
12
+ nlp_model = load_model()
13
 
14
+ # Function to check if the question is mathematical
15
  def is_math_question(question):
16
+ try:
17
+ parsed_expr = sp.sympify(question)
18
  return True
19
+ except (sp.SympifyError, SyntaxError):
20
+ return False
21
 
22
+ # Function to solve mathematical questions using SymPy
23
+ def solve_math_question(question):
24
+ try:
25
+ # Parse and solve the mathematical expression
26
+ solution = sp.solve(sp.sympify(question))
27
+ return f"The solution is: {solution}"
28
+ except Exception as e:
29
+ return f"Error solving the equation: {e}"
30
+
31
+ # Streamlit UI
32
+ st.title("Math Chatbot (Open Source)")
33
+ st.write("Ask any mathematical question and get an answer. Non-mathematical questions will be restricted.")
34
 
35
+ # User input
36
+ question = st.text_input("Enter your mathematical question:")
 
 
 
 
37
 
38
+ # Processing the input
39
+ if st.button("Submit"):
40
+ if is_math_question(question):
41
+ # Solve the mathematical question
42
+ answer = solve_math_question(question)
43
+ st.write(f"Answer: {answer}")
44
+ else:
45
+ # Filter non-mathematical questions using NLP model
46
+ nlp_result = nlp_model(question)[0]
47
+ if nlp_result['label'] == 'Math':
48
+ st.write("Answer: Processing your math question...")
49
+ else:
50
+ st.write("This chatbot only answers questions related to mathematics. Please ask a mathematical question.")