Repository navigation
Expand file tree
/
Copy pathtoolhunt_in_colab.py
More file actions
105 lines (86 loc) · 3.63 KB
/
Copy pathtoolhunt_in_colab.py
File metadata and controls
105 lines (86 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
## Download the required model for semantic search
from sentence_transformers import SentenceTransformer
import os
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('sentence-transformers/all-MiniLM-L12-v2')
embeddings = model.encode(sentences)
print(embeddings)
## Download dependencises
!git clone https://github.com/cyberytti/ToolHunt
os.chdir("ToolHunt")
!pip install -r requirements.txt
!pip install pyngrok
# =========================
# Directly Run ToolHunt in Colab (Debugging Mode)
# =========================
import sys
import threading
import time
from pyngrok import ngrok
# --- 1. Set up paths and environment ---
project_root = "/content" # Standard Colab working directory
toolhunt_dir = os.path.join(project_root, "ToolHunt")
if not os.path.isdir(toolhunt_dir):
raise FileNotFoundError(f"ToolHunt directory not found at {toolhunt_dir}")
# Crucially, add the project root to Python's path
# This allows 'from backend.main import ...' to work when app.py is run
if project_root not in sys.path:
sys.path.insert(0, project_root)
print(f"Added {project_root} to sys.path")
print(f"Project root: {project_root}")
print(f"ToolHunt dir: {toolhunt_dir}")
print(f"sys.path (relevant parts): {[p for p in sys.path if 'content' in p]}")
# Set ngrok token
ngrok.set_auth_token('your ngrok auth')
# --- 2. Import and run Flask app ---
try:
# Change working directory to ToolHunt so relative paths inside app.py work
# (like loading templates)
os.chdir(toolhunt_dir)
print(f"Changed working directory to: {os.getcwd()}")
# Now we can import app.py as a module because we added /content to sys.path
# and we are running from within the ToolHunt directory for relative file paths.
import app
print("✅ Successfully imported app.py")
# --- 3. Run Flask in a separate thread ---
def run_flask():
print("🚀 Starting Flask app...")
# Run Flask app. Note: debug=True might cause issues in threads/Colab,
# but let's try it first. If problems occur, remove debug=True.
app.app.run(host='127.0.0.1', port=5000, debug=False) # Start without debug first
flask_thread = threading.Thread(target=run_flask)
flask_thread.daemon = True # Dies when main thread dies
flask_thread.start()
print("🧵 Flask thread started.")
# --- 4. Wait and check ---
# Give Flask time to start (especially for model download on first run)
print("⏳ Waiting for Flask to initialize...")
time.sleep(8) # Adjust if needed
# A simple check if the thread is still alive (not a guarantee the server is up)
if flask_thread.is_alive():
print("✅ Flask thread is running.")
else:
print("⚠️ Flask thread has stopped. Check for errors in the logs above.")
# --- 5. Create ngrok tunnel ---
print("🚇 Starting ngrok tunnel...")
tunnel = ngrok.connect(5000, "http")
print("\n🎉 ToolHunt is ready!")
print(f"🔗 Public URL: {tunnel.public_url}")
print("\nPress the 'Interrupt Execution' button (⏹️) in Colab to stop.")
# --- 6. Keep alive ---
# Keep the main thread alive to maintain the tunnel
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\n🛑 Interrupted by user.")
finally:
print("🧹 Cleaning up...")
ngrok.kill()
# Note: Stopping the Flask thread cleanly from here is tricky in Colab.
# The daemon thread will stop when the main program ends.
print("✅ Done.")
except Exception as e:
print(f"❌ An error occurred during startup: {e}")
import traceback
traceback.print_exc()