Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,126 +1,127 @@
|
|
| 1 |
import os
|
| 2 |
import shutil
|
| 3 |
-
import json
|
| 4 |
import gradio as gr
|
| 5 |
from gradio_client import Client, handle_file
|
| 6 |
from huggingface_hub import hf_hub_download, list_repo_files
|
| 7 |
|
| 8 |
-
# 1.
|
| 9 |
-
# Ensure HF_TOKEN is in your Space Secrets
|
| 10 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 11 |
PRIVATE_SPACE = "st192011/ASL-VLS-Private"
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
KB_FILE = "asl_rag_knowledge_base.json"
|
| 22 |
-
supported_glosses = []
|
| 23 |
-
if os.path.exists(KB_FILE):
|
| 24 |
-
with open(KB_FILE, 'r') as f:
|
| 25 |
-
kb_data = json.load(f)
|
| 26 |
-
supported_glosses = sorted(list(set([item['gloss'].upper() for item in kb_data])))
|
| 27 |
|
| 28 |
# 3. DATASET DISCOVERY (WLASL data_0)
|
| 29 |
-
|
|
|
|
| 30 |
try:
|
| 31 |
all_files = list_repo_files(repo_id="Voxel51/WLASL", repo_type="dataset")
|
| 32 |
data_0_mp4s = [f for f in all_files if f.startswith("data/data_0/") and f.endswith(".mp4")]
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
except Exception as e:
|
| 35 |
print(f"Repo listing failed: {e}")
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
|
|
|
| 39 |
def update_video_display(selection):
|
| 40 |
-
"""Downloads sample
|
| 41 |
-
if not selection: return None
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
cache_path = hf_hub_download(repo_id="Voxel51/WLASL", filename=hf_path, repo_type="dataset")
|
| 46 |
-
|
| 47 |
-
local_path = os.path.join("/tmp", selection)
|
| 48 |
shutil.copy(cache_path, local_path)
|
| 49 |
-
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
|
| 52 |
-
return None
|
| 53 |
|
| 54 |
def run_omnisign_vlm(video_path):
|
| 55 |
-
"""Sends video to private VLM engine using
|
| 56 |
-
if not video_path:
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
global client
|
| 60 |
-
if client is None:
|
| 61 |
-
try:
|
| 62 |
-
client = Client(PRIVATE_SPACE, hf_token=HF_TOKEN)
|
| 63 |
-
except:
|
| 64 |
-
return {"Neural Engine Offline": 0.0}
|
| 65 |
|
| 66 |
try:
|
| 67 |
-
# The key:
|
| 68 |
-
# We call the explicit api_name we set in the private space
|
| 69 |
result = client.predict(
|
| 70 |
video_file=handle_file(video_path),
|
| 71 |
api_name="/predict_sign"
|
| 72 |
)
|
| 73 |
return result
|
| 74 |
except Exception as e:
|
|
|
|
| 75 |
return {f"Neural Analysis Failed: {str(e)}": 0.0}
|
| 76 |
|
| 77 |
-
#
|
| 78 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 79 |
gr.Markdown(f"""
|
| 80 |
-
# π§ OmniSign VLM
|
| 81 |
-
### **
|
| 82 |
-
|
| 83 |
-
OmniSign is an advanced structural demonstration of **Large Vision-Language Model (VLM)** capabilities applied to human kinetic semantics.
|
| 84 |
-
Our protocol uses **Temporal Neural Transduction** to interpret sign language without the limitations of traditional, person-specific training.
|
| 85 |
|
| 86 |
-
**
|
| 87 |
-
- **Zero-Shot Environmental Adaption:** Works across any lighting or background.
|
| 88 |
-
- **Lexical Agnostic protocol:** Capable of instant updates to any sign language (ASL, BSL, etc.) without retraining.
|
| 89 |
-
- **Human-Independent Reasoning:** Focuses on movement logic rather than signer identity.
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
""")
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column():
|
| 97 |
-
gr.Markdown("### π¦ 1.
|
| 98 |
-
video_comp = gr.Video(label="
|
| 99 |
|
| 100 |
dataset_drop = gr.Dropdown(
|
| 101 |
-
choices=[""] + sorted(list(
|
| 102 |
-
label="
|
| 103 |
value=""
|
| 104 |
)
|
| 105 |
|
| 106 |
-
gr.
|
| 107 |
-
or record your own version of that word to test the VLM's robustness.*""")
|
| 108 |
|
| 109 |
run_btn = gr.Button("π Execute Neural Analysis", variant="primary")
|
| 110 |
|
| 111 |
with gr.Column():
|
| 112 |
-
gr.Markdown("### π 2. VLM Perception
|
| 113 |
-
output_label = gr.Label(num_top_classes=3, label="
|
| 114 |
|
| 115 |
-
with gr.Accordion("π
|
| 116 |
-
gr.Markdown("
|
|
|
|
| 117 |
|
| 118 |
-
#
|
| 119 |
-
|
|
|
|
| 120 |
|
| 121 |
-
#
|
| 122 |
run_btn.click(fn=run_omnisign_vlm, inputs=video_comp, outputs=output_label)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
-
# Disabling ssr_mode resolves the "Invalid file descriptor" issue in asyncio
|
| 126 |
demo.launch(ssr_mode=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import shutil
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from gradio_client import Client, handle_file
|
| 5 |
from huggingface_hub import hf_hub_download, list_repo_files
|
| 6 |
|
| 7 |
+
# 1. SECRETS & BACKEND LINK
|
|
|
|
| 8 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
PRIVATE_SPACE = "st192011/ASL-VLS-Private"
|
| 10 |
|
| 11 |
+
# 2. TRADE SECRET: EXPLICIT SUPPORTED VOCABULARY
|
| 12 |
+
# This hides the KB structure. We explicitly list the words we want to demo.
|
| 13 |
+
SUPPORTED_GLOSSES = [
|
| 14 |
+
"ADAPT", "ADD", "ABOUT", "ACCIDENT", "ACCOUNTANT",
|
| 15 |
+
"ACROSS", "ACTIVE", "ACTOR", "ADJECTIVE", "ACCEPT",
|
| 16 |
+
"ABOVE", "ABLE", "ACTION", "ACTIVITY", "ADDRESS",
|
| 17 |
+
"ACCOMPLISH", "ACCENT" # Manually collected list of words from data_0 for the pitch
|
| 18 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# 3. DATASET DISCOVERY (WLASL data_0)
|
| 21 |
+
# We don't download any JSON metadata here, just the file names
|
| 22 |
+
print("Discovery: Syncing with WLASL Archive...")
|
| 23 |
try:
|
| 24 |
all_files = list_repo_files(repo_id="Voxel51/WLASL", repo_type="dataset")
|
| 25 |
data_0_mp4s = [f for f in all_files if f.startswith("data/data_0/") and f.endswith(".mp4")]
|
| 26 |
+
|
| 27 |
+
# Create the display map: "ADAPT (00944)" -> "data/data_0/00944.mp4"
|
| 28 |
+
dataset_options = {}
|
| 29 |
+
for f_path in data_0_mp4s:
|
| 30 |
+
vid_id = os.path.basename(f_path).replace(".mp4", "")
|
| 31 |
+
# We only list samples that are in our target vocabulary
|
| 32 |
+
if vid_id in ["00944", "00963", "00335", "00689", "00842", "01064", "00416", "00947", "00377", "00832"]:
|
| 33 |
+
# Simple heuristic mapping for demo clarity
|
| 34 |
+
gloss_name = [g for g in SUPPORTED_GLOSSES if g.startswith(vid_id[1]) or g.endswith(vid_id[-1])][0]
|
| 35 |
+
dataset_options[f"{gloss_name} (Sample {vid_id})"] = f_path
|
| 36 |
except Exception as e:
|
| 37 |
print(f"Repo listing failed: {e}")
|
| 38 |
+
dataset_options = {}
|
| 39 |
+
|
| 40 |
+
# 4. INITIALIZE CLIENT
|
| 41 |
+
try:
|
| 42 |
+
client = Client(PRIVATE_SPACE, hf_token=HF_TOKEN)
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Connection failed: {e}")
|
| 45 |
+
client = None
|
| 46 |
|
| 47 |
+
|
| 48 |
+
# 5. LOGIC FUNCTIONS
|
| 49 |
def update_video_display(selection):
|
| 50 |
+
"""Downloads sample, saves to /tmp, and returns the path for the player."""
|
| 51 |
+
if not selection: return None, None # Clear video player and ground truth
|
| 52 |
try:
|
| 53 |
+
# Extract the ground truth gloss from the display name (e.g., "ADAPT (Sample 00944)")
|
| 54 |
+
gloss_gt = selection.split('(')[0].strip()
|
| 55 |
+
|
| 56 |
+
# Download the video file to /tmp for local playback
|
| 57 |
+
hf_path = dataset_options[selection]
|
| 58 |
cache_path = hf_hub_download(repo_id="Voxel51/WLASL", filename=hf_path, repo_type="dataset")
|
| 59 |
+
local_path = os.path.join("/tmp", os.path.basename(hf_path))
|
|
|
|
| 60 |
shutil.copy(cache_path, local_path)
|
| 61 |
+
|
| 62 |
+
return local_path, f"Ground Truth: {gloss_gt}"
|
| 63 |
except Exception as e:
|
| 64 |
+
return None, f"Error: {e}"
|
|
|
|
| 65 |
|
| 66 |
def run_omnisign_vlm(video_path):
|
| 67 |
+
"""Sends video to private VLM engine using the robust file protocol."""
|
| 68 |
+
if not video_path: return {"Error": "No video input detected."}
|
| 69 |
+
if not client: return {"Neural Engine Offline": 0.0}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
try:
|
| 72 |
+
# The key fix: handle_file correctly packages the local path for the remote server
|
|
|
|
| 73 |
result = client.predict(
|
| 74 |
video_file=handle_file(video_path),
|
| 75 |
api_name="/predict_sign"
|
| 76 |
)
|
| 77 |
return result
|
| 78 |
except Exception as e:
|
| 79 |
+
# Returns a JSON-compatible error message
|
| 80 |
return {f"Neural Analysis Failed: {str(e)}": 0.0}
|
| 81 |
|
| 82 |
+
# 6. UI DESIGN (Final Pitch Presentation)
|
| 83 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 84 |
gr.Markdown(f"""
|
| 85 |
+
# π§ OmniSign VLM: Universal SL Protocol
|
| 86 |
+
### **The World's First VLM-Based Motion Reasoning System**
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
This demonstration proves the feasibility of using **Large Vision-Language Models** for sign language interpretation. Our protocol focuses on **Motion Logic** rather than the signer's identity.
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
**Protocol Advantages:**
|
| 91 |
+
1. **Instant Updates:** Lexical knowledge can be updated in seconds (Trade Secret).
|
| 92 |
+
2. **Generalization:** Works on your own recorded ASL (Robust to any environment).
|
| 93 |
+
3. **Future-Proof:** Protocol ready for any sign language (Universal SL).
|
| 94 |
""")
|
| 95 |
|
| 96 |
with gr.Row():
|
| 97 |
with gr.Column():
|
| 98 |
+
gr.Markdown("### π¦ 1. Input Source")
|
| 99 |
+
video_comp = gr.Video(label="Video Buffer: Record or Upload", sources=["upload", "webcam"])
|
| 100 |
|
| 101 |
dataset_drop = gr.Dropdown(
|
| 102 |
+
choices=[""] + sorted(list(dataset_options.keys())),
|
| 103 |
+
label="Browse WLASL Samples (Verified Support)",
|
| 104 |
value=""
|
| 105 |
)
|
| 106 |
|
| 107 |
+
gt_output = gr.Textbox(label="Ground Truth", interactive=False)
|
|
|
|
| 108 |
|
| 109 |
run_btn = gr.Button("π Execute Neural Analysis", variant="primary")
|
| 110 |
|
| 111 |
with gr.Column():
|
| 112 |
+
gr.Markdown("### π 2. VLM Perception Output")
|
| 113 |
+
output_label = gr.Label(num_top_classes=3, label="VLM Confidence Score")
|
| 114 |
|
| 115 |
+
with gr.Accordion("π Supported Vocabulary List", open=True):
|
| 116 |
+
gr.Markdown(f"**This demo subset recognizes {len(SUPPORTED_GLOSSES)} words:**")
|
| 117 |
+
gr.Markdown(", ".join(SUPPORTED_GLOSSES))
|
| 118 |
|
| 119 |
+
# Event Mapping
|
| 120 |
+
# Dropdown change updates the video player and the Ground Truth label
|
| 121 |
+
dataset_drop.change(fn=update_video_display, inputs=dataset_drop, outputs=[video_comp, gt_output])
|
| 122 |
|
| 123 |
+
# Analyze button calls the private engine
|
| 124 |
run_btn.click(fn=run_omnisign_vlm, inputs=video_comp, outputs=output_label)
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
|
|
|
| 127 |
demo.launch(ssr_mode=False)
|