NitinBot001 commited on
Commit
048224c
·
verified ·
1 Parent(s): d25d5d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -76
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
- # Use /tmp directory for storing the model
12
- MODEL_DIR = "/tmp/model"
13
- MODEL_PATH = os.path.join(MODEL_DIR, "calme-3.3-llamaloi-3b.Q4_K_M.gguf")
14
- GH_PAT = "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" # GitHub Personal Access Token
 
 
15
  REPO_URL = "https://github.com/NitinBot001/Audio-url-new-js.git"
16
 
17
- def download_model():
18
- os.makedirs(MODEL_DIR, exist_ok=True)
19
- if not os.path.exists(MODEL_PATH):
20
- print("Downloading model...")
21
- r = requests.get(
22
- "https://huggingface.co/MaziyarPanahi/calme-3.3-llamaloi-3b-GGUF/resolve/main/calme-3.3-llamaloi-3b.Q4_K_M.gguf",
23
- stream=True,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  )
25
- with open(MODEL_PATH, "wb") as f:
26
- for chunk in r.iter_content(chunk_size=8192):
27
- f.write(chunk)
 
 
 
 
 
 
28
 
29
  def start_tunnel():
30
- tunnel_process = subprocess.Popen(
31
- ["npx", "nport", "-s", "ai-service", "-p", "5000"],
 
32
  stdout=subprocess.PIPE,
33
  stderr=subprocess.PIPE,
 
34
  )
35
- time.sleep(10)
36
-
37
- tunnel_url = None
38
- for line in iter(tunnel_process.stdout.readline, b""):
39
- line = line.decode("utf-8").strip()
40
  if "your domain is:" in line:
41
- tunnel_url = line.split("your domain is: ")[1]
 
42
  break
 
43
 
44
- if not tunnel_url:
45
- raise Exception("Failed to extract tunnel URL")
46
- return tunnel_url
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(["rm", "-rf", repo_dir], check=True)
58
-
59
- subprocess.run(["git", "clone", repo_url, repo_dir], check=True)
60
- os.chdir(repo_dir)
 
 
 
61
 
62
- subprocess.run(["mv", "/tmp/instance.json", "."], check=True)
63
- subprocess.run(["git", "config", "user.email", "[email protected]"], check=True)
64
- subprocess.run(["git", "config", "user.name", "github-actions"], check=True)
65
- subprocess.run(["git", "add", "instance.json"], check=True)
66
 
67
- result = subprocess.run(["git", "status", "--porcelain"], stdout=subprocess.PIPE, text=True)
68
- if result.stdout.strip():
69
- subprocess.run(["git", "commit", "-m", f"Update tunnel URL to {tunnel_url}"], check=True)
70
- subprocess.run(["git", "push", "origin", "main"], check=True)
71
- else:
72
- print("No changes to commit.")
 
73
 
74
  @app.route("/chat", methods=["GET"])
75
- def chat():
 
 
76
  message = request.args.get("message", "")
77
- prompt = (
78
- f"<|begin_of_text|>"
79
- f"<|start_header_id|>user<|end_header_id|>\n"
80
- f"{message}"
81
- f"<|eot_id|>\n"
82
- f"<|start_header_id|>assistant<|end_header_id|>\n"
83
- )
84
- output = llm(
85
- prompt,
86
- max_tokens=2048,
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
- download_model()
95
- llm = Llama(
96
- model_path=MODEL_PATH,
97
- n_ctx=131072,
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)