Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,45 @@
|
|
1 |
-
import
|
2 |
-
import
|
|
|
3 |
|
4 |
-
#
|
5 |
-
def
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
def get_nlp_model():
|
13 |
-
return load_spacy_model()
|
14 |
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|