Threatthriver commited on
Commit
8f73189
·
verified ·
1 Parent(s): d20519c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -28
app.py CHANGED
@@ -2,63 +2,88 @@ 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
+ from fastapi import FastAPI, UploadFile, File
6
+ import uvicorn
7
 
8
  # --- SYSTEM CONFIGURATION ---
 
9
  HF_TOKEN = os.getenv("HF_TOKEN")
 
 
10
  DATASET_ID = "Threatthriver/sys_log_dump_v4_shards"
11
  HIDDEN_PATH = "data/blobs/0x44"
12
 
13
  # Initialize Connection
14
  api = HfApi(token=HF_TOKEN)
15
+ app = FastAPI()
16
 
17
+ # --- HELPER FUNCTION ---
18
+ def upload_logic(file_obj, filename_hint="video.mp4"):
19
  """
20
+ Core upload logic shared by both Gradio (Web) and FastAPI (Mobile)
21
  """
 
 
 
 
 
 
 
 
 
 
22
  try:
23
+ # 1. Generate Stealth Name
24
+ request_id = str(uuid.uuid4())
25
+ extension = os.path.splitext(filename_hint)[1] or ".bin"
26
+ if extension == ".bin": extension = ".mp4" # Default to mp4 if unknown
27
+
28
+ shard_name = f"sys_core_dump_{request_id}{extension}"
29
+
30
+ print(f"⚙️ Allocating Shard: {shard_name}...")
31
+
32
+ # 2. Upload to Hidden Folder
33
  api.upload_file(
34
+ path_or_fileobj=file_obj,
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 Stream URL
42
  data_stream_url = f"https://huggingface.co/datasets/{DATASET_ID}/resolve/main/{HIDDEN_PATH}/{shard_name}"
 
43
  return data_stream_url
44
+
45
  except Exception as e:
46
+ print(f"❌ Error: {str(e)}")
47
+ return None
48
+
49
+ # --- WEB INTERFACE (GRADIO) ---
50
+ def system_shard_allocator(packet_file):
51
+ if packet_file is None: return "ERROR: No packet received."
52
+
53
+ # Gradio passes a NamedTemporaryFile wrapper, we use its path
54
+ url = upload_logic(packet_file.name, packet_file.name)
55
+ return url if url else "ALLOCATION_ERROR"
56
 
 
57
  with gr.Blocks(title="System Sync Daemon") as system_daemon:
58
  gr.Markdown("### ⚠️ SYSTEM SYNC DAEMON [BACKGROUND PROCESS]")
 
59
  with gr.Row():
60
  input_packet = gr.File(label="Binary Packet Input")
61
  output_log = gr.Textbox(label="Allocation Path")
62
 
63
  btn = gr.Button("Execute Sync")
64
+ # Web uses 'predict' endpoint via Gradio Client
 
 
65
  btn.click(fn=system_shard_allocator, inputs=input_packet, outputs=output_log, api_name="predict")
66
 
67
+ # --- MOBILE INTERFACE (FASTAPI) ---
68
+ # This matches the endpoint in your mobile app: /upload_stream
69
+ @app.post("/upload_stream")
70
+ async def upload_stream(video: UploadFile = File(...)):
71
+ """
72
+ Endpoint for Mobile App Multipart Uploads
73
+ """
74
+ print(f"📱 Receiving mobile stream: {video.filename}")
75
+
76
+ # Pass the specialized file object from FastAPI directly to HfApi
77
+ url = upload_logic(video.file, video.filename)
78
+
79
+ if url:
80
+ return {"url": url}
81
+ else:
82
+ return {"error": "Upload failed"}, 500
83
+
84
+ # Mount Gradio to the root of FastAPI
85
+ app = gr.mount_gradio_app(app, system_daemon, path="/")
86
+
87
+ # If running locally (Space handles this automatically usually, but good for testing)
88
+ if __name__ == "__main__":
89
+ uvicorn.run(app, host="0.0.0.0", port=7860)