Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,19 +5,19 @@ import re
|
|
| 5 |
|
| 6 |
def load_random_conversations():
|
| 7 |
"""
|
| 8 |
-
๋ฐ์ดํฐ์
'gaeunseo/Taskmaster_sample_data'์ train split์์
|
| 9 |
-
conversation_id๋ณ๋ก ๊ทธ๋ฃนํํ ํ, ๋ชจ๋ ํ์ used
|
| 10 |
-
์ด ์ค ๋๋คํ๊ฒ 2๊ฐ ๊ทธ๋ฃน์ ์ ํํ์ฌ ๊ฐ
|
| 11 |
"""
|
| 12 |
ds = load_dataset("gaeunseo/Taskmaster_sample_data")["train"]
|
| 13 |
|
| 14 |
-
# conversation_id
|
| 15 |
groups = {}
|
| 16 |
for row in ds:
|
| 17 |
cid = row["conversation_id"]
|
| 18 |
groups.setdefault(cid, []).append(row)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
valid_groups = [grp for grp in groups.values() if all(not row["used"] for row in grp)]
|
| 22 |
|
| 23 |
if len(valid_groups) < 2:
|
|
@@ -26,48 +26,58 @@ def load_random_conversations():
|
|
| 26 |
# ์ ํจํ ๊ทธ๋ฃน ์ค ๋๋คํ๊ฒ 2๊ฐ ์ ํ
|
| 27 |
selected_groups = random.sample(valid_groups, 2)
|
| 28 |
|
| 29 |
-
# ๊ฐ ๊ทธ๋ฃน์ ๋ชจ๋ utterance๋ฅผ ์ด์ด๋ถ์
|
| 30 |
conv_A = "\n".join(row["utterance"] for row in selected_groups[0])
|
| 31 |
conv_B = "\n".join(row["utterance"] for row in selected_groups[1])
|
| 32 |
|
| 33 |
return conv_A, conv_B
|
| 34 |
|
| 35 |
-
def
|
| 36 |
"""
|
| 37 |
-
conv_text ๋ฌธ์์ด์ [turn]
|
| 38 |
-
|
| 39 |
-
gr.Chatbot ์ปดํฌ๋ํธ์์ ์ฌ์ฉํ๋ (user_message, ai_message) ํํ ๋ฆฌ์คํธ๋ก ๋ณํ.
|
| 40 |
|
| 41 |
-
-
|
| 42 |
-
-
|
| 43 |
"""
|
| 44 |
-
# [turn]์ [BC]
|
| 45 |
utterances = re.split(r'\[turn\]|\[BC\]', conv_text)
|
| 46 |
-
# ๊ณต๋ฐฑ ์ ๊ฑฐ ๋ฐ ๋น ๋ฌธ์์ด ์ ๊ฑฐ
|
| 47 |
utterances = [utt.strip() for utt in utterances if utt.strip()]
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
def load_and_format_conversations():
|
| 62 |
"""
|
| 63 |
-
๋ฐ์ดํฐ์
์์ ๋๋คํ๊ฒ ๋ ๋ํ ๋ฌธ์์ด์ ๊ฐ์ ธ์จ ํ, ๊ฐ๊ฐ
|
| 64 |
-
์ฑํ
|
| 65 |
"""
|
| 66 |
conv_A, conv_B = load_random_conversations()
|
| 67 |
-
# ์๋ฌ ๋ฉ์์ง์ธ ๊ฒฝ์ฐ ๊ทธ๋๋ก ๋ฐํ
|
| 68 |
if conv_A.startswith("Not enough"):
|
| 69 |
-
return conv_A, conv_B
|
| 70 |
-
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# ํ๊ฐ ๋ฒํผ ํด๋ฆญ ์ ์
๋ฐ์ดํธํ ์ ์ญ ๋ณ์
|
| 73 |
statement = ""
|
|
@@ -78,26 +88,26 @@ def update_statement(val):
|
|
| 78 |
return statement
|
| 79 |
|
| 80 |
with gr.Blocks() as demo:
|
| 81 |
-
# ์๋จ: ์ข์ฐ์ ๊ฐ๊ฐ
|
| 82 |
with gr.Row():
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
# "Load Random Conversations" ๋ฒํผ์ ๋๋ฌ ๋ฐ์ดํฐ์
์์ ๋ํ๋ฅผ ๋ถ๋ฌ์ค๊ณ
|
| 87 |
load_btn = gr.Button("Load Random Conversations")
|
| 88 |
-
load_btn.click(fn=load_and_format_conversations, inputs=[], outputs=[
|
| 89 |
|
| 90 |
-
# ํ๋จ: ํ๊ฐ ๋ฒํผ 4๊ฐ ๋ฐฐ์น
|
| 91 |
with gr.Row():
|
| 92 |
btn_both_good = gr.Button("Both good") # "๋ ๋ค ์ข์" โ "BG"
|
| 93 |
btn_a_better = gr.Button("A is better") # "A๊ฐ ๋ ์ข์" โ "AG"
|
| 94 |
btn_b_better = gr.Button("B is better") # "B๊ฐ ๋ ์ข์" โ "BG"
|
| 95 |
btn_both_bad = gr.Button("Both not good") # "๋ ๋ค ๋ณ๋ก์" โ "BB"
|
| 96 |
|
| 97 |
-
# ์ ํ
|
| 98 |
statement_output = gr.Textbox(label="Selected Statement", value="", interactive=False)
|
| 99 |
|
| 100 |
-
# ๊ฐ
|
| 101 |
btn_both_good.click(fn=lambda: update_statement("BG"), inputs=[], outputs=statement_output)
|
| 102 |
btn_a_better.click(fn=lambda: update_statement("AG"), inputs=[], outputs=statement_output)
|
| 103 |
btn_b_better.click(fn=lambda: update_statement("BG"), inputs=[], outputs=statement_output)
|
|
|
|
| 5 |
|
| 6 |
def load_random_conversations():
|
| 7 |
"""
|
| 8 |
+
๋ฐ์ดํฐ์
'gaeunseo/Taskmaster_sample_data'์ train split์์
|
| 9 |
+
conversation_id๋ณ๋ก ๊ทธ๋ฃนํํ ํ, ๋ชจ๋ ํ์ used ์ปฌ๋ผ์ด False์ธ ๊ทธ๋ฃน๋ง ํํฐ๋งํฉ๋๋ค.
|
| 10 |
+
์ด ์ค ๋๋คํ๊ฒ 2๊ฐ ๊ทธ๋ฃน์ ์ ํํ์ฌ ๊ฐ ๊ทธ๋ฃน์ utterance๋ฅผ ์ค๋ฐ๊ฟ์ผ๋ก ์ฐ๊ฒฐํ ๋ฌธ์์ด ๋ ๊ฐ๋ฅผ ๋ฐํํฉ๋๋ค.
|
| 11 |
"""
|
| 12 |
ds = load_dataset("gaeunseo/Taskmaster_sample_data")["train"]
|
| 13 |
|
| 14 |
+
# conversation_id๋ณ๋ก ๊ทธ๋ฃนํ
|
| 15 |
groups = {}
|
| 16 |
for row in ds:
|
| 17 |
cid = row["conversation_id"]
|
| 18 |
groups.setdefault(cid, []).append(row)
|
| 19 |
|
| 20 |
+
# ๋ชจ๋ ํ์ used๊ฐ False์ธ ๊ทธ๋ฃน๋ง ์ ํ
|
| 21 |
valid_groups = [grp for grp in groups.values() if all(not row["used"] for row in grp)]
|
| 22 |
|
| 23 |
if len(valid_groups) < 2:
|
|
|
|
| 26 |
# ์ ํจํ ๊ทธ๋ฃน ์ค ๋๋คํ๊ฒ 2๊ฐ ์ ํ
|
| 27 |
selected_groups = random.sample(valid_groups, 2)
|
| 28 |
|
| 29 |
+
# ๊ฐ ๊ทธ๋ฃน์ ๋ชจ๋ utterance๋ฅผ ์ค๋ฐ๊ฟ(\n)์ผ๋ก ์ด์ด๋ถ์
|
| 30 |
conv_A = "\n".join(row["utterance"] for row in selected_groups[0])
|
| 31 |
conv_B = "\n".join(row["utterance"] for row in selected_groups[1])
|
| 32 |
|
| 33 |
return conv_A, conv_B
|
| 34 |
|
| 35 |
+
def format_chat_html(conv_text):
|
| 36 |
"""
|
| 37 |
+
conv_text ๋ฌธ์์ด์ [turn]์ [BC] ํ ํฐ์ ๊ธฐ์ค์ผ๋ก ๋ถํ ํ ํ,
|
| 38 |
+
๊ฐ ๋ฐํ๋ฅผ HTML ๋งํ์ ํํ๋ก ๋ณํํฉ๋๋ค.
|
|
|
|
| 39 |
|
| 40 |
+
- ์ฒซ ๋ฒ์งธ ๋ฐํ(์ธ๊ฐ): ์ค๋ฅธ์ชฝ ์ ๋ ฌ, ๋งํ์ ์ค๋ฅธ์ชฝ์ ๐ง ์ด๋ชจํฐ์ฝ ํ์
|
| 41 |
+
- ๋ ๋ฒ์งธ ๋ฐํ(AI): ์ผ์ชฝ ์ ๋ ฌ, ๋งํ์ ์ผ์ชฝ์ ๐ค ์ด๋ชจํฐ์ฝ ํ์
|
| 42 |
"""
|
| 43 |
+
# [turn]์ [BC] ํ ํฐ์ ๊ตฌ๋ถ์๋ก ๋ฐํ ๋ถํ
|
| 44 |
utterances = re.split(r'\[turn\]|\[BC\]', conv_text)
|
|
|
|
| 45 |
utterances = [utt.strip() for utt in utterances if utt.strip()]
|
| 46 |
|
| 47 |
+
# HTML ์ปจํ
์ด๋ (์ฑํ
์ฐฝ์ฒ๋ผ ๋ณด์ด๋๋ก)
|
| 48 |
+
html = '<div style="display: flex; flex-direction: column; gap: 10px; background-color: #f0f0f0; padding: 10px;">'
|
| 49 |
+
|
| 50 |
+
for i, utterance in enumerate(utterances):
|
| 51 |
+
if i % 2 == 0:
|
| 52 |
+
# ์ฒซ ๋ฒ์งธ, ์ธ ๋ฒ์งธ ๋ฑ (์ธ๊ฐ ๋ฐํ): ์ค๋ฅธ์ชฝ ์ ๋ ฌ
|
| 53 |
+
bubble = f'''
|
| 54 |
+
<div style="align-self: flex-end; background-color: #dcf8c6; padding: 10px; border-radius: 10px; max-width: 70%; position: relative;">
|
| 55 |
+
<div style="text-align: right;">{utterance} <span>๐ง</span></div>
|
| 56 |
+
</div>
|
| 57 |
+
'''
|
| 58 |
+
else:
|
| 59 |
+
# ๋ ๋ฒ์งธ, ๋ค ๋ฒ์งธ ๋ฑ (AI ๋ฐํ): ์ผ์ชฝ ์ ๋ ฌ
|
| 60 |
+
bubble = f'''
|
| 61 |
+
<div style="align-self: flex-start; background-color: #fff; padding: 10px; border-radius: 10px; max-width: 70%; position: relative; border: 1px solid #ccc;">
|
| 62 |
+
<div style="text-align: left;"><span>๐ค</span> {utterance}</div>
|
| 63 |
+
</div>
|
| 64 |
+
'''
|
| 65 |
+
html += bubble
|
| 66 |
+
html += '</div>'
|
| 67 |
+
return html
|
| 68 |
|
| 69 |
def load_and_format_conversations():
|
| 70 |
"""
|
| 71 |
+
๋ฐ์ดํฐ์
์์ ๋๋คํ๊ฒ ๋ ๋ํ ๋ฌธ์์ด์ ๊ฐ์ ธ์จ ํ, ๊ฐ๊ฐ format_chat_html()์ ํตํด
|
| 72 |
+
HTML ํ์์ ์ฑํ
์ฐฝ์ฒ๋ผ ๋ณด์ด๊ฒ ๋ณํํ์ฌ ๋ฐํํฉ๋๋ค.
|
| 73 |
"""
|
| 74 |
conv_A, conv_B = load_random_conversations()
|
| 75 |
+
# ์๋ฌ ๋ฉ์์ง์ธ ๊ฒฝ์ฐ ๊ทธ๋๋ก HTML ํ๊ทธ๋ก ๊ฐ์ธ์ ๋ฐํ
|
| 76 |
if conv_A.startswith("Not enough"):
|
| 77 |
+
return f"<p>{conv_A}</p>", f"<p>{conv_B}</p>"
|
| 78 |
+
html_A = format_chat_html(conv_A)
|
| 79 |
+
html_B = format_chat_html(conv_B)
|
| 80 |
+
return html_A, html_B
|
| 81 |
|
| 82 |
# ํ๊ฐ ๋ฒํผ ํด๋ฆญ ์ ์
๋ฐ์ดํธํ ์ ์ญ ๋ณ์
|
| 83 |
statement = ""
|
|
|
|
| 88 |
return statement
|
| 89 |
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
+
# ์๋จ: ์ข์ฐ์ ๊ฐ๊ฐ ๋ํ ๋ด์ฉ์ HTML๋ก ํ์ (Conversation A์ Conversation B)
|
| 92 |
with gr.Row():
|
| 93 |
+
conv_A_html = gr.HTML(label="Conversation A")
|
| 94 |
+
conv_B_html = gr.HTML(label="Conversation B")
|
| 95 |
|
| 96 |
+
# "Load Random Conversations" ๋ฒํผ์ ๋๋ฌ ๋ฐ์ดํฐ์
์์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๊ณ HTML๋ก ๋ณํ
|
| 97 |
load_btn = gr.Button("Load Random Conversations")
|
| 98 |
+
load_btn.click(fn=load_and_format_conversations, inputs=[], outputs=[conv_A_html, conv_B_html])
|
| 99 |
|
| 100 |
+
# ํ๋จ: ํ๊ฐ ๋ฒํผ 4๊ฐ ๋ฐฐ์น
|
| 101 |
with gr.Row():
|
| 102 |
btn_both_good = gr.Button("Both good") # "๋ ๋ค ์ข์" โ "BG"
|
| 103 |
btn_a_better = gr.Button("A is better") # "A๊ฐ ๋ ์ข์" โ "AG"
|
| 104 |
btn_b_better = gr.Button("B is better") # "B๊ฐ ๋ ์ข์" โ "BG"
|
| 105 |
btn_both_bad = gr.Button("Both not good") # "๋ ๋ค ๋ณ๋ก์" โ "BB"
|
| 106 |
|
| 107 |
+
# ์ ํํ ํ๊ฐ๊ฐ์ ํ์ํ๋ ํ
์คํธ๋ฐ์ค
|
| 108 |
statement_output = gr.Textbox(label="Selected Statement", value="", interactive=False)
|
| 109 |
|
| 110 |
+
# ๊ฐ ๋ฒํผ ํด๋ฆญ ์ ์ ์ญ ๋ณ์ statement๋ฅผ ์
๋ฐ์ดํธ
|
| 111 |
btn_both_good.click(fn=lambda: update_statement("BG"), inputs=[], outputs=statement_output)
|
| 112 |
btn_a_better.click(fn=lambda: update_statement("AG"), inputs=[], outputs=statement_output)
|
| 113 |
btn_b_better.click(fn=lambda: update_statement("BG"), inputs=[], outputs=statement_output)
|