Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,103 +1,122 @@
|
|
1 |
import os
|
|
|
2 |
import requests
|
3 |
-
from flask import Flask, request, jsonify
|
4 |
-
from llama_cpp import Llama
|
5 |
import subprocess
|
6 |
-
import time
|
7 |
import json
|
|
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
-
#
|
12 |
-
MODEL_DIR = "/
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
REPO_URL = "https://github.com/NitinBot001/Audio-url-new-js.git"
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
)
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
def start_tunnel():
|
30 |
-
|
31 |
-
|
|
|
32 |
stdout=subprocess.PIPE,
|
33 |
stderr=subprocess.PIPE,
|
|
|
34 |
)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
line = line.decode("utf-8").strip()
|
40 |
if "your domain is:" in line:
|
41 |
-
|
|
|
42 |
break
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
def push_tunnel_url_to_repo(tunnel_url):
|
49 |
-
instance_data = {"tunnel_url": tunnel_url}
|
50 |
-
with open("/tmp/instance.json", "w") as f:
|
51 |
-
json.dump(instance_data, f)
|
52 |
-
|
53 |
-
repo_dir = "/tmp/repo"
|
54 |
-
repo_url = f"https://x-access-token:{GH_PAT}@github.com/NitinBot001/Audio-url-new-js.git"
|
55 |
|
|
|
56 |
if os.path.exists(repo_dir):
|
57 |
-
subprocess.run(["
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
subprocess.run(["git", "add", "instance.json"], check=True)
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
73 |
|
74 |
@app.route("/chat", methods=["GET"])
|
75 |
-
def
|
|
|
|
|
76 |
message = request.args.get("message", "")
|
77 |
-
prompt =
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
stop=["<|eot_id|>"],
|
88 |
-
temperature=0.8,
|
89 |
-
top_p=0.9,
|
90 |
-
)
|
91 |
-
return jsonify({"response": output["choices"][0]["text"].strip()})
|
92 |
|
93 |
if __name__ == "__main__":
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
n_threads=2,
|
99 |
-
n_gpu_layers=0,
|
100 |
-
verbose=False,
|
101 |
-
)
|
102 |
-
tunnel_url = start_tunnel()
|
103 |
-
push_tunnel_url_to_repo(tunnel_url)
|
|
|
1 |
import os
|
2 |
+
import threading
|
3 |
import requests
|
|
|
|
|
4 |
import subprocess
|
|
|
5 |
import json
|
6 |
+
from flask import Flask, jsonify, request
|
7 |
+
from llama_cpp import Llama
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
|
11 |
+
# Configuration
|
12 |
+
MODEL_DIR = "/data/model" # Persistent storage directory
|
13 |
+
MODEL_NAME = "calme-3.3-llamaloi-3b.Q4_K_M.gguf"
|
14 |
+
MODEL_PATH = os.path.join(MODEL_DIR, MODEL_NAME)
|
15 |
+
MODEL_URL = "https://huggingface.co/MaziyarPanahi/calme-3.3-llamaloi-3b-GGUF/resolve/main/calme-3.3-llamaloi-3b.Q4_K_M.gguf"
|
16 |
+
GH_PAT = os.getenv("GH_PAT")
|
17 |
REPO_URL = "https://github.com/NitinBot001/Audio-url-new-js.git"
|
18 |
|
19 |
+
# Global state
|
20 |
+
initialized = False
|
21 |
+
llm = None
|
22 |
+
|
23 |
+
def background_init():
|
24 |
+
global initialized, llm
|
25 |
+
try:
|
26 |
+
# 1. Ensure model directory exists
|
27 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
28 |
+
|
29 |
+
# 2. Download model if not exists
|
30 |
+
if not os.path.exists(MODEL_PATH):
|
31 |
+
print("Downloading model...")
|
32 |
+
with requests.get(MODEL_URL, stream=True) as r:
|
33 |
+
r.raise_for_status()
|
34 |
+
with open(MODEL_PATH, "wb") as f:
|
35 |
+
for chunk in r.iter_content(chunk_size=8192):
|
36 |
+
f.write(chunk)
|
37 |
+
print("Model download complete")
|
38 |
+
|
39 |
+
# 3. Initialize LLM
|
40 |
+
llm = Llama(
|
41 |
+
model_path=MODEL_PATH,
|
42 |
+
n_ctx=8192,
|
43 |
+
n_threads=2,
|
44 |
+
n_gpu_layers=0,
|
45 |
+
verbose=False
|
46 |
)
|
47 |
+
|
48 |
+
# 4. Start tunnel and update repo
|
49 |
+
tunnel_url = start_tunnel()
|
50 |
+
update_repo_with_tunnel(tunnel_url)
|
51 |
+
|
52 |
+
initialized = True
|
53 |
+
print("Initialization complete")
|
54 |
+
except Exception as e:
|
55 |
+
print(f"Initialization failed: {str(e)}")
|
56 |
|
57 |
def start_tunnel():
|
58 |
+
"""Start nport tunnel and return URL"""
|
59 |
+
proc = subprocess.Popen(
|
60 |
+
["npx", "nport", "-s", "hf-space", "-p", "7860"],
|
61 |
stdout=subprocess.PIPE,
|
62 |
stderr=subprocess.PIPE,
|
63 |
+
text=True
|
64 |
)
|
65 |
+
|
66 |
+
# Wait for tunnel URL
|
67 |
+
while True:
|
68 |
+
line = proc.stdout.readline()
|
|
|
69 |
if "your domain is:" in line:
|
70 |
+
return line.split("your domain is: ")[1].strip()
|
71 |
+
if proc.poll() is not None:
|
72 |
break
|
73 |
+
raise RuntimeError("Failed to establish tunnel")
|
74 |
|
75 |
+
def update_repo_with_tunnel(url):
|
76 |
+
"""Update GitHub repository with tunnel URL"""
|
77 |
+
repo_dir = "/data/repo"
|
78 |
+
instance_path = os.path.join(repo_dir, "instance.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
+
# Clone or update repository
|
81 |
if os.path.exists(repo_dir):
|
82 |
+
subprocess.run(["git", "-C", repo_dir, "pull"], check=True)
|
83 |
+
else:
|
84 |
+
subprocess.run([
|
85 |
+
"git", "clone",
|
86 |
+
f"https://x-access-token:{GH_PAT}@github.com/NitinBot001/Audio-url-new-js.git",
|
87 |
+
repo_dir
|
88 |
+
], check=True)
|
89 |
|
90 |
+
# Update instance.json
|
91 |
+
with open(instance_path, "w") as f:
|
92 |
+
json.dump({"tunnel_url": url}, f)
|
|
|
93 |
|
94 |
+
# Commit and push changes
|
95 |
+
subprocess.run(["git", "-C", repo_dir, "add", "."], check=True)
|
96 |
+
subprocess.run([
|
97 |
+
"git", "-C", repo_dir,
|
98 |
+
"commit", "-m", f"Update tunnel URL: {url}"
|
99 |
+
], check=True)
|
100 |
+
subprocess.run(["git", "-C", repo_dir, "push"], check=True)
|
101 |
|
102 |
@app.route("/chat", methods=["GET"])
|
103 |
+
def chat_endpoint():
|
104 |
+
if not initialized:
|
105 |
+
return jsonify({"error": "Service initializing"}), 503
|
106 |
message = request.args.get("message", "")
|
107 |
+
prompt = f"<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n{message}<|eot_id|>\n<|start_header_id|>assistant<|end_header_id|>\n"
|
108 |
+
output = llm(prompt, max_tokens=512, stop=["<|eot_id|>"])
|
109 |
+
return jsonify({"response": output['choices'][0]['text'].strip()})
|
110 |
+
|
111 |
+
@app.route("/health")
|
112 |
+
def health_check():
|
113 |
+
return jsonify({
|
114 |
+
"status": "ready" if initialized else "initializing",
|
115 |
+
"model_loaded": os.path.exists(MODEL_PATH)
|
116 |
+
}), 200 if initialized else 503
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
if __name__ == "__main__":
|
119 |
+
# Start initialization in background
|
120 |
+
threading.Thread(target=background_init, daemon=True).start()
|
121 |
+
# Start Flask server
|
122 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|