Kelvin000010191 commited on
Commit
e7a3e05
·
verified ·
1 Parent(s): ebe527d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -6,16 +6,16 @@ from huggingface_hub import InferenceClient
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,24 +23,29 @@ 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)
 
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
  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)