Spaces:
Build error

gaeunseo commited on
Commit
a4decd8
ยท
verified ยท
1 Parent(s): e3a3cf7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -38
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๊ฐ€ 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,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 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 = ""
@@ -78,26 +88,26 @@ def update_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)
 
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)