NitinBot001 commited on
Commit
0d88d68
·
verified ·
1 Parent(s): d3fbb15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -32
app.py CHANGED
@@ -15,7 +15,7 @@ GH_PAT = "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" # GitHub Personal Access To
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) # Create the /tmp/model directory
19
  if not os.path.exists(MODEL_PATH):
20
  print("Downloading model...")
21
  r = requests.get(
@@ -27,15 +27,13 @@ def download_model():
27
  f.write(chunk)
28
 
29
  def start_tunnel():
30
- # Start nport tunnel
31
  tunnel_process = subprocess.Popen(
32
  ["npx", "nport", "-s", "ai-service", "-p", "5000"],
33
  stdout=subprocess.PIPE,
34
  stderr=subprocess.PIPE,
35
  )
36
- time.sleep(10) # Wait for tunnel to establish
37
 
38
- # Extract tunnel URL from logs
39
  tunnel_url = None
40
  for line in iter(tunnel_process.stdout.readline, b""):
41
  line = line.decode("utf-8").strip()
@@ -45,42 +43,29 @@ def start_tunnel():
45
 
46
  if not tunnel_url:
47
  raise Exception("Failed to extract tunnel URL")
48
-
49
  return tunnel_url
50
 
51
  def push_tunnel_url_to_repo(tunnel_url):
52
- # Create instance.json
53
  instance_data = {"tunnel_url": tunnel_url}
54
  with open("/tmp/instance.json", "w") as f:
55
  json.dump(instance_data, f)
56
 
57
- # Clone the repository
58
  repo_dir = "/tmp/repo"
59
  repo_url = f"https://x-access-token:{GH_PAT}@github.com/NitinBot001/Audio-url-new-js.git"
60
 
61
- # Remove the existing repo directory if it exists
62
  if os.path.exists(repo_dir):
63
  subprocess.run(["rm", "-rf", repo_dir], check=True)
64
 
65
- subprocess.run(
66
- ["git", "clone", repo_url, repo_dir],
67
- check=True,
68
- )
69
  os.chdir(repo_dir)
70
 
71
- # Move instance.json to the repository
72
  subprocess.run(["mv", "/tmp/instance.json", "."], check=True)
73
-
74
- # Configure Git locally (without --global)
75
  subprocess.run(["git", "config", "user.email", "[email protected]"], check=True)
76
  subprocess.run(["git", "config", "user.name", "github-actions"], check=True)
77
-
78
- # Add the file to the staging area
79
  subprocess.run(["git", "add", "instance.json"], check=True)
80
 
81
- # Check if there are changes to commit
82
  result = subprocess.run(["git", "status", "--porcelain"], stdout=subprocess.PIPE, text=True)
83
- if result.stdout.strip(): # If there are changes
84
  subprocess.run(["git", "commit", "-m", f"Update tunnel URL to {tunnel_url}"], check=True)
85
  subprocess.run(["git", "push", "origin", "main"], check=True)
86
  else:
@@ -88,10 +73,7 @@ def push_tunnel_url_to_repo(tunnel_url):
88
 
89
  @app.route("/chat", methods=["GET"])
90
  def chat():
91
- # Get the message from the query parameter
92
  message = request.args.get("message", "")
93
-
94
- # Construct the prompt without duplicate special tokens
95
  prompt = (
96
  f"<|begin_of_text|>"
97
  f"<|start_header_id|>user<|end_header_id|>\n"
@@ -109,21 +91,13 @@ def chat():
109
  return jsonify({"response": output["choices"][0]["text"].strip()})
110
 
111
  if __name__ == "__main__":
112
- # Download the model
113
  download_model()
114
-
115
- # Initialize the LLM
116
  llm = Llama(
117
  model_path=MODEL_PATH,
118
- n_ctx=131072, # Set to match the training context length
119
  n_threads=2,
120
  n_gpu_layers=0,
121
  verbose=False,
122
  )
123
-
124
- # Start the tunnel and push the URL
125
  tunnel_url = start_tunnel()
126
- push_tunnel_url_to_repo(tunnel_url)
127
-
128
- # Run the Flask app
129
- app.run(host="0.0.0.0", port=5000)
 
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(
 
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()
 
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:
 
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"
 
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)