Kelvin Tepes commited on
Commit
50ec5fc
·
verified ·
1 Parent(s): e7a3e05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -4,18 +4,18 @@ 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 from Space Settings
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  MODEL_REPO = "Kelvin000010191/Krypton-1"
12
 
13
- # Initializes the client with the authentication token
14
  client = InferenceClient(token=HF_TOKEN)
15
 
16
  @app.route("/", methods=["GET"])
17
  def home():
18
- return "Krypton-1 Core Node Operational."
19
 
20
  @app.route("/api/chat", methods=["POST"])
21
  def chat():
@@ -23,29 +23,30 @@ def chat():
23
  data = request.json
24
  user_prompt = data.get("prompt", "")
25
 
26
- # Explicitly targets the model repo and handles text generation
27
- response = client.text_generation(
28
- prompt=user_prompt,
29
  model=MODEL_REPO,
30
- max_new_tokens=250,
 
31
  temperature=0.7
32
  )
33
 
34
- return jsonify({"reply": response})
 
 
35
 
36
  except Exception as e:
37
  raw_error = str(e)
38
 
39
- # Flags if the newly generated token isn't saved right
40
  if "401" in raw_error or "unauthorized" in raw_error.lower():
41
- return jsonify({"reply": "SYS_AUTH_ERR: Token unauthorized. Make sure your fresh WRITE token is saved as HF_TOKEN in Space Secrets."})
42
 
43
- # Flags if the massive 15GB clusters are waking up
44
  if "loading" in raw_error.lower() or "503" in raw_error:
45
- return jsonify({"reply": "SYS_BOOT: Krypton-1 engine is loading weights into active memory. Give the cloud 60 seconds, then try again!"})
46
 
47
- # Prints any other raw issue directly to your phone screen
48
- return jsonify({"reply": f"SYS_ALERT: Connected to server, but model errored. Details: {raw_error}"})
49
 
50
  if __name__ == "__main__":
51
  app.run(host="0.0.0.0", port=7860)
 
4
  from huggingface_hub import InferenceClient
5
 
6
  app = Flask(__name__)
7
+ CORS(app) # Keeps your Acode terminal connected safely!
8
 
9
+ # Pulls your hidden WRITE token from Space secrets
10
  HF_TOKEN = os.getenv("HF_TOKEN")
11
  MODEL_REPO = "Kelvin000010191/Krypton-1"
12
 
13
+ # Initializes the official native client wrapper
14
  client = InferenceClient(token=HF_TOKEN)
15
 
16
  @app.route("/", methods=["GET"])
17
  def home():
18
+ return "Krypton-1 Core Chat Node Operational."
19
 
20
  @app.route("/api/chat", methods=["POST"])
21
  def chat():
 
23
  data = request.json
24
  user_prompt = data.get("prompt", "")
25
 
26
+ # Uses standard Chat Completion matrix format instead of raw text blocks
27
+ response = client.chat_completion(
 
28
  model=MODEL_REPO,
29
+ messages=[{"role": "user", "content": user_prompt}],
30
+ max_tokens=250,
31
  temperature=0.7
32
  )
33
 
34
+ # Pulls out the clean text string answer generated by the model
35
+ reply_text = response.choices[0].message.content
36
+ return jsonify({"reply": reply_text})
37
 
38
  except Exception as e:
39
  raw_error = str(e)
40
 
41
+ # Catch if the token setup isn't fully saved yet
42
  if "401" in raw_error or "unauthorized" in raw_error.lower():
43
+ return jsonify({"reply": "SYS_AUTH_ERR: Token unauthorized. Double check your HF_TOKEN inside Space Secrets."})
44
 
45
+ # Catch if your 15GB model layer is waking up its hardware node
46
  if "loading" in raw_error.lower() or "503" in raw_error:
47
+ return jsonify({"reply": "SYS_BOOT: Krypton-1 engine is loading weights into active memory clusters. Give the cloud 60 seconds, then try again!"})
48
 
49
+ return jsonify({"reply": f"SYS_ALERT: Handshake complete, but model rejected structure. Error Details: {raw_error}"})
 
50
 
51
  if __name__ == "__main__":
52
  app.run(host="0.0.0.0", port=7860)