Kelvin Tepes commited on
Commit
cb90759
·
verified ·
1 Parent(s): 13f0f0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -26
app.py CHANGED
@@ -1,18 +1,21 @@
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
  import os
4
- import requests
5
 
6
  app = Flask(__name__)
7
- CORS(app) # Bypasses your browser security blocks instantly
8
 
 
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
  MODEL_REPO = "Kelvin000010191/Krypton-1"
11
- API_URL = f"https://api-inference.huggingface.co/models/{MODEL_REPO}"
 
 
12
 
13
  @app.route("/", methods=["GET"])
14
  def home():
15
- return "Krypton-1 Core API Proxy Node Online."
16
 
17
  @app.route("/api/chat", methods=["POST"])
18
  def chat():
@@ -20,32 +23,24 @@ def chat():
20
  data = request.json
21
  user_prompt = data.get("prompt", "")
22
 
23
- headers = {
24
- "Authorization": f"Bearer {HF_TOKEN}",
25
- "Content-Type": "application/json"
26
- }
 
 
 
27
 
28
- payload = {
29
- "inputs": user_prompt,
30
- "parameters": {
31
- "max_new_tokens": 250,
32
- "temperature": 0.7,
33
- "return_full_text": False
34
- }
35
- }
36
 
37
- response = requests.post(API_URL, headers=headers, json=payload)
38
- res_data = response.json()
39
 
40
- # Keep track if your 15GB model needs a hot second to load weights
41
- if isinstance(res_data, dict) and "error" in res_data:
42
- return jsonify({"reply": f"SYS_BOOT: Krypton-1 is loading weights. Estimated activation: {round(res_data.get('estimated_time', 60))}s. Wait a moment and hit RUN again."})
43
 
44
- reply = res_data[0].get("generated_text", "Data stream loop empty.")
45
- return jsonify({"reply": reply})
46
-
47
- except Exception as e:
48
- return jsonify({"reply": f"SYS_ERR: Cloud proxy connection broken. {str(e)}"})
49
 
50
  if __name__ == "__main__":
51
  app.run(host="0.0.0.0", port=7860)
 
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
  import os
4
+ from huggingface_hub import InferenceClient
5
 
6
  app = Flask(__name__)
7
+ CORS(app) # Keeps your Acode connection unlocked permanently!
8
 
9
+ # Pulls your hidden secret token safely
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  MODEL_REPO = "Kelvin000010191/Krypton-1"
12
+
13
+ # Initializes the official native Hugging Face client
14
+ client = InferenceClient(model=MODEL_REPO, token=HF_TOKEN)
15
 
16
  @app.route("/", methods=["GET"])
17
  def home():
18
+ return "Krypton-1 Native Proxy Node is Online."
19
 
20
  @app.route("/api/chat", methods=["POST"])
21
  def chat():
 
23
  data = request.json
24
  user_prompt = data.get("prompt", "")
25
 
26
+ # Uses internal channel loops instead of outbound internet requests
27
+ response = client.text_generation(
28
+ user_prompt,
29
+ max_new_tokens=250,
30
+ temperature=0.7,
31
+ return_full_text=False
32
+ )
33
 
34
+ return jsonify({"reply": response})
 
 
 
 
 
 
 
35
 
36
+ except Exception as e:
37
+ error_msg = str(e)
38
 
39
+ # Catch if your 15GB model cluster is waking up/loading weights
40
+ if "loading" in error_msg.lower():
41
+ return jsonify({"reply": "SYS_BOOT: Krypton-1 is loading weights into memory clusters. Let the cloud cook for a moment, then press RUN again."})
42
 
43
+ return jsonify({"reply": f"SYS_ERR: Native cloud connection broken. {error_msg}"})
 
 
 
 
44
 
45
  if __name__ == "__main__":
46
  app.run(host="0.0.0.0", port=7860)