Threatthriver commited on
Commit
706021a
·
verified ·
1 Parent(s): 78ff835

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -36
app.py CHANGED
@@ -2,63 +2,125 @@ import gradio as gr
2
  from huggingface_hub import HfApi
3
  import os
4
  import uuid
 
 
5
 
6
- # --- SYSTEM CONFIGURATION ---
7
- # The Bridge looks for the Secret Token we just added
 
 
 
 
8
  HF_TOKEN = os.getenv("HF_TOKEN")
9
 
10
- # TARGET REPO: Your specific dataset
11
  DATASET_ID = "Threatthriver/sys_log_dump_v4_shards"
12
  HIDDEN_PATH = "data/blobs/0x44"
13
 
14
- # Initialize Connection
 
 
 
15
  api = HfApi(token=HF_TOKEN)
16
 
17
- def system_shard_allocator(packet_file):
 
 
 
 
 
18
  """
19
- INTERNAL: Allocates binary shard to persistent storage.
 
20
  """
21
- if packet_file is None:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  return "ERROR: No packet received."
23
-
24
- # 1. Generate System Hash (Stealth Name)
25
- # We rename the video to look like a system dump file ending in .bin
26
  request_id = str(uuid.uuid4())
27
- shard_name = f"sys_core_dump_{request_id}.bin"
28
-
29
- print(f"⚙️ Allocating Shard: {shard_name}...")
30
-
 
 
31
  try:
32
- # 2. Upload to the Hidden Folder in the Dataset
 
 
 
 
33
  api.upload_file(
34
- path_or_fileobj=packet_file.name,
35
- path_in_repo=f"{HIDDEN_PATH}/{shard_name}",
36
  repo_id=DATASET_ID,
37
  repo_type="dataset",
38
- commit_message=f"Auto-sync daemon: shard {request_id}"
39
  )
40
 
41
- # 3. Generate the Stream URL (Using 'resolve' for direct streaming)
42
- data_stream_url = f"https://huggingface.co/datasets/{DATASET_ID}/resolve/main/{HIDDEN_PATH}/{shard_name}"
43
-
44
  return data_stream_url
45
 
46
  except Exception as e:
47
- return f"ALLOCATION_ERROR: {str(e)}"
 
 
48
 
49
- # --- DAEMON INTERFACE ---
50
- with gr.Blocks(title="System Sync Daemon") as system_daemon:
51
- gr.Markdown("### ⚠️ SYSTEM SYNC DAEMON [BACKGROUND PROCESS]")
52
-
53
- with gr.Row():
54
- input_packet = gr.File(label="Binary Packet Input")
55
- output_log = gr.Textbox(label="Allocation Path")
56
-
57
- btn = gr.Button("Execute Sync")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- # --- CRITICAL FIX: api_name="predict" ---
60
- # This allows your Next.js client to call client.predict("/predict", ...)
61
- btn.click(fn=system_shard_allocator, inputs=input_packet, outputs=output_log, api_name="predict")
62
 
63
- # Launch
64
- system_daemon.queue().launch()
 
 
2
  from huggingface_hub import HfApi
3
  import os
4
  import uuid
5
+ import gzip
6
+ import shutil
7
 
8
+ # ==========================================
9
+ # 1. CONFIGURATION & SECURITY (Source of Truth)
10
+ # ==========================================
11
+
12
+ # --- SECURITY PRINCIPLE: ISOLATION ---
13
+ # We retrieve the token from the environment.
14
  HF_TOKEN = os.getenv("HF_TOKEN")
15
 
16
+ # --- CONSTANTS ---
17
  DATASET_ID = "Threatthriver/sys_log_dump_v4_shards"
18
  HIDDEN_PATH = "data/blobs/0x44"
19
 
20
+ # --- VALIDATION ---
21
+ if not HF_TOKEN:
22
+ raise RuntimeError("CORE_FAILURE: HF_TOKEN is missing. Please add it to Space Secrets.")
23
+
24
  api = HfApi(token=HF_TOKEN)
25
 
26
+
27
+ # ==========================================
28
+ # 2. CORE BUSINESS LOGIC (The Engine)
29
+ # ==========================================
30
+
31
+ def compress_packet(input_path):
32
  """
33
+ INTERNAL: Applies lossless GZIP compression to the binary packet.
34
+ Returns the path to the compressed temporary file.
35
  """
36
+ try:
37
+ compressed_path = input_path + ".gz"
38
+ with open(input_path, 'rb') as f_in:
39
+ with gzip.open(compressed_path, 'wb') as f_out:
40
+ shutil.copyfileobj(f_in, f_out)
41
+ return compressed_path
42
+ except Exception as e:
43
+ print(f"⚠️ [COMPRESSION ERROR] Skipping compression: {e}")
44
+ return input_path
45
+
46
+ def upload_shard(file_obj):
47
+ """
48
+ Allocates a binary file to the persistent dataset storage.
49
+ Now includes an automatic lossless compression step.
50
+ """
51
+ # 1. Input Validation
52
+ if file_obj is None:
53
  return "ERROR: No packet received."
54
+
55
+ # 2. Identity Generation
 
56
  request_id = str(uuid.uuid4())
57
+ # We append .gz to indicate compression, though it remains a binary blob
58
+ shard_name = f"sys_core_dump_{request_id}.bin.gz"
59
+ target_path = f"{HIDDEN_PATH}/{shard_name}"
60
+
61
+ print(f"⚙️ [DAEMON] Processing Shard: {request_id}...")
62
+
63
  try:
64
+ # 3. Compression (New Step: Increase Density)
65
+ print(f"🗜️ [DAEMON] Compressing packet (Lossless GZIP)...")
66
+ final_file_path = compress_packet(file_obj)
67
+
68
+ # 4. Execution (Upload)
69
  api.upload_file(
70
+ path_or_fileobj=final_file_path,
71
+ path_in_repo=target_path,
72
  repo_id=DATASET_ID,
73
  repo_type="dataset",
74
+ commit_message=f"daemon_sync: {request_id} [compressed]"
75
  )
76
 
77
+ # 5. Result Construction
78
+ data_stream_url = f"https://huggingface.co/datasets/{DATASET_ID}/resolve/main/{target_path}"
79
+ print(f"✅ [DAEMON] Success: {data_stream_url}")
80
  return data_stream_url
81
 
82
  except Exception as e:
83
+ error_msg = f"ALLOCATION_FAILURE: {str(e)}"
84
+ print(f"❌ [DAEMON] {error_msg}")
85
+ return error_msg
86
 
87
+
88
+ # ==========================================
89
+ # 3. INTERFACE LAYER (The Bridge)
90
+ # ==========================================
91
+
92
+ def build_interface():
93
+ with gr.Blocks(title="System Sync Daemon", theme=gr.themes.Monochrome()) as demo:
94
+
95
+ gr.Markdown("## 🔒 SYSTEM SYNC DAEMON [COMPRESSED]")
96
+ gr.Markdown("Auto-compresses and bridges binary packets to secure storage.")
97
+
98
+ with gr.Row():
99
+ with gr.Column():
100
+ input_packet = gr.File(
101
+ label="Binary Packet Input",
102
+ type="filepath",
103
+ file_count="single"
104
+ )
105
+ execute_btn = gr.Button("Execute Sync", variant="primary")
106
+
107
+ with gr.Column():
108
+ output_log = gr.Textbox(
109
+ label="Allocation Stream URL",
110
+ interactive=False,
111
+ show_copy_button=True
112
+ )
113
+
114
+ # --- API BINDING ---
115
+ execute_btn.click(
116
+ fn=upload_shard,
117
+ inputs=input_packet,
118
+ outputs=output_log,
119
+ api_name="predict"
120
+ )
121
 
122
+ return demo
 
 
123
 
124
+ if __name__ == "__main__":
125
+ app = build_interface()
126
+ app.queue().launch()