Devity4756 commited on
Commit
2ca2973
·
verified ·
1 Parent(s): 0373ee4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -1,27 +1,58 @@
1
  import gradio as gr
2
  import os
3
  import subprocess
 
 
4
 
5
- # Make sure repo folder exists
6
- if not os.path.exists("storage"):
7
- os.makedirs("storage")
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def run_command(cmd):
10
  try:
11
- # Run inside storage so data persists during Space lifetime
12
  result = subprocess.run(
13
- cmd, shell=True, cwd="storage",
14
  capture_output=True, text=True
15
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  return result.stdout + result.stderr
17
  except Exception as e:
18
  return str(e)
19
 
 
20
  with gr.Blocks() as demo:
21
- gr.Markdown("## 🚀 Hugging Face Mini Terminal with Persistence")
22
  cmd = gr.Textbox(label="Enter command", placeholder="e.g. git clone https://github.com/user/repo.git")
23
- out = gr.Textbox(label="Output")
24
- run_btn = gr.Button("Run")
25
  run_btn.click(run_command, inputs=cmd, outputs=out)
26
 
27
  demo.launch()
 
1
  import gradio as gr
2
  import os
3
  import subprocess
4
+ import shutil
5
+ from huggingface_hub import HfApi, HfFolder, snapshot_download
6
 
7
+ # === Config ===
8
+ DATASET_REPO = "Devity4756/Terminal" # change to your dataset repo
9
+ HF_TOKEN = HfFolder.get_token() # will read from your Space secret
10
+ api = HfApi(token=HF_TOKEN)
11
 
12
+ WORKDIR = "workspace"
13
+ os.makedirs(WORKDIR, exist_ok=True)
14
+
15
+ # === Restore previous state on startup ===
16
+ try:
17
+ snapshot_path = snapshot_download(
18
+ repo_id=DATASET_REPO,
19
+ repo_type="dataset",
20
+ token=HF_TOKEN
21
+ )
22
+ shutil.copytree(snapshot_path, WORKDIR, dirs_exist_ok=True)
23
+ except Exception as e:
24
+ print("No previous data restored:", e)
25
+
26
+ # === Command runner ===
27
  def run_command(cmd):
28
  try:
 
29
  result = subprocess.run(
30
+ cmd, shell=True, cwd=WORKDIR,
31
  capture_output=True, text=True
32
  )
33
+
34
+ # After running, sync everything in WORKDIR back to dataset
35
+ try:
36
+ api.upload_folder(
37
+ folder_path=WORKDIR,
38
+ repo_id=DATASET_REPO,
39
+ repo_type="dataset",
40
+ commit_message=f"Sync after: {cmd}",
41
+ token=HF_TOKEN
42
+ )
43
+ except Exception as e:
44
+ print("Upload failed:", e)
45
+
46
  return result.stdout + result.stderr
47
  except Exception as e:
48
  return str(e)
49
 
50
+ # === Gradio UI ===
51
  with gr.Blocks() as demo:
52
+ gr.Markdown("## 🚀 Hugging Face Terminal (Auto-Sync + Auto-Restore)")
53
  cmd = gr.Textbox(label="Enter command", placeholder="e.g. git clone https://github.com/user/repo.git")
54
+ out = gr.Textbox(label="Output", lines=15)
55
+ run_btn = gr.Button("Run Command")
56
  run_btn.click(run_command, inputs=cmd, outputs=out)
57
 
58
  demo.launch()