Spaces:
Sleeping
Sleeping
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 |
-
- 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 |
-
|
| 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)
|