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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -43
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
- # 'used'๊ฐ€ False์ธ ๊ทธ๋ฃน ํ•„ํ„ฐ๋ง
20
- unused_groups = df[df['used'] == False].groupby('conversation_id')
 
 
 
 
 
 
 
 
 
 
21
 
22
- # ์กฐ๊ฑด์— ๋งž๋Š” conversation_id ๋ฆฌ์ŠคํŠธ
23
- valid_conversation_ids = [cid for cid, group in unused_groups if len(group) >= 2]
24
 
25
- if not valid_conversation_ids:
26
- return "No available conversation", "No available conversation"
27
 
28
- # ๋žœ๋ค์œผ๋กœ ํ•˜๋‚˜์˜ conversation_id ์„ ํƒ
29
- selected_id = random.choice(valid_conversation_ids)
30
 
31
- # ์„ ํƒ๋œ ๊ทธ๋ฃน์—์„œ ๋‘ ๊ฐœ์˜ ํ–‰์„ ๋žœ๋ค ์„ ํƒ
32
- selected_group = df[df['conversation_id'] == selected_id]
33
- selected_conversations = selected_group.sample(2)
 
 
 
 
 
 
 
 
34
 
35
- # ์„ ํƒ๋œ ๋Œ€ํ™” ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
36
- conversation_A = selected_conversations.iloc[0]['text']
37
- conversation_B = selected_conversations.iloc[1]['text']
 
 
 
 
38
 
39
- return conversation_A, conversation_B
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  with gr.Blocks() as demo:
42
- # ๋‘ ๊ฐœ์˜ ๋Œ€ํ™”์ฐฝ์„ ์ขŒ์šฐ์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
43
  with gr.Row():
44
- conversation_A = gr.Textbox(label="Conversation A", placeholder="Enter conversation A here...", lines=10)
45
- conversation_B = gr.Textbox(label="Conversation B", placeholder="Enter conversation B here...", lines=10)
 
 
 
 
46
 
47
- # 4๊ฐœ์˜ ๋ฒ„ํŠผ์„ ํ•œ ํ–‰์— ๋ฐฐ์น˜ํ•ฉ๋‹ˆ๋‹ค.
48
  with gr.Row():
49
  btn_both_good = gr.Button("Both good") # "๋‘˜ ๋‹ค ์ข‹์Œ" โ†’ "BG"
50
- btn_a_better = gr.Button("A is better") # "A๊ฐ€ ๋” ์ข‹์Œ" โ†’ "AG"
51
- btn_b_better = gr.Button("B is better") # "B๊ฐ€ ๋” ์ข‹์Œ" โ†’ "BG"
52
- btn_both_bad = gr.Button("Both not good") # "๋‘˜ ๋‹ค ๋ณ„๋กœ์ž„" โ†’ "BB"
53
 
54
- # ์„ ํƒ๋œ statement ๊ฐ’์„ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ์ถœ๋ ฅ ํ…์ŠคํŠธ๋ฐ•์Šค (์˜ต์…˜)
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)