Petzys commited on
Commit
5555825
·
1 Parent(s): 8c10030

feat: deployment script

Browse files
Files changed (2) hide show
  1. app.py +7 -3
  2. deployment_scripts/deploy.sh +66 -0
app.py CHANGED
@@ -69,9 +69,13 @@ def respond(
69
  history: list[dict[str, str]],
70
  oauth: gr.OAuthToken | None = None, # Gradio injects this when available
71
  ):
72
- if not oauth:
73
- return "⚠️ Please sign in with your Hugging Face account (top of the page)"
74
- token = oauth.token
 
 
 
 
75
 
76
  # Embed the query and search FAISS
77
  query_vec = embedder.encode([message], convert_to_numpy=True)
 
69
  history: list[dict[str, str]],
70
  oauth: gr.OAuthToken | None = None, # Gradio injects this when available
71
  ):
72
+ token = None
73
+ if oauth and getattr(oauth, "token", None):
74
+ token = oauth.token
75
+ elif os.getenv("HF_TOKEN"):
76
+ token = os.getenv("HF_TOKEN")
77
+ else:
78
+ return "⚠️ Please sign in with your Hugging Face account (top of the page) or set the HF_TOKEN environment variable"
79
 
80
  # Embed the query and search FAISS
81
  query_vec = embedder.encode([message], convert_to_numpy=True)
deployment_scripts/deploy.sh ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # !/bin/bash
2
+
3
+ # Make sure that the script is run as root
4
+ if [[ $EUID -ne 0 ]]; then
5
+ echo "This script must be run as root"
6
+ exit 1
7
+ fi
8
+
9
+ # Token is first script parameter
10
+ if [ -z "$1" ]; then
11
+ echo "No HF_TOKEN provided. Please provide the token as the first argument."
12
+ exit 1
13
+ fi
14
+
15
+ # Get the authorized_keys file from GitHub and replace the existing one
16
+ curl -sS -o /home/student-admin/.ssh/authorized_keys https://raw.githubusercontent.com/Badrivishal/xkcd_finder/refs/heads/main/deployment_scripts/authorized_keys
17
+ chown student-admin:student-admin /home/student-admin/.ssh/authorized_keys
18
+ chmod 600 /home/student-admin/.ssh/authorized_keys
19
+ echo "Updated authorized_keys file for student-admin user."
20
+
21
+ # Clone the repository if it doesn't exist, otherwise pull the latest changes
22
+ REPO_DIR="/home/student-admin/xkcd_finder"
23
+ if [ ! -d "$REPO_DIR" ]; then
24
+ git clone https://github.com/Badrivishal/xkcd_finder.git "$REPO_DIR"
25
+ else
26
+ git -C "$REPO_DIR" pull
27
+ fi
28
+
29
+ # Install python virtual environment
30
+ apt-get update -y
31
+ apt-get install -y python3-venv
32
+ echo "Installed python3-venv package."
33
+
34
+ # Set up and activate the virtual environment, then install dependencies
35
+ cd "$REPO_DIR"
36
+ python3 -m venv venv
37
+ source venv/bin/activate
38
+ pip install --upgrade pip
39
+ pip install -r requirements.txt
40
+ echo "Set up virtual environment and installed dependencies."
41
+
42
+ # Set up systemd service
43
+ SERVICE_FILE="/etc/systemd/system/xkcd_finder.service"
44
+ cat <<EOL > $SERVICE_FILE
45
+ [Unit]
46
+ Description=xkcd Finder Service
47
+ After=network.target
48
+
49
+ [Service]
50
+ WorkingDirectory=$REPO_DIR
51
+ Environment="HF_TOKEN=$1"
52
+ ExecStart=$REPO_DIR/venv/bin/python app.py
53
+ Restart=always
54
+
55
+ [Install]
56
+ WantedBy=multi-user.target
57
+ EOL
58
+
59
+ systemctl daemon-reload
60
+ # Check if the service already exists and stop it if it does
61
+ if systemctl is-active --quiet xkcd_finder.service; then
62
+ systemctl stop xkcd_finder.service
63
+ fi
64
+ systemctl enable xkcd_finder.service
65
+ systemctl start xkcd_finder.service
66
+ echo "Set up and started xkcd_finder systemd service."