MeysamSh commited on
Commit
8f5d1a0
Β·
1 Parent(s): 67ae4b6

Add application file

Browse files
Files changed (1) hide show
  1. app.py +119 -4
app.py CHANGED
@@ -1,7 +1,122 @@
 
1
  import gradio as gr
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import gradio as gr
3
+ import hashlib
4
+ from datetime import datetime
5
+ from huggingface_hub import HfApi
6
+ from pathlib import Path
7
 
8
+ # --- CONFIGURATION ---
9
+ DATASET_REPO_ID = "MeysamSh/ENSIMSoundDataCollection"
10
+ HF_TOKEN = os.environ.get("HF_TOKEN")
11
 
12
+ # Admin Credentials (Keep these secure!)
13
+ ADMIN_USERNAME = "admin"
14
+ ADMIN_PASSWORD = "30c8663d3ca10ededd17ac1b55f3d533ab29cf1b8470b1729af09afda3f0a516"
15
+
16
+ AUTHORIZED_USERS = [
17
+ "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
18
+ "test@univ-lemans.fr"
19
+ ]
20
+
21
+ api = HfApi()
22
+
23
+ # --- LOGIC FUNCTIONS ---
24
+
25
+ def verify_user(email):
26
+ if not email: return gr.update(visible=False), "⚠️ Enter email."
27
+ clean_email = email.strip().lower()
28
+ email_hash = hashlib.sha256(clean_email.encode()).hexdigest()
29
+ if clean_email in AUTHORIZED_USERS or email_hash in AUTHORIZED_USERS:
30
+ return gr.update(visible=True), f"βœ… Access Granted: {clean_email}"
31
+ return gr.update(visible=False), "🚫 Not authorized."
32
+
33
+ def upload_data(email, label, audio_path):
34
+ if not audio_path: return "⚠️ No audio recorded."
35
+ try:
36
+ # email_hash = hashlib.sha256(email.strip().lower().encode()).hexdigest()
37
+ email_index = AUTHORIZED_USERS.index(email.strip().lower()) if email.strip().lower() in AUTHORIZED_USERS else -1
38
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
39
+ unique_filename = f"{email_index}_{timestamp}.wav"
40
+
41
+ # Upload Audio
42
+ api.upload_file(path_or_fileobj=audio_path, path_in_repo=f"data/{unique_filename}",
43
+ repo_id=DATASET_REPO_ID, repo_type="dataset", token=HF_TOKEN)
44
+ # Upload Metadata
45
+ meta_content = f"user_id,label,file_name,timestamp\n{email.strip().lower()},{label},{unique_filename},{timestamp}"
46
+ api.upload_file(path_or_fileobj=meta_content.encode(), path_in_repo=f"metadata/meta_{email_index}_{timestamp}.csv",
47
+ repo_id=DATASET_REPO_ID, repo_type="dataset", token=HF_TOKEN)
48
+ return f"πŸŽ‰ Success! Audio saved as {label}."
49
+ except Exception as e: return f"❌ Error: {str(e)}"
50
+
51
+ # --- ADMIN LOGIC ---
52
+
53
+ def admin_login(user, pwd):
54
+ pwd_hash = hashlib.sha256(pwd.encode()).hexdigest()
55
+ if user == ADMIN_USERNAME and pwd_hash == ADMIN_PASSWORD:
56
+ files = api.list_repo_files(repo_id=DATASET_REPO_ID, repo_type="dataset")
57
+ audio_files = [f for f in files if f.startswith("data/")]
58
+ return gr.update(visible=True), gr.update(choices=audio_files), "πŸ”“ Admin Authenticated"
59
+ return gr.update(visible=False), gr.update(choices=[]), "❌ Invalid Credentials"
60
+
61
+ def delete_selected_file(file_path):
62
+ if not file_path: return "⚠️ Select a file."
63
+ try:
64
+ # Delete the audio file
65
+ api.delete_file(path_in_repo=file_path, repo_id=DATASET_REPO_ID, repo_type="dataset", token=HF_TOKEN)
66
+
67
+ # Attempt to delete corresponding metadata
68
+ meta_path = file_path.replace("data/", "metadata/meta_").replace(".wav", ".csv")
69
+ try:
70
+ api.delete_file(path_in_repo=meta_path, repo_id=DATASET_REPO_ID, repo_type="dataset", token=HF_TOKEN)
71
+ except: pass
72
+
73
+ # Refresh file list
74
+ files = api.list_repo_files(repo_id=DATASET_REPO_ID, repo_type="dataset")
75
+ audio_files = [f for f in files if f.startswith("data/")]
76
+ return f"πŸ—‘οΈ Deleted {file_path}", gr.update(choices=audio_files)
77
+ except Exception as e: return f"❌ Error: {str(e)}", gr.update()
78
+
79
+ # --- UI ---
80
+
81
+ with gr.Blocks() as demo:
82
+ gr.Markdown("# πŸŽ™οΈ ENSIM Sound Data Platform")
83
+
84
+ with gr.Tabs():
85
+ # STUDENT TAB
86
+ with gr.TabItem("Student Recording"):
87
+ with gr.Row():
88
+ email_input = gr.Textbox(label="Email", placeholder="test@univ-lemans.fr")
89
+ login_btn = gr.Button("Verify", variant="primary")
90
+ login_status = gr.Markdown("Waiting for login...")
91
+
92
+ with gr.Column(visible=False) as recording_zone:
93
+ # gr.Separator()
94
+ label_input = gr.Radio(choices=["Engine", "Environmental", "Mechanical"], label="Category")
95
+ audio_input = gr.Audio(label="Record (40s)", sources=["microphone"], type="filepath")
96
+ submit_btn = gr.Button("πŸš€ Submit", variant="primary")
97
+ final_status = gr.Textbox(label="Status", interactive=False)
98
+
99
+ # ADMIN TAB
100
+ with gr.TabItem("Administration"):
101
+ with gr.Row():
102
+ admin_user = gr.Textbox(label="Admin Username")
103
+ admin_pass = gr.Textbox(label="Admin Password", type="password")
104
+ admin_login_btn = gr.Button("Login Admin")
105
+
106
+ admin_msg = gr.Markdown("Log in to manage files.")
107
+
108
+ with gr.Column(visible=False) as admin_panel:
109
+ # gr.Separator()
110
+ file_dropdown = gr.Dropdown(label="Select File to Remove", choices=[])
111
+ delete_btn = gr.Button("πŸ—‘οΈ Delete Selected File", variant="stop")
112
+ delete_status = gr.Textbox(label="Delete Progress")
113
+
114
+ # --- EVENT HANDLERS ---
115
+ login_btn.click(verify_user, [email_input], [recording_zone, login_status])
116
+ submit_btn.click(upload_data, [email_input, label_input, audio_input], final_status)
117
+
118
+ admin_login_btn.click(admin_login, [admin_user, admin_pass], [admin_panel, file_dropdown, admin_msg])
119
+ delete_btn.click(delete_selected_file, [file_dropdown], [delete_status, file_dropdown])
120
+
121
+ if __name__ == "__main__":
122
+ demo.launch(theme=gr.themes.Soft())