File size: 12,695 Bytes
a02cf38 9bc4dda a02cf38 8cd1106 a02cf38 9bc4dda 4bc60dd a02cf38 4bc60dd a02cf38 4bc60dd a02cf38 4bc60dd a02cf38 4bc60dd a02cf38 4bc60dd a02cf38 4bc60dd a02cf38 4bc60dd a02cf38 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
import shutil
import gradio as gr
from supabase import create_client, Client
from supabase.client import ClientOptions
import os
from supabase_auth.errors import (
AuthWeakPasswordError,
AuthInvalidCredentialsError,
AuthApiError,
)
url = os.environ['url']
key = os.environ['key']
supabase = create_client(url, key)
user_country = "السعودية"
UNIVERSITIES = [
"جامعة طيبة",
"جامعة الملك فهد للبترول والمعادن"
]
COUNTRY = [
"السعودية",
"مصر"
]
CITIES = ["مكة", "المدينة", "الرياض", "جدة"]
DIALECTS = ["اللهجة الحجازية", "اللهجة النجدية", "اللهجة الخليجية"]
GENDER = [
"ذكر",
"أنثى"
]
AGE = [
"18-24",
"25-34",
"35-44",
"45-54",
"55-64",
"65+"
]
SENTENCES = [
file.strip() for file in open("sentences.txt", "r", encoding="utf-8").readlines()
]
idx = 0
USER_ID = ""
TERMS = """
الاتفاقية دي ما بين المشارك (حضرتك) وبين فريق البحث من جامعة الملك فهد للبترول والمعادن وجامعة طيبة (اللي هنسميه هنا "الجامعين"). الاتفاقية دي مخصوص علشان جمع واستخدام وتوزيع تسجيلات صوتية هتساعد في أبحاث كشف الأصوات المزوّرة (Deepfake) وغيرها من الأبحاث غير التجارية.
١. هدف جمع البيانات:
الفريق بيجمع تسجيلات صوت علشان يعمل مجموعة بيانات (Dataset) خاصة بالكشف عن الأصوات المصنوعة بالذكاء الاصطناعي باستخدام تقنيات تحويل النص لصوت (TTS) أو تقليد الأصوات (Voice Conversion) وطرق أخري. البيانات دي هتتستخدم في أبحاث علمية وأكاديمية لتطوير طرق أفضل لاكتشاف الأصوات المزوّرة وغيرها من الأبحاث غير التجارية.
٢. طبيعة البيانات اللي هتتجمع:
المشارك يوافق إنه يدي:
تسجيلات صوتية بصوته الطبيعي أو من خلال نصوص/جمل هنطلب منه يقراها.
بيانات اختيارية زى: النوع (ذكر/أنثى)، الفئة العمرية، اللهجة، ألخ
موافقة إن صوته يتعدل أو يتغيّر بأساليب صناعية
٣. الحقوق الممنوحة:
المشارك بيدّي الفريق الحق الكامل (من غير فلوس أو قيود) إنه:
يسجّل ويعالج ويستخدم الصوت الطبيعي والنسخ المصنوعة منه.
يوزّع مجموعة البيانات دي (الطبيعية والمصنوعة) للباحثين في المجتمع العلمي، لأغراض بحثية غير تجارية وبحثية فقط.
ينشر عينات صوتية على منصات مهنية أو أكاديمية زي: LinkedIn، Twitter/X، YouTube علشان يزود الوعي بأبحاث الديب فيك أو يعرّف الناس عن توفر البيانات.
٤. إتاحة البيانات:
المجموعة الصوتية (الطبيعية والمصنوعة) هتتنشر بترخيص مفتوح (Creative Commons Attribution 4.0) يسمح لأي باحث يستخدمها ويشاركها للأغراض الأكاديمية غير التجارية.
٥. الخصوصية والسرية:
مش هيتم نشر اسم المشارك أو أي بيانات شخصية مباشرة إلا بموافقته المكتوبة.
المشارك هيكون ليه ID مجهول في مجموعة البيانات.
٦. المشاركة والانضمام:
المشاركة اختيارية ١٠٠٪.
المشارك له الحق ينسحب أو يطلب حذف تسجيلاته قبل ما مجموعة البيانات تنشر للعامة.
بعد النشر العام، سحب البيانات مش هيكون ممكن بسبب طريقة توزيعها.
٧. التعويض:
المشارك عارف إن مفيش أي مقابل مادي أو فلوس مقابل تسجيلاته، والمشاركة هنا علشان دعم وتطوير البحث العلمي بس.
بمجرد تسجيلك على الموقع معناه موافقتك على جميع الشروط والاتفاقيات المطلوبة
المذكورة أعلاه. لو في أي استفسار أو محتاج توضيح، ممكن تبعتلنا على الإيميل:a.a.elghawas@gmail.com
"""
def sign_up(su_name, su_email, su_pw, su_pw2, university, country, city, dialect, gender, age):
global supabase
if(su_pw == su_pw2):
try:
response = supabase.auth.sign_up(
{
"email": su_email,
"password": su_pw,
}
)
print(response.user.id)
# print(type(response.user.id))
uuid = response.user.id
data = {
"id": uuid,
"university": university,
"country": country,
"city": city,
"dialect": dialect,
"gender": gender,
"age": age,
"idx": 0
}
print("HEERER")
try:
session = response.session
supabase.postgrest.auth(session.access_token)
supabase.table("users").insert(data).execute()
except Exception as e:
print("Error inserting user data:", e)
# u_id = reponse.user.id # get user id from response
# add_user(authed,"Tibah","Saudi","Maka","Hiajazi", "male") # add user to db with empty fields
return(
"✅ تم إنشاء الحساب بنجاح, يمكنك تسجيل الدخول الآن"
)
except AuthWeakPasswordError as e:
return(
"⚠️ كلمة السر غير مقبولة. استخدم علي الأقل 6 أرقام"
)
except Exception as e:
if(str(e) == "User already registered"):
return(
"⚠️ هذا المستخدم موجود بالفعل, قم بتسجيل الدخول علي حسابك"
)
elif(str(e) == "Unable to validate email address: invalid format"):
return(
"⚠️ يوجد خطأ بالإيميل, يرجي إعادة الإدخال مرة أخري بصورة صحيحة"
)
else:
return(
"⚠️ كلمة السر غير متطابقة" # goes to msg
)
def sign_in(si_email, si_pw):
global supabase, USER_ID, idx
try:
response = supabase.auth.sign_in_with_password(
{
"email": si_email,
"password": si_pw,
}
)
USER_ID = response.user.id
session = response.session
supabase.postgrest.auth(session.access_token)
idx = supabase.table("users").select("idx").eq('id', USER_ID).execute().data[0]["idx"]
print("IDDXXXXXXXX",idx)
return(
gr.update(visible=False),
gr.update(visible=True),
f"✅ هلا {si_email}!",
gr.update(value=SENTENCES[idx])
)
except Exception as e:
print(e)
return(
gr.update(visible=True),
gr.update(visible=False),
"⚠️ هناك خطأ بالإيميل أو كلمة السر, في حالة عدم وجود حساب يرجي إنشاء حساب أولاً",
gr.update(value="")
)
def save_audio(audio):
print(audio)
# temp_path = audio # temp file
# USER_ID = supabase.auth.get_user().user.id
response = (
supabase.table("users")
.select("*")
.eq('id', USER_ID)
.execute()
)
print(response)
idx = response.data[0]["idx"]
save_path = f"{USER_ID}/audio_{idx}.wav" # where you want to save it
# shutil.copy(temp_path, save_path)
try:
with open(audio, "rb") as f:
response = (
supabase.storage
.from_("files")
.upload(
file=f,
path=save_path,
file_options={"cache-control": "3600", "upsert": "false"}
)
)
idx = (idx + 1) % len(SENTENCES)
response = (
supabase.table("users")
.update({"idx": idx})
.eq("id", USER_ID)
.execute()
)
return (
"🎉 تم التسجيل بنجاح.. استمر!",
None,
SENTENCES[idx]
)
except Exception as e:
print(e)
return (
"⚠️ هناك مشكلة في حفظ التسجيل, حاول مرة أخري",
None,
SENTENCES[idx]
)
def skip_audio():
response = (
supabase.table("users")
.select("*")
.eq('id', USER_ID)
.execute()
)
print(response)
idx = response.data[0]["idx"]
idx = (idx + 1) % len(SENTENCES)
response = (
supabase.table("users")
.update({"idx": idx})
.eq("id", USER_ID)
.execute()
)
return (
"⏭️ تم تخطي التسجيل.. استمر!",
None,
SENTENCES[idx]
)
with gr.Blocks() as demo:
msg = gr.Markdown("", visible=False)
# --- AUTH UI ---
with gr.Group(visible=True) as auth_box:
with gr.Tabs():
with gr.Tab("تسجيل الدخول"):
with gr.Group():
si_email = gr.Textbox(label="الإيميل", placeholder="you@example.com")
si_pw = gr.Textbox(label="كلمة السر", type="password", placeholder="••••••••")
si_btn = gr.Button("تسجيل الدخول", variant="primary")
si_msg = gr.Markdown("") # <— this is a COMPONENT
# si_btn.click(sign_in, [si_email, si_pw], [auth_box, si_msg])
with gr.Tab("إنشاء حساب"):
with gr.Group():
su_name = gr.Textbox(label="الاسم", placeholder="Your name")
su_email = gr.Textbox(label="الإيميل", placeholder="you@example.com")
su_pw = gr.Textbox(label="كلمة السر", type="password")
su_pw2 = gr.Textbox(label="تأكيد كلمة السر", type="password")
university = gr.Dropdown(choices=UNIVERSITIES, label="اختر الجامعة")
country = gr.Dropdown(choices=COUNTRY, label="اختر الدولة", value="السعودية", interactive=False)
city = gr.Dropdown(choices=CITIES, label="اختر المدينة")
dialect = gr.Dropdown(choices=DIALECTS, label="اختر اللهجة")
gender = gr.Dropdown(choices=GENDER, label="اختر النوع", value="ذكر")
age = gr.Dropdown(choices=AGE, label="اختر الفئة العمرية", value="25-34")
with gr.Accordion(label="⚠️ سياسة تجميع البيانات والاستخدام", open=True):
gr.Markdown(TERMS)
su_btn = gr.Button("إنشاء حساب", variant="primary")
su_msg = gr.Markdown("") # <— this is a COMPONENT
# --- MAIN APP (hidden initially) ---
with gr.Group(visible=False) as app_box:
gr.Markdown("## 🇸🇦🎤 تسجيل الجمل الصوتية بلهجتك")
text_sample = gr.Textbox(label="الجملة المطلوب تسجيلها", value=SENTENCES[idx])
audio = gr.Audio(sources="microphone", type="filepath")
output = gr.Textbox(label="الحالة", value="")
with gr.Row():
btn_skip = gr.Button("تخطي", variant="secondary")
btn_save = gr.Button("حفظ التسجيل", variant="primary")
# Connect buttons to functions
su_btn.click(sign_up, [su_name, su_email, su_pw, su_pw2, university, country, city, dialect, gender, age], [su_msg])
btn_save.click(save_audio,[audio],[output, audio, text_sample])
si_btn.click(sign_in, [si_email, si_pw], [auth_box,app_box,si_msg, text_sample])
btn_skip.click(skip_audio, [], [output, audio, text_sample])
# Connect button
# btn.click(sign_in, [email, pw], [auth_box, app_box, msg])
# msg.render()
if __name__ == "__main__":
demo.queue().launch() |