gaeunseo commited on
Commit
43769dc
·
verified ·
1 Parent(s): f4a9a7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -46
app.py CHANGED
@@ -1,68 +1,66 @@
1
  import gradio as gr
 
2
  from datasets import load_dataset
3
  import random
4
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  def load_random_conversations():
6
- """
7
- 데이터셋 'gaeunseo/Taskmaster_sample_data' train split에서
8
- conversation_id별로 그룹화한 후, 각 그룹의 모든 행의 used가 False인
9
- 그룹들 중에서 랜덤하게 2개 그룹을 선택하여, 각 그룹의 utterance들을
10
- newline으로 join한 문자열을 반환합니다.
11
- """
12
- # 데이터셋 로드 (split 이름은 상황에 맞게 수정)
13
- ds = load_dataset("gaeunseo/Taskmaster_sample_data")["train"]
14
 
15
- # conversation_id별로 그룹화 (그룹은 리스트로 저장)
16
- groups = {}
17
- for row in ds:
18
- cid = row["conversation_id"]
19
- groups.setdefault(cid, []).append(row)
20
 
21
- # 모든 행의 used 컬럼이 False인 그룹만 필터링
22
- valid_groups = [grp for grp in groups.values() if all(not row["used"] for row in grp)]
23
 
24
- if len(valid_groups) < 2:
25
- # 조건을 만족하는 그룹이 2개 미만이면 에러 메시지 반환
26
- return "Not enough unused conversations", "Not enough unused conversations"
27
 
28
- # 유효한 그룹 중에서 랜덤하게 2개 그룹 선택
29
- selected_groups = random.sample(valid_groups, 2)
 
30
 
31
- # 그룹의 모든 utterance를 이어붙여 하나의 대화 문자열로 생성
32
- conv_A = selected_groups[0]['text']
33
- conv_B = selected_groups[1]['text']
34
 
35
- return conv_A, conv_B
36
-
37
- # 평가 버튼을 눌렀을 때 업데이트할 전역 변수
38
- statement = ""
39
-
40
- def update_statement(val):
41
- global statement
42
- statement = val
43
- return statement
44
 
45
  with gr.Blocks() as demo:
46
- # 상단: 두 개의 대화창 (Conversation A, Conversation B)
47
  with gr.Row():
48
- conversation_A = gr.Textbox(label="Conversation A", lines=10, placeholder="Conversation A will be loaded here...")
49
- conversation_B = gr.Textbox(label="Conversation B", lines=10, placeholder="Conversation B will be loaded here...")
50
 
51
- # 'Load Random Conversations' 버튼을 눌러 데이터셋에서 대화들을 불러옵니다.
52
- load_btn = gr.Button("Load Random Conversations")
53
- load_btn.click(fn=load_random_conversations, inputs=[], outputs=[conversation_A, conversation_B])
54
-
55
- # 하단: 평가 버튼 4개 배치
56
  with gr.Row():
57
- btn_both_good = gr.Button("Both good") # “둘좋음” → "BG"
58
- btn_a_better = gr.Button("A is better") # A가 더 좋음” → "AG"
59
- btn_b_better = gr.Button("B is better") # B가 더 좋음” → "BG"
60
- btn_both_bad = gr.Button("Both not good") # “둘별로임” → "BB"
61
 
62
- # 선택된 statement 보여주는 텍스트박스 (업데이트 전역변수)
63
  statement_output = gr.Textbox(label="Selected Statement", value="", interactive=False)
64
 
65
- # 버튼 클릭 시 해당 값을 전역변수에 저장하여 출력
 
 
 
 
 
 
66
  btn_both_good.click(fn=lambda: update_statement("BG"), inputs=[], outputs=statement_output)
67
  btn_a_better.click(fn=lambda: update_statement("AG"), inputs=[], outputs=statement_output)
68
  btn_b_better.click(fn=lambda: update_statement("BG"), inputs=[], outputs=statement_output)
 
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)