Update app.py
Browse files44 unmodified lines454647484950517 unmodified lines596061626364656667686970717273747576777879808182838485868788899030 unmodified lines12112212312412512612712812913013113213313413513613713813964 unmodified lines20420520620720820921011 unmodified lines2222232242252262272282292302312322332342354 unmodified lines24024124224324424524624744 unmodified linesreturn tags# 4.[๊ด๋ฆฌ์์ฉ] ์ฑ์ฐ ๋ฐ์ดํฐ ๋ฑ๋ก (์๋ ๋ฉํ๋ฐ์ดํฐ ์ถ๊ฐ)def register_voice(actor_name, audio_file, gender, age, atmosphere, content):if not actor_name or audio_file is None:return "์ฑ์ฐ ์ด๋ฆ๊ณผ ์ค๋์ค ํ์ผ์ ๋ชจ๋ ์ ๋ ฅํด์ฃผ์ธ์."7 unmodified lineswith open(audio_file, "rb") as f:audio_b64 = base64.b64encode(f.read()).decode("utf-8")# ์๋ ํ๊ทธ ๋ฐ ์๋ ํ๊ทธ ๊ฒฐํฉ ๋ณด๊ด์ฉnew_data = {"actor_name": [actor_name],"tags":[json.dumps(auto_tags)],"embedding": [json.dumps(embedding)],"registered_at": [datetime.now().strftime("%Y-%m-%d %H:%M:%S")],"audio_b64": [audio_b64],"gender": [gender if gender else ""],"age": [age if age else ""],"atmosphere": [json.dumps(atmosphere if atmosphere else [])],"content": [json.dumps(content if content else [])]}try:try:ds = load_dataset(DATASET_REPO, split="train")new_ds = Dataset.from_dict(new_data)from datasets import concatenate_datasetsupdated_ds = concatenate_datasets([ds, new_ds])except Exception:updated_ds = Dataset.from_dict(new_data)updated_ds.push_to_hub(DATASET_REPO, token=HF_TOKEN)return f"โ [{actor_name}] ์ฑ์ฐ ๋ฑ๋ก ์๋ฃ! ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค."except Exception as e:return f"โ ๋ฑ๋ก ์คํจ: {str(e)}"# 5.[์ฌ์ฉ์์ฉ] ์ ์ฌ๋ ๋ถ์def match_voice(user_audio):30 unmodified linesdef save_feedback(best_match, feedback_score):return f"[{best_match}] ์ถ์ฒ์ ๋ํ ํผ๋๋ฐฑ({feedback_score}์ )์ด ์์ ํ๊ฒ ์ ์ฅ๋์์ต๋๋ค."# 6.[๋ผ์ด๋ธ๋ฌ๋ฆฌ์ฉ] ํํฐ๋ง ๋ฐ ๋ถ๋ฌ์ค๊ธฐ ๊ธฐ๋ฅdef get_filtered_actors(genders, ages, atmos, contents):try:ds = load_dataset(DATASET_REPO, split="train")filtered_list =[]for row in ds:# ์ด์ ๋ฐ์ดํฐ ํธํ์ฑ์ ์ํ ์ฒ๋ฆฌ (.get ์ฌ์ฉ)r_gender = row.get("gender", "")r_age = row.get("age", "")r_atmos = json.loads(row.get("atmosphere", "[]")) if row.get("atmosphere") else[]r_content = json.loads(row.get("content", "[]")) if row.get("content") else[]# ํํฐ ์กฐ๊ฑด ๊ฒ์ฌ (์ ํ ์ ํ ํํฐ๋ ๋ฌด์ํ๊ณ ํต๊ณผ)if genders and r_gender not in genders: continueif ages and r_age not in ages: continueif atmos and not any(a in r_atmos for a in atmos): continue64 unmodified linesmatch_btn.click(fn=match_voice, inputs=user_audio, outputs=[result_text, matched_actor])feedback_btn.click(fn=save_feedback, inputs=[matched_actor, feedback_slider], outputs=feedback_out)# ํญ 2: ๋ผ์ด๋ธ๋ฌ๋ฆฌ (์์ธ ํํฐ๋ง ๊ธฐ๋ฅ ์ถ๊ฐ)with gr.Tab("์ฑ์ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (๋ฏธ๋ฆฌ๋ฃ๊ธฐ)"):gr.Markdown("์กฐ๊ฑด์ ์ ํํ๊ณ ํํฐ๋ฅผ ์ ์ฉํ์ฌ ์ํ๋ ์ฑ์ฐ์ ๋ชฉ์๋ฆฌ๋ฅผ ์ง์ ๋ค์ด๋ณด์ธ์.")with gr.Row():11 unmodified lineslibrary_audio = gr.Audio(label="๋ชฉ์๋ฆฌ ๋ฏธ๋ฆฌ๋ฃ๊ธฐ", interactive=False)library_tags = gr.Markdown("์ฑ์ฐ๋ฅผ ์ ํํ๋ฉด ์์ธ ์ ๋ณด๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค.")# ํํฐ ์ ์ฉ ๋ฒํผ ํด๋ฆญ ์ ๋๋กญ๋ค์ด ๋ชฉ๋ก ์ ๋ฐ์ดํธfilter_btn.click(fn=get_filtered_actors, inputs=[lib_gender, lib_age, lib_atmos, lib_content], outputs=actor_dropdown)# ๋๋กญ๋ค์ด์์ ์ฑ์ฐ ์ ํ ์ ์์ธ ์ ๋ณด ๋ถ๋ฌ์ค๊ธฐactor_dropdown.change(fn=load_actor_details, inputs=actor_dropdown, outputs=[library_audio, library_tags])# ํญ 3: ๊ด๋ฆฌ์ ํ๋ฉด (๋ฉํ๋ฐ์ดํฐ ์ ๋ ฅ ํผ ์ถ๊ฐ)with gr.Tab("๊ด๋ฆฌ์ ํญ (์ฑ์ฐ ๋ฑ๋ก)"):gr.Markdown("์๋ก์ด ์ฑ์ฐ ์ํ๊ณผ ์์ธ ๋ถ๋ฅ๋ฅผ ์ ํํ์ฌ DB์ ์๊ตฌ ์ ์ฅํฉ๋๋ค.")with gr.Row():with gr.Column(scale=1):admin_name = gr.Textbox(label="์ฑ์ฐ ์ด๋ฆ (ํ์)")4 unmodified linesadmin_atmos = gr.CheckboxGroup(choices=UI_ATMOS, label="๋ถ์๊ธฐ (๋ค์ค ์ ํ ๊ฐ๋ฅ)")admin_content = gr.CheckboxGroup(choices=UI_CONTENT, label="์ถ์ฒ ์ฝํ ์ธ (๋ค์ค ์ ํ ๊ฐ๋ฅ)")admin_btn = gr.Button("์ฑ์ฐ DB์ ๋ฑ๋กํ๊ธฐ", variant="primary")admin_out = gr.Textbox(label="๋ฑ๋ก ๊ฒฐ๊ณผ")admin_btn.click(fn=register_voice,44 unmodified lines454647484950517 unmodified lines5960616263646566676869707172737475767778798081828384858687888990919293949596979830 unmodified lines12913013113213313413513613713813914014114214314414514614714814915015115264 unmodified lines21721821922022122222311 unmodified lines2352362372382392402412422432442452462472482492502512522532544 unmodified lines25926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930044 unmodified linesreturn tags# 4.[๊ด๋ฆฌ์์ฉ] ์ฑ์ฐ ๋ฐ์ดํฐ ๋ฑ๋ก ๋ฐ ๋ฎ์ด์ฐ๊ธฐ(์์ ) ๊ธฐ๋ฅ ์ถ๊ฐdef register_voice(actor_name, audio_file, gender, age, atmosphere, content):if not actor_name or audio_file is None:return "์ฑ์ฐ ์ด๋ฆ๊ณผ ์ค๋์ค ํ์ผ์ ๋ชจ๋ ์ ๋ ฅํด์ฃผ์ธ์."7 unmodified lineswith open(audio_file, "rb") as f:audio_b64 = base64.b64encode(f.read()).decode("utf-8")new_data = {"actor_name":[actor_name],"tags":[json.dumps(auto_tags)],"embedding": [json.dumps(embedding)],"registered_at":[datetime.now().strftime("%Y-%m-%d %H:%M:%S")],"audio_b64": [audio_b64],"gender":[gender if gender else ""],"age": [age if age else ""],"atmosphere":[json.dumps(atmosphere if atmosphere else [])],"content":[json.dumps(content if content else [])]}try:try:ds = load_dataset(DATASET_REPO, split="train")# [์์ ๋ก์ง] ์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฆ์ด๋ฉด ๊ธฐ์กด ๋ฐ์ดํฐ ํํฐ๋ง(์ญ์ )if actor_name in ds["actor_name"]:ds = ds.filter(lambda x: x["actor_name"] != actor_name)msg_prefix = "๐ [์ ๋ณด ์์ ]"else:msg_prefix = "โ [์ ๊ท ๋ฑ๋ก]"new_ds = Dataset.from_dict(new_data)from datasets import concatenate_datasetsupdated_ds = concatenate_datasets([ds, new_ds])except Exception:updated_ds = Dataset.from_dict(new_data)msg_prefix = "โ [์ ๊ท ๋ฑ๋ก]"updated_ds.push_to_hub(DATASET_REPO, token=HF_TOKEN)return f"{msg_prefix} ์๋ฃ! [{actor_name}] ์ฑ์ฐ์ ์ ๋ณด๊ฐ ๋ฐ์๋์์ต๋๋ค."except Exception as e:return f"โ ์ค๋ฅ ๋ฐ์: {str(e)}"# 5.[์ฌ์ฉ์์ฉ] ์ ์ฌ๋ ๋ถ์def match_voice(user_audio):30 unmodified linesdef save_feedback(best_match, feedback_score):return f"[{best_match}] ์ถ์ฒ์ ๋ํ ํผ๋๋ฐฑ({feedback_score}์ )์ด ์์ ํ๊ฒ ์ ์ฅ๋์์ต๋๋ค."# 6.[๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ ๊ด๋ฆฌ์ ๊ณตํต์ฉ] ๋ชฉ๋ก ๋ฐ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ ๊ธฐ๋ฅdef get_actor_list():try:ds = load_dataset(DATASET_REPO, split="train")return [row["actor_name"] for row in ds]except:return[]def get_filtered_actors(genders, ages, atmos, contents):try:ds = load_dataset(DATASET_REPO, split="train")filtered_list =[]for row in ds:r_gender = row.get("gender", "")r_age = row.get("age", "")r_atmos = json.loads(row.get("atmosphere", "[]")) if row.get("atmosphere") else[]r_content = json.loads(row.get("content", "[]")) if row.get("content") else[]if genders and r_gender not in genders: continueif ages and r_age not in ages: continueif atmos and not any(a in r_atmos for a in atmos): continue64 unmodified linesmatch_btn.click(fn=match_voice, inputs=user_audio, outputs=[result_text, matched_actor])feedback_btn.click(fn=save_feedback, inputs=[matched_actor, feedback_slider], outputs=feedback_out)# ํญ 2: ๋ผ์ด๋ธ๋ฌ๋ฆฌwith gr.Tab("์ฑ์ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (๋ฏธ๋ฆฌ๋ฃ๊ธฐ)"):gr.Markdown("์กฐ๊ฑด์ ์ ํํ๊ณ ํํฐ๋ฅผ ์ ์ฉํ์ฌ ์ํ๋ ์ฑ์ฐ์ ๋ชฉ์๋ฆฌ๋ฅผ ์ง์ ๋ค์ด๋ณด์ธ์.")with gr.Row():11 unmodified lineslibrary_audio = gr.Audio(label="๋ชฉ์๋ฆฌ ๋ฏธ๋ฆฌ๋ฃ๊ธฐ", interactive=False)library_tags = gr.Markdown("์ฑ์ฐ๋ฅผ ์ ํํ๋ฉด ์์ธ ์ ๋ณด๊ฐ ์ฌ๊ธฐ์ ํ์๋ฉ๋๋ค.")filter_btn.click(fn=get_filtered_actors, inputs=[lib_gender, lib_age, lib_atmos, lib_content], outputs=actor_dropdown)actor_dropdown.change(fn=load_actor_details, inputs=actor_dropdown, outputs=[library_audio, library_tags])# ํญ 3: ๊ด๋ฆฌ์ ํ๋ฉดwith gr.Tab("๊ด๋ฆฌ์ ํญ (์ฑ์ฐ ๋ฑ๋ก ๋ฐ ์์ )"):gr.Markdown("์๋ก์ด ์ฑ์ฐ๋ฅผ ๋ฑ๋กํ๊ฑฐ๋ ๊ธฐ์กด ์ฑ์ฐ์ ์ ๋ณด๋ฅผ ์์ ํ ์ ์์ต๋๋ค.")# [์ ๊ท ์ถ๊ฐ] ๊ธฐ์กด ์ ๋ณด ๋ถ๋ฌ์ค๊ธฐ UIwith gr.Row():admin_edit_dropdown = gr.Dropdown(choices=[], label="โ๏ธ ๊ธฐ์กด ์ฑ์ฐ ์ ๋ณด ์์ ํ๊ธฐ (์ ํ ์ ์๋ ํผ์ ๋ถ๋ฌ์ต๋๋ค)")admin_refresh_btn = gr.Button("๋ชฉ๋ก ์๋ก๊ณ ์นจ")gr.Markdown("---")with gr.Row():with gr.Column(scale=1):admin_name = gr.Textbox(label="์ฑ์ฐ ์ด๋ฆ (ํ์)")4 unmodified linesadmin_atmos = gr.CheckboxGroup(choices=UI_ATMOS, label="๋ถ์๊ธฐ (๋ค์ค ์ ํ ๊ฐ๋ฅ)")admin_content = gr.CheckboxGroup(choices=UI_CONTENT, label="์ถ์ฒ ์ฝํ ์ธ (๋ค์ค ์ ํ ๊ฐ๋ฅ)")admin_btn = gr.Button("์ฑ์ฐ DB์ ๋ฑ๋ก/์์ ํ๊ธฐ", variant="primary")admin_out = gr.Textbox(label="์ฒ๋ฆฌ ๊ฒฐ๊ณผ")# ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ๋ค ์ฐ๊ฒฐadmin_refresh_btn.click(fn=lambda: gr.Dropdown(choices=get_actor_list()), outputs=admin_edit_dropdown)# ๊ธฐ์กด ์ฑ์ฐ ์ ํ ์ ํผ์ ๋ฐ์ดํฐ ์ฑ์๋ฃ๊ธฐ ํจ์def load_for_admin_edit(name):if not name: return name, None, None, None, [],[]try:ds = load_dataset(DATASET_REPO, split="train")for row in ds:if row["actor_name"] == name:r_gender = row.get("gender", None)r_age = row.get("age", None)r_atmos = json.loads(row.get("atmosphere", "[]")) if row.get("atmosphere") else[]r_content = json.loads(row.get("content", "[]")) if row.get("content") else[]audio_path = Noneif "audio_b64" in row and row["audio_b64"]:audio_bytes = base64.b64decode(row["audio_b64"])temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")temp_file.write(audio_bytes)temp_file.close()audio_path = temp_file.namereturn name, audio_path, r_gender, r_age, r_atmos, r_contentexcept:passreturn name, None, None, None, [],[]admin_edit_dropdown.change(fn=load_for_admin_edit,inputs=admin_edit_dropdown,outputs=[admin_name, admin_audio, admin_gender, admin_age, admin_atmos, admin_content])admin_btn.click(fn=register_voice,
app.py+73-20