Spaces:
Running
Running
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import contextlibfrom flask import Flask, request, jsonifyfrom flask_cors import CORSfrom scipy import ioimport torch# Import the specific functions from your ai_thingy script# Make sure your AI script is named ai_thingy.pyfrom ai_thingy import (initialize_or_retrain,generate_text,current_model,current_tokenizer,device,MAX_SEQ_LENGTH)import ai_thingyapp = Flask(__name__)CORS(app) # This allows your JS to talk to this Python server# Initialize the model once when the server startsprint("[Bridge] Waking up the Aoban Brain...")initialize_or_retrain(initial_train=True, epochs=10)@app.route('/ask', methods=['POST'])def ask():try:data = request.jsonuser_prompt = data.get("input", "")# Use the imported generate_text# We pass None for penalty/temp to let it use the script's defaultsresponse = ai_thingy.generate_text(ai_thingy.current_model,ai_thingy.current_tokenizer,user_prompt,ai_thingy.MAX_SEQ_LENGTH,ai_thingy.device,top_k=3,penalty=3.0,temperature=1.0)return jsonify({"response": response, "logs": "Generation successful."})except Exception as e:print(f"[CRASH] Aoban Brain Error: {e}")return jsonify({"response": f"Brain Error: {str(e)}", "logs": str(e)}), 500if __name__ == '__main__':app.run(port=5000, debug=False)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import contextlibfrom flask import Flask, request, jsonifyfrom flask_cors import CORSfrom scipy import ioimport torch# Import the specific functions from your ai_thingy script# Make sure your AI script is named ai_thingy.pyfrom ai_thingy import (initialize_or_retrain,generate_text,current_model,current_tokenizer,device,MAX_SEQ_LENGTH)import ai_thingyapp = Flask(__name__)CORS(app) # This allows your JS to talk to this Python server# Initialize the model once when the server startsprint("[Bridge] Waking up the Aoban Brain...")initialize_or_retrain(initial_train=True, epochs=10)@app.route('/ask', methods=['POST'])def ask():try:data = request.jsonuser_prompt = data.get("input", "")# Use the imported generate_text# We pass None for penalty/temp to let it use the script's defaultsresponse = ai_thingy.generate_text(ai_thingy.current_model,ai_thingy.current_tokenizer,user_prompt,ai_thingy.MAX_SEQ_LENGTH,ai_thingy.device,top_k=3,penalty=3.0,temperature=1.0)return jsonify({"response": response, "logs": "Generation successful."})except Exception as e:print(f"[CRASH] Aoban Brain Error: {e}")return jsonify({"response": f"Brain Error: {str(e)}", "logs": str(e)}), 500if __name__ == "__main__":# The cloud will tell us which port to use; default to 7860 for Hugging Faceimport osport = int(os.environ.get("PORT", 7860))app.run(host="0.0.0.0", port=port)
app.py+53-50