Spaces:
Running
Running
| from backend.solver import LinearSolverEngine | |
| from backend.linear_graph import LinearGraphPlotter | |
| from flask import Flask, request, jsonify, render_template | |
| app = Flask(__name__) | |
| def index(): | |
| return render_template("index.html") | |
| 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) |