akazmi commited on
Commit
eb4f52c
·
verified ·
1 Parent(s): c1dce33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -12
app.py CHANGED
@@ -1,15 +1,45 @@
1
- import spacy
2
- import os
 
3
 
4
- # Ensure model is downloaded before usage
5
- def load_spacy_model():
6
- if not os.path.exists("en_core_web_sm"):
7
- os.system("python -m spacy download en_core_web_sm")
8
- return spacy.load("en_core_web_sm")
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Cache the model to avoid reloading on every request
11
- @st.cache_resource
12
- def get_nlp_model():
13
- return load_spacy_model()
14
 
15
- nlp = get_nlp_model()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()