Spaces:
Sleeping
Sleeping
Looking for math functionality
Browse files
app.py
CHANGED
@@ -8,18 +8,8 @@ import json
|
|
8 |
import numpy as np
|
9 |
from sklearn.preprocessing import LabelEncoder
|
10 |
from sklearn.feature_extraction.text import CountVectorizer
|
11 |
-
import os
|
12 |
-
#from flask_wtf.csrf import CSRFProtect
|
13 |
-
|
14 |
-
# Load secret key from environment variable
|
15 |
-
SECRET_KEY = os.getenv('SECRET_KEY', 'pumpkin')
|
16 |
|
17 |
app = Flask(__name__)
|
18 |
-
app.secret_key = SECRET_KEY
|
19 |
-
|
20 |
-
# Enable CSRF protection
|
21 |
-
#csrf = CSRFProtect(app)
|
22 |
-
|
23 |
x, y, z = sm.symbols('x y z')
|
24 |
|
25 |
@app.route('/', methods=['GET', 'POST'])
|
@@ -30,15 +20,55 @@ def index():
|
|
30 |
latex_result = ''
|
31 |
|
32 |
if request.method == 'POST':
|
33 |
-
expression =
|
34 |
operation = request.form['operation']
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
try:
|
37 |
expr = sm.sympify(expression)
|
38 |
-
if operation
|
39 |
-
result =
|
40 |
-
|
41 |
-
result =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
latex_result = sm.latex(result)
|
44 |
|
@@ -106,12 +136,10 @@ def get_response(message):
|
|
106 |
return response
|
107 |
|
108 |
@app.route('/chat', methods=['POST'])
|
109 |
-
#@csrf.exempt # Temporarily disable CSRF for API endpoint (ensure API is secure)
|
110 |
def chat():
|
111 |
user_message = request.json['message']
|
112 |
response = get_response(user_message)
|
113 |
return jsonify({'response': response})
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
-
|
117 |
-
app.run(port=7860, ssl_context=('cert.pem', 'key.pem'), debug=True)
|
|
|
8 |
import numpy as np
|
9 |
from sklearn.preprocessing import LabelEncoder
|
10 |
from sklearn.feature_extraction.text import CountVectorizer
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
13 |
x, y, z = sm.symbols('x y z')
|
14 |
|
15 |
@app.route('/', methods=['GET', 'POST'])
|
|
|
20 |
latex_result = ''
|
21 |
|
22 |
if request.method == 'POST':
|
23 |
+
expression = request.form['expression']
|
24 |
operation = request.form['operation']
|
25 |
|
26 |
+
# Replace e**x with exp(x) to make sure SymPy interprets it correctly
|
27 |
+
expression = re.sub(r'e\*\*([a-zA-Z0-9_]+)', r'exp(\1)', expression)
|
28 |
+
|
29 |
+
# Insert an asterisk between numbers and letters using regex
|
30 |
+
expression = re.sub(r'(\d)([a-zA-Z])', r'\1*\2', expression)
|
31 |
+
|
32 |
try:
|
33 |
expr = sm.sympify(expression)
|
34 |
+
if operation == 'diff':
|
35 |
+
result = sm.diff(expr, x)
|
36 |
+
elif operation == 'expand':
|
37 |
+
result = sm.expand(expr)
|
38 |
+
elif operation == 'solve':
|
39 |
+
result = sm.solve(expr, x)
|
40 |
+
elif operation == 'simplify':
|
41 |
+
result = sm.simplify(expr)
|
42 |
+
elif operation == 'factorize':
|
43 |
+
result = sm.factor(expr)
|
44 |
+
elif operation == 'rationalize':
|
45 |
+
result = sm.rationalize(expr)
|
46 |
+
elif operation == 'diffx':
|
47 |
+
result = sm.diff(expr, x)
|
48 |
+
elif operation == 'diffy':
|
49 |
+
result = sm.diff(expr, y)
|
50 |
+
elif operation == 'diffz':
|
51 |
+
result = sm.diff(expr, z)
|
52 |
+
elif operation == 'integratex':
|
53 |
+
# Perform the integration
|
54 |
+
integral_result = sm.integrate(expr, x)
|
55 |
+
# Format the result with C after the variables (if not empty)
|
56 |
+
if integral_result != 0:
|
57 |
+
result = sm.sympify(f"{integral_result} + C")
|
58 |
+
else:
|
59 |
+
result = "C" # If the integral is zero, just return C
|
60 |
+
elif operation == 'integratey':
|
61 |
+
integral_result = sm.integrate(expr, y)
|
62 |
+
if integral_result != 0:
|
63 |
+
result = sm.sympify(f"{integral_result} + C")
|
64 |
+
else:
|
65 |
+
result = "C"
|
66 |
+
elif operation == 'integratez':
|
67 |
+
integral_result = sm.integrate(expr, z)
|
68 |
+
if integral_result != 0:
|
69 |
+
result = sm.sympify(f"{integral_result} + C")
|
70 |
+
else:
|
71 |
+
result = "C"
|
72 |
|
73 |
latex_result = sm.latex(result)
|
74 |
|
|
|
136 |
return response
|
137 |
|
138 |
@app.route('/chat', methods=['POST'])
|
|
|
139 |
def chat():
|
140 |
user_message = request.json['message']
|
141 |
response = get_response(user_message)
|
142 |
return jsonify({'response': response})
|
143 |
|
144 |
if __name__ == "__main__":
|
145 |
+
app.run(port=7860, debug=True)
|
|