AI-API / app.py
JJ94's picture
Update app.py
2e1b7f6 verified
raw
history blame
1.37 kB
from flask import Flask, request, jsonify, render_template
from llama_cpp import Llama
app = Flask(__name__)
# Load the model
print("πŸ”„ Loading model... (this may take a while)")
llm = Llama.from_pretrained(
repo_id="bartowski/google_gemma-3-1b-it-GGUF",
filename="google_gemma-3-1b-it-IQ4_XS.gguf",
)
print("βœ… Model loaded!")
@app.route("/")
def home():
print("πŸ“’ Serving index.html")
return render_template("index.html")
@app.route("/chat", methods=["POST"])
def chat():
user_input = request.json.get("message", "")
print(f"πŸ’¬ Received message: {user_input}")
if not user_input:
print("⚠️ Empty input received!")
return jsonify({"error": "Empty input"}), 400
try:
response = llm.create_chat_completion(
messages=[{"role": "user", "content": user_input}]
)
print(f"πŸ” Full response object: {response}") # Debugging
bot_reply = response["choices"][0]["message"]["content"]
print(f"πŸ€– Bot response: {bot_reply}")
return jsonify({"response": bot_reply})
except Exception as e:
print(f"❌ Error generating response: {e}")
return jsonify({"error": "Model failed to generate response"}), 500
if __name__ == "__main__":
print("πŸš€ Starting Flask app on port 7860")
app.run(host="0.0.0.0", port=7860, debug=True)