Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,15 @@
|
|
1 |
import spacy
|
2 |
-
import
|
3 |
-
import gradio as gr
|
4 |
|
5 |
-
#
|
6 |
-
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
for token in doc:
|
14 |
-
# Look for numbers, mathematical operators, or symbols
|
15 |
-
if token.like_num or token.text in ['+', '-', '=', '*', '/', '^', 'x', 'y']:
|
16 |
-
tokens.append(token.text)
|
17 |
-
|
18 |
-
# Join the tokens to form the mathematical expression
|
19 |
-
expression = " ".join(tokens)
|
20 |
-
|
21 |
-
if "=" in expression:
|
22 |
-
return expression
|
23 |
-
else:
|
24 |
-
return "Could not parse a valid equation from the input."
|
25 |
|
26 |
-
|
27 |
-
def solve_math_question(question):
|
28 |
-
try:
|
29 |
-
expression = extract_expression(question)
|
30 |
-
if "Could not parse" in expression:
|
31 |
-
return expression
|
32 |
-
|
33 |
-
# Separate left and right sides of the equation
|
34 |
-
left_side, right_side = expression.split("=")
|
35 |
-
|
36 |
-
# Create symbolic variables
|
37 |
-
y = sp.Symbol('y') # Assuming we are solving for y (expandable for other variables)
|
38 |
-
|
39 |
-
# SymPy solves the equation
|
40 |
-
solution = sp.solve(sp.Eq(sp.sympify(left_side), sp.sympify(right_side)), y)
|
41 |
-
return f"The solution is: {solution}"
|
42 |
-
|
43 |
-
except Exception as e:
|
44 |
-
return f"Error solving the equation: {str(e)}"
|
45 |
-
|
46 |
-
# Gradio interface
|
47 |
-
def chatbot_interface(question):
|
48 |
-
# Restrict input to mathematical questions only
|
49 |
-
if not any(char.isdigit() for char in question):
|
50 |
-
return "This chatbot only answers questions related to mathematics. Please ask a mathematical question."
|
51 |
-
|
52 |
-
return solve_math_question(question)
|
53 |
-
|
54 |
-
# Define Gradio interface
|
55 |
-
iface = gr.Interface(fn=chatbot_interface,
|
56 |
-
inputs=gr.Textbox(label="Enter your mathematical question:"),
|
57 |
-
outputs=gr.Textbox(label="Answer"),
|
58 |
-
title="Math Chatbot (Open Source)",
|
59 |
-
description="Ask any mathematical question and get an answer. Non-mathematical questions will be restricted.")
|
60 |
-
|
61 |
-
# Run the Gradio app
|
62 |
-
iface.launch()
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|