|
|
import flask |
|
|
from flask import request, jsonify |
|
|
|
|
|
|
|
|
|
|
|
from ctransformers import AutoModelForCausalLM |
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model_id = "TheBloke/Mistral-7B-Instruct-v0.2-GGUF" |
|
|
print("🔄 Loading model...") |
|
|
|
|
|
try: |
|
|
|
|
|
ai = AutoModelForCausalLM.from_pretrained( |
|
|
model_id, |
|
|
model_file="mistral-7b-instruct-v0.2.Q4_K_M.gguf", |
|
|
model_type="mistral", |
|
|
gpu_layers=0 |
|
|
) |
|
|
print("✅ Model loaded!") |
|
|
except Exception as e: |
|
|
print(f"❌ Error loading model: {e}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/chat', methods=['POST']) |
|
|
def chat(): |
|
|
try: |
|
|
data = request.get_json() |
|
|
msg = data.get("message", "") |
|
|
if not msg: |
|
|
return jsonify({"error": "No message sent"}), 400 |
|
|
|
|
|
|
|
|
output = ai(msg, max_new_tokens=200, temperature=0.7) |
|
|
return jsonify({"reply": output}) |
|
|
except Exception as e: |
|
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.run(host='0.0.0.0', port=7860) |