Spaces:
Sleeping
Sleeping
File size: 1,795 Bytes
4bd99f3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | from backend.solver import LinearSolverEngine
from backend.linear_graph import LinearGraphPlotter
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/ask", methods=["POST"])
def ask():
question = request.form.get("question", "").strip()
if not question:
return jsonify({"error": "Please provide a question."}), 400
if "=" not in question:
return jsonify({"error": "Please enter a valid equation with '='"}), 400
try:
lhs, _, rhs = question.partition("=")
engine = LinearSolverEngine(lhs, rhs, n=3)
steps, solutions, var = engine.run()
input_values = [p[0] for p in solutions]
output_values = [p[1] for p in solutions]
graph_plotter = LinearGraphPlotter(input_values, output_values)
graph = graph_plotter.plot()
response_text = (
f"Let's solve your equation {lhs} = {rhs} step by step:\n\n"
f"{steps}\n"
f"Graph shows how solution changes with respect to '{var}'."
)
return jsonify({
"answer": response_text,
"graph": graph,
"variable": str(var),
"input_values": input_values,
"output_values": output_values,
"solutions": [
{"input": str(p[0]), "output": str(p[1])}
for p in solutions
]
})
except Exception as e:
print(f"Error: {e}")
return jsonify({
"error": "An error occurred while processing your request."
}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True) |