Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,103 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
from datasets import load_dataset
|
| 4 |
import random
|
| 5 |
-
|
| 6 |
-
# ๋ฐ์ดํฐ์
๋ถ๋ฌ์ค๊ธฐ ๋ฐ ์ ์ฒ๋ฆฌ
|
| 7 |
-
dataset = load_dataset("gaeunseo/Taskmaster_sample_data", split='train')
|
| 8 |
-
df = dataset.to_pandas()
|
| 9 |
-
|
| 10 |
-
# ์ ์ญ ๋ณ์ ์ ์ธ
|
| 11 |
-
statement = ""
|
| 12 |
-
|
| 13 |
-
def update_statement(value):
|
| 14 |
-
global statement
|
| 15 |
-
statement = value
|
| 16 |
-
return statement
|
| 17 |
|
| 18 |
def load_random_conversations():
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
|
| 25 |
-
if
|
| 26 |
-
return "
|
| 27 |
|
| 28 |
-
# ๋๋ค
|
| 29 |
-
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
with gr.Blocks() as demo:
|
| 42 |
-
#
|
| 43 |
with gr.Row():
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
with gr.Row():
|
| 49 |
btn_both_good = gr.Button("Both good") # "๋ ๋ค ์ข์" โ "BG"
|
| 50 |
-
btn_a_better
|
| 51 |
-
btn_b_better
|
| 52 |
-
btn_both_bad
|
| 53 |
|
| 54 |
-
# ์ ํ๋
|
| 55 |
statement_output = gr.Textbox(label="Selected Statement", value="", interactive=False)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
load_conversations_btn = gr.Button("Load Random Conversations")
|
| 59 |
-
|
| 60 |
-
# ๋ํ ๋ถ๋ฌ์ค๊ธฐ ๋ฒํผ ํด๋ฆญ ์ ๋ํ ์์ ์
๋ฐ์ดํธ
|
| 61 |
-
load_conversations_btn.click(fn=load_random_conversations, inputs=[], outputs=[conversation_A, conversation_B])
|
| 62 |
-
|
| 63 |
-
# ๊ฐ ๋ฒํผ ํด๋ฆญ ์ update_statement ํจ์๋ฅผ ํธ์ถํ์ฌ statement ๊ฐ์ ์
๋ฐ์ดํธํฉ๋๋ค.
|
| 64 |
btn_both_good.click(fn=lambda: update_statement("BG"), inputs=[], outputs=statement_output)
|
| 65 |
btn_a_better.click(fn=lambda: update_statement("AG"), inputs=[], outputs=statement_output)
|
| 66 |
btn_b_better.click(fn=lambda: update_statement("BG"), inputs=[], outputs=statement_output)
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
import random
|
| 4 |
+
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|
| 24 |
+
return "Not enough unused conversations", "Not enough unused conversations"
|
| 25 |
|
| 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 format_chat(conv_text):
|
| 36 |
+
"""
|
| 37 |
+
conv_text ๋ฌธ์์ด์ [turn]๊ณผ [BC] ํ ํฐ์ ๊ธฐ์ค์ผ๋ก ๋ถํ ํ ํ,
|
| 38 |
+
์ฒซ ๋ฒ์งธ ๋ฐํ๋ ์ฌ๋(์ฌ์ฉ์), ๋ ๋ฒ์งธ ๋ฐํ๋ AI์ ์๋ต์ผ๋ก ๊ฐ์ฃผํ์ฌ
|
| 39 |
+
gr.Chatbot ์ปดํฌ๋ํธ์์ ์ฌ์ฉํ๋ (user_message, ai_message) ํํ ๋ฆฌ์คํธ๋ก ๋ณํ.
|
| 40 |
|
| 41 |
+
- ์ฌ๋(์ฌ์ฉ์) ๋ฉ์์ง: ์ค๋ฅธ์ชฝ ๋งํ์ , ๋ฉ์์ง ๋์ "๐ง" ์ด๋ชจํฐ์ฝ ์ถ๊ฐ
|
| 42 |
+
- AI ๋ฉ์์ง: ์ผ์ชฝ ๋งํ์ , ๋ฉ์์ง ์์ "๐ค" ์ด๋ชจํฐ์ฝ ์ถ๊ฐ
|
| 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 |
+
chat = []
|
| 50 |
+
# ๋ฒ๊ฐ์ ๋ฑ์ฅํ๋ค๊ณ ๊ฐ์ (์ฒซ ๋ฒ์งธ: ์ฌ๋, ๋ ๋ฒ์งธ: AI, ...)
|
| 51 |
+
for i in range(0, len(utterances), 2):
|
| 52 |
+
# ์ฒซ ๋ฒ์งธ ๋ฐํ โ ์ฌ๋ (์ค๋ฅธ์ชฝ ์ ๋ ฌ: gr.Chatbot์์ ์ฌ์ฉ์ ๋ฉ์์ง๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ค๋ฅธ์ชฝ์ ํ์๋จ)
|
| 53 |
+
human = utterances[i] + " ๐ง"
|
| 54 |
+
ai = ""
|
| 55 |
+
if i + 1 < len(utterances):
|
| 56 |
+
# ๋ ๋ฒ์งธ ๋ฐํ โ AI (์ผ์ชฝ ์ ๋ ฌ: gr.Chatbot์์ ๋ด ๋ฉ์์ง๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ผ์ชฝ์ ํ์๋จ)
|
| 57 |
+
ai = "๐ค " + utterances[i + 1]
|
| 58 |
+
chat.append((human, ai))
|
| 59 |
+
return chat
|
| 60 |
+
|
| 61 |
+
def load_and_format_conversations():
|
| 62 |
+
"""
|
| 63 |
+
๋ฐ์ดํฐ์
์์ ๋๋คํ๊ฒ ๋ ๋ํ ๋ฌธ์์ด์ ๊ฐ์ ธ์จ ํ, ๊ฐ๊ฐ format_chat()์ ํตํด
|
| 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 |
+
return format_chat(conv_A), format_chat(conv_B)
|
| 71 |
+
|
| 72 |
+
# ํ๊ฐ ๋ฒํผ ํด๋ฆญ ์ ์
๋ฐ์ดํธํ ์ ์ญ ๋ณ์
|
| 73 |
+
statement = ""
|
| 74 |
+
|
| 75 |
+
def update_statement(val):
|
| 76 |
+
global statement
|
| 77 |
+
statement = val
|
| 78 |
+
return statement
|
| 79 |
|
| 80 |
with gr.Blocks() as demo:
|
| 81 |
+
# ์๋จ: ์ข์ฐ์ ๊ฐ๊ฐ ์ฑํ
์ธํฐํ์ด์ค ๋ฐฐ์น (Conversation A์ Conversation B)
|
| 82 |
with gr.Row():
|
| 83 |
+
chat_A = gr.Chatbot(label="Conversation A")
|
| 84 |
+
chat_B = gr.Chatbot(label="Conversation B")
|
| 85 |
+
|
| 86 |
+
# "Load Random Conversations" ๋ฒํผ์ ๋๋ฌ ๋ฐ์ดํฐ์
์์ ๋ํ๋ฅผ ๋ถ๋ฌ์ค๊ณ ์ฑํ
์ธํฐํ์ด์ค์ ํ์
|
| 87 |
+
load_btn = gr.Button("Load Random Conversations")
|
| 88 |
+
load_btn.click(fn=load_and_format_conversations, inputs=[], outputs=[chat_A, chat_B])
|
| 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 |
+
# ์ ํ๋ ํ๊ฐ๊ฐ(statement)์ ๋ณด์ฌ์ฃผ๋ ํ
์คํธ๋ฐ์ค
|
| 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)
|