CyberCoder225 commited on
Commit
afd5803
·
verified ·
1 Parent(s): 1e4f000

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -12
app.py CHANGED
@@ -1,35 +1,55 @@
1
  from flask import Flask, request, jsonify, send_from_directory
 
2
  from brain import MairaBrain
3
  import os
4
 
5
  app = Flask(__name__)
 
6
 
7
- # Model config
 
8
  REPO_ID = "bartowski/Llama-3.2-1B-Instruct-GGUF"
9
  FILENAME = "Llama-3.2-1B-Instruct-Q4_K_M.gguf"
10
 
11
- # Start Maira
 
12
  maira = MairaBrain(REPO_ID, FILENAME)
13
 
 
 
14
  @app.route("/", methods=["GET"])
15
  def home():
16
- # Serves your HTML file from the root folder
 
17
  return send_from_directory('.', 'maira2.html')
18
 
19
  @app.route("/chat", methods=["POST"])
20
  def chat():
 
21
  try:
22
  data = request.json
23
- msg = data.get("message", "")
24
- uid = data.get("user_id", "default")
25
-
26
- # Maira thinks here (takes 5-30 seconds)
27
- res = maira.get_response(uid, msg)
 
 
 
28
 
29
- return jsonify({"response": res})
 
 
 
 
30
  except Exception as e:
31
- print(f"ERROR: {e}")
32
- return jsonify({"response": "Brain glitch! Check logs."}), 500
 
 
 
33
 
34
  if __name__ == "__main__":
35
- app.run(host="0.0.0.0", port=7860)
 
 
 
1
  from flask import Flask, request, jsonify, send_from_directory
2
+ from flask_cors import CORS # Required for your HTML to talk to this API
3
  from brain import MairaBrain
4
  import os
5
 
6
  app = Flask(__name__)
7
+ CORS(app) # Unlocks the neural link for your maira2.html
8
 
9
+ # --- CONFIGURATION ---
10
+ # Using the Llama 3.2 1B model (Smart & Fast)
11
  REPO_ID = "bartowski/Llama-3.2-1B-Instruct-GGUF"
12
  FILENAME = "Llama-3.2-1B-Instruct-Q4_K_M.gguf"
13
 
14
+ # Initialize Maira's Brain
15
+ # This will download the model to the container on first run
16
  maira = MairaBrain(REPO_ID, FILENAME)
17
 
18
+ # --- ROUTES ---
19
+
20
  @app.route("/", methods=["GET"])
21
  def home():
22
+ """Serves the front-end website"""
23
+ # This looks for maira2.html in the same folder as app.py
24
  return send_from_directory('.', 'maira2.html')
25
 
26
  @app.route("/chat", methods=["POST"])
27
  def chat():
28
+ """The API endpoint for both the Website and WhatsApp"""
29
  try:
30
  data = request.json
31
+ user_message = data.get("message", "")
32
+ user_id = data.get("user_id", "default_user")
33
+
34
+ if not user_message:
35
+ return jsonify({"error": "Message is empty"}), 400
36
+
37
+ # Ask the AI for a response
38
+ ai_response = maira.get_response(user_id, user_message)
39
 
40
+ return jsonify({
41
+ "status": "success",
42
+ "response": ai_response
43
+ })
44
+
45
  except Exception as e:
46
+ print(f"CRITICAL ERROR: {e}")
47
+ return jsonify({
48
+ "status": "error",
49
+ "response": "My neural circuits are lagging. Try again? 🧊"
50
+ }), 500
51
 
52
  if __name__ == "__main__":
53
+ # Port 7860 is mandatory for Hugging Face Spaces.
54
+ # It also works perfectly for local testing.
55
+ app.run(host="0.0.0.0", port=7860, debug=False)