NitinBot001 commited on
Commit
6cb858c
·
verified ·
1 Parent(s): 974caa5

Create app.py

Browse files

Added main app.py

Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ MODEL_PATH = "model/calme-3.3-llamaloi-3b.Q4_K_M.gguf"
11
+ GH_PAT = os.getenv("GH_PAT","ghp_TGVMURDWG3HkLIfWUa4hyJUGYOYgXH1EljIi") # GitHub Personal Access Token
12
+ REPO_URL = "https://github.com/NitinBot001/Audio-url-new-js.git"
13
+
14
+ def download_model():
15
+ os.makedirs("model", exist_ok=True)
16
+ if not os.path.exists(MODEL_PATH):
17
+ print("Downloading model...")
18
+ r = requests.get(
19
+ "https://huggingface.co/MaziyarPanahi/calme-3.3-llamaloi-3b-GGUF/resolve/main/calme-3.3-llamaloi-3b.Q4_K_M.gguf",
20
+ stream=True,
21
+ )
22
+ with open(MODEL_PATH, "wb") as f:
23
+ for chunk in r.iter_content(chunk_size=8192):
24
+ f.write(chunk)
25
+
26
+ def start_tunnel():
27
+ # Start nport tunnel
28
+ tunnel_process = subprocess.Popen(
29
+ ["npx", "nport", "-s", "ai-service", "-p", "5000"],
30
+ stdout=subprocess.PIPE,
31
+ stderr=subprocess.PIPE,
32
+ )
33
+ time.sleep(10) # Wait for tunnel to establish
34
+
35
+ # Extract tunnel URL from logs
36
+ tunnel_url = None
37
+ for line in iter(tunnel_process.stdout.readline, b""):
38
+ line = line.decode("utf-8").strip()
39
+ if "your domain is:" in line:
40
+ tunnel_url = line.split("your domain is: ")[1]
41
+ break
42
+
43
+ if not tunnel_url:
44
+ raise Exception("Failed to extract tunnel URL")
45
+
46
+ return tunnel_url
47
+
48
+ def push_tunnel_url_to_repo(tunnel_url):
49
+ # Create instance.json
50
+ instance_data = {"tunnel_url": tunnel_url}
51
+ with open("instance.json", "w") as f:
52
+ json.dump(instance_data, f)
53
+
54
+ # Clone the repository
55
+ subprocess.run(
56
+ ["git", "clone", f"https://x-access-token:{GH_PAT}@{REPO_URL.split('https://')[1]}", "repo"],
57
+ check=True,
58
+ )
59
+ os.chdir("repo")
60
+
61
+ # Move instance.json to the repository
62
+ subprocess.run(["mv", "../instance.json", "."], check=True)
63
+
64
+ # Commit and push changes
65
+ subprocess.run(["git", "config", "--global", "user.email", "[email protected]"], check=True)
66
+ subprocess.run(["git", "config", "--global", "user.name", "github-actions"], check=True)
67
+ subprocess.run(["git", "add", "instance.json"], check=True)
68
+ subprocess.run(["git", "commit", "-m", f"Update tunnel URL to {tunnel_url}"], check=True)
69
+ subprocess.run(["git", "push", "origin", "main"], check=True)
70
+
71
+ @app.route("/chat", methods=["POST"])
72
+ def chat():
73
+ data = request.json
74
+ prompt = f"<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n{data.get('message','')}<|eot_id|>\n<|start_header_id|>assistant<|end_header_id|>\n"
75
+ output = llm(prompt, max_tokens=2048, stop=["<|eot_id|>"], temperature=0.8, top_p=0.9)
76
+ return jsonify({"response": output["choices"][0]["text"].strip()})
77
+
78
+ if __name__ == "__main__":
79
+ # Download the model
80
+ download_model()
81
+
82
+ # Initialize the LLM
83
+ llm = Llama(
84
+ model_path=MODEL_PATH,
85
+ n_ctx=8192,
86
+ n_threads=2,
87
+ n_gpu_layers=0,
88
+ verbose=False,
89
+ )
90
+
91
+ # Start the tunnel and push the URL
92
+ tunnel_url = start_tunnel()
93
+ push_tunnel_url_to_repo(tunnel_url)
94
+
95
+ # Run the Flask app
96
+ app.run(host="0.0.0.0", port=5000)