Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,8 +12,8 @@ data_lock = threading.Lock()
|
|
| 12 |
|
| 13 |
def initialize_global_data():
|
| 14 |
"""
|
| 15 |
-
DATA_FILE์ด ์กด์ฌํ์ง ์์ผ๋ฉด,
|
| 16 |
-
์ด๋ฏธ ํ์ผ์ด ์์ผ๋ฉด ํ์ผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด ๋ฐํํฉ๋๋ค.
|
| 17 |
"""
|
| 18 |
if not os.path.exists(DATA_FILE):
|
| 19 |
ds = load_dataset("gaeunseo/Taskmaster_sample_data", split="train")
|
|
@@ -33,49 +33,51 @@ def initialize_global_data():
|
|
| 33 |
return df
|
| 34 |
|
| 35 |
def load_global_data():
|
|
|
|
| 36 |
with data_lock:
|
| 37 |
df = pd.read_csv(DATA_FILE)
|
| 38 |
return df
|
| 39 |
|
| 40 |
def save_global_data(df):
|
|
|
|
| 41 |
with data_lock:
|
| 42 |
df.to_csv(DATA_FILE, index=False)
|
| 43 |
|
|
|
|
| 44 |
global_data = initialize_global_data()
|
| 45 |
|
| 46 |
def get_random_row_from_dataset():
|
| 47 |
"""
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
"""
|
| 55 |
global global_data
|
| 56 |
-
global_data = load_global_data()
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
for
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
valid_groups = [
|
| 64 |
-
group for group in conversation_groups.values()
|
| 65 |
-
if all(not r["used"] for r in group) and any(r["overlapping"] == "TT" for r in group)
|
| 66 |
-
]
|
| 67 |
if not valid_groups:
|
| 68 |
return None
|
| 69 |
-
chosen_group = random.choice(valid_groups)
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
if row["overlapping"] == "TT":
|
| 73 |
-
row["used"] = True # ์
๋ฐ์ดํธ
|
| 74 |
-
chosen_row = row
|
| 75 |
-
break
|
| 76 |
save_global_data(global_data)
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
|
|
|
|
|
|
| 79 |
row = get_random_row_from_dataset()
|
| 80 |
if row is None:
|
| 81 |
human_message = "No valid conversation available."
|
|
@@ -86,7 +88,7 @@ else:
|
|
| 86 |
ai_message = raw_text.split("[turn]")[1].strip()
|
| 87 |
|
| 88 |
#############################################
|
| 89 |
-
# ์ฑํ
์ธํฐํ์ด์ค ๊ด๋ จ ํจ์
|
| 90 |
#############################################
|
| 91 |
|
| 92 |
def get_initial_human_html():
|
|
@@ -104,7 +106,7 @@ def get_initial_human_html():
|
|
| 104 |
|
| 105 |
def stream_human_message():
|
| 106 |
"""
|
| 107 |
-
Start Typing ๋ฒํผ ํด๋ฆญ ์, ์ ์ญ ๋ณ์ human_message์ ๋ด์ฉ์ ํ ๊ธ์์ฉ ํ์ดํ ํจ๊ณผ๋ก
|
| 108 |
์ด์ ์ํ(โ๏ธ ์์ด์ฝ, ํ์ ์ฒ๋ฆฌ ๋ฑ)๋ ๋ฆฌ์
๋ฉ๋๋ค.
|
| 109 |
"""
|
| 110 |
bubble_content = ""
|
|
@@ -119,7 +121,7 @@ def stream_human_message():
|
|
| 119 |
# ์ด๊ธฐ ์ํ: ๋น ๋งํ์ ๊ณผ ์ด๋ชจํฐ์ฝ
|
| 120 |
yield wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 121 |
|
| 122 |
-
# ํ ๊ธ์์ฉ ์ถ๊ฐ (ํ์ดํ ํจ๊ณผ)
|
| 123 |
for i, ch in enumerate(human_message):
|
| 124 |
bubble_content += f"<span data-index='{i}'>{ch}</span>"
|
| 125 |
current_html = wrapper_start + bubble_start + bubble_content + bubble_end + emoji_html + wrapper_end
|
|
@@ -131,21 +133,21 @@ def submit_edit(edited_text):
|
|
| 131 |
Submit ๋ฒํผ ํด๋ฆญ ์ ํธ์ถ๋๋ ํจ์.
|
| 132 |
1. ํธ์ง๋ human ๋ฉ์์ง(โ๏ธ ์๋ถ๋ถ)๋ฅผ ์ ํ์ผ๋ก global_data์ ์ถ๊ฐํฉ๋๋ค.
|
| 133 |
2. get_random_row_from_dataset()์ ํตํด ์๋ก์ด ๋ํ๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์ ์ญ ๋ณ์ human_message์ ai_message๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค.
|
| 134 |
-
3. ์ด๊ธฐ ์ํ์ human
|
| 135 |
"""
|
| 136 |
-
global
|
| 137 |
-
|
| 138 |
new_row = {
|
| 139 |
-
"conversation_id": "edited_" + str(random.randint(1000,9999)),
|
| 140 |
"used": False,
|
| 141 |
"overlapping": "",
|
| 142 |
"text": edited_text,
|
| 143 |
"human_message": edited_text,
|
| 144 |
"ai_message": ""
|
| 145 |
}
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
save_global_data(
|
| 149 |
|
| 150 |
new_row_data = get_random_row_from_dataset()
|
| 151 |
if new_row_data is None:
|
|
@@ -170,7 +172,8 @@ def submit_edit(edited_text):
|
|
| 170 |
#############################################
|
| 171 |
|
| 172 |
with gr.Blocks() as demo:
|
| 173 |
-
# (A)
|
|
|
|
| 174 |
gr.HTML(
|
| 175 |
"""
|
| 176 |
<script>
|
|
@@ -275,7 +278,9 @@ with gr.Blocks() as demo:
|
|
| 275 |
gr.Markdown("## Chat Interface")
|
| 276 |
|
| 277 |
with gr.Column(elem_classes="chat-container"):
|
|
|
|
| 278 |
human_bubble = gr.HTML(get_initial_human_html())
|
|
|
|
| 279 |
ai_html = f"""
|
| 280 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
| 281 |
<div class="emoji">๐ค</div>
|
|
@@ -287,10 +292,12 @@ with gr.Blocks() as demo:
|
|
| 287 |
# ์จ๊น ํ
์คํธ๋ฐ์ค (ํธ์ง๋ ํ
์คํธ ์ ์ฅ์ฉ)
|
| 288 |
edited_text_input = gr.Textbox(visible=False, elem_id="edited_text_input")
|
| 289 |
|
|
|
|
| 290 |
with gr.Row():
|
| 291 |
start_button = gr.Button("Start Typing")
|
| 292 |
submit_button = gr.Button("Submit", elem_id="submit_btn")
|
| 293 |
|
|
|
|
| 294 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 295 |
submit_button.click(fn=submit_edit, inputs=edited_text_input, outputs=[human_bubble, ai_bubble])
|
| 296 |
|
|
|
|
| 12 |
|
| 13 |
def initialize_global_data():
|
| 14 |
"""
|
| 15 |
+
DATA_FILE์ด ์กด์ฌํ์ง ์์ผ๋ฉด, gaeunseo/Taskmaster_sample_data ๋ฐ์ดํฐ์
์ ๋ก๋ํ์ฌ DataFrame์ผ๋ก ๋ณํํ ํ CSV ํ์ผ๋ก ์ ์ฅํฉ๋๋ค.
|
| 16 |
+
์ด๋ฏธ ํ์ผ์ด ์์ผ๋ฉด ํ์ผ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด DataFrame์ ๋ฐํํฉ๋๋ค.
|
| 17 |
"""
|
| 18 |
if not os.path.exists(DATA_FILE):
|
| 19 |
ds = load_dataset("gaeunseo/Taskmaster_sample_data", split="train")
|
|
|
|
| 33 |
return df
|
| 34 |
|
| 35 |
def load_global_data():
|
| 36 |
+
"""CSV ํ์ผ์์ global_data DataFrame์ ์ฝ์ด์ต๋๋ค."""
|
| 37 |
with data_lock:
|
| 38 |
df = pd.read_csv(DATA_FILE)
|
| 39 |
return df
|
| 40 |
|
| 41 |
def save_global_data(df):
|
| 42 |
+
"""DataFrame์ CSV ํ์ผ์ ์ ์ฅํฉ๋๋ค."""
|
| 43 |
with data_lock:
|
| 44 |
df.to_csv(DATA_FILE, index=False)
|
| 45 |
|
| 46 |
+
# CSV ํ์ผ์ ์ ์ฅ๋ global_data ์ด๊ธฐํ
|
| 47 |
global_data = initialize_global_data()
|
| 48 |
|
| 49 |
def get_random_row_from_dataset():
|
| 50 |
"""
|
| 51 |
+
CSV ํ์ผ์ ์ ์ฅ๋ global_data์์,
|
| 52 |
+
1. conversation_id๋ณ๋ก ๊ทธ๋ฃนํํ๊ณ ,
|
| 53 |
+
2. ๊ฐ ๊ทธ๋ฃน์์ ๋ชจ๋ ํ์ used ์ปฌ๋ผ์ด False์ด๋ฉฐ, ๊ทธ๋ฃน ๋ด์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ์ด ์กด์ฌํ๋ ๊ทธ๋ฃน๋ง valid๋ก ๊ฐ์ฃผํฉ๋๋ค.
|
| 54 |
+
validํ ๊ทธ๋ฃน๋ค ์ค ๋๋คํ๊ฒ ํ๋์ ๊ทธ๋ฃน์ ์ ํํ ํ,
|
| 55 |
+
- ํด๋น ๊ทธ๋ฃน์ ๋ชจ๋ ํ์ used ๊ฐ์ True๋ก ์
๋ฐ์ดํธ(์ฆ, ์ ์ฒด ๊ทธ๋ฃน์ ํ ๋น)ํ๊ณ CSV ํ์ผ์ ์ ์ฅํฉ๋๋ค.
|
| 56 |
+
- ์ ํ๋ ๊ทธ๋ฃน ๋ด์์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ(์ฌ๋ฌ ๊ฐ๋ผ๋ฉด ์ฒซ ๋ฒ์งธ)์ ๋ฐํํฉ๋๋ค.
|
| 57 |
"""
|
| 58 |
global global_data
|
| 59 |
+
global_data = load_global_data() # ์ต์ ๋ฐ์ดํฐ ๋ก๋
|
| 60 |
+
groups = global_data.groupby('conversation_id')
|
| 61 |
+
valid_groups = []
|
| 62 |
+
for cid, group in groups:
|
| 63 |
+
# ๋ชจ๋ ํ์ used ๊ฐ์ด False์ด๊ณ , ๊ทธ๋ฃน ๋ด์ overlapping ๊ฐ์ด "TT"์ธ ํ์ด ์๋ ๊ทธ๋ฃน ํํฐ๋ง
|
| 64 |
+
if group['used'].apply(lambda x: bool(x) == False).all() and (group['overlapping'] == "TT").any():
|
| 65 |
+
valid_groups.append((cid, group))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if not valid_groups:
|
| 67 |
return None
|
| 68 |
+
chosen_cid, chosen_group = random.choice(valid_groups)
|
| 69 |
+
# ์ ์ฒด ๊ทธ๋ฃน์ used ๊ฐ์ True๋ก ์
๋ฐ์ดํธ
|
| 70 |
+
global_data.loc[global_data['conversation_id'] == chosen_cid, 'used'] = True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
save_global_data(global_data)
|
| 72 |
+
# ์ ํ๋ ๊ทธ๋ฃน ๋ด์์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ์ ๋ฐํ (์ฌ๋ฌ ๊ฐ๋ผ๋ฉด ์ฒซ ๋ฒ์งธ)
|
| 73 |
+
chosen_rows = chosen_group[chosen_group['overlapping'] == "TT"]
|
| 74 |
+
if chosen_rows.empty:
|
| 75 |
+
return None
|
| 76 |
+
chosen_row = chosen_rows.iloc[0]
|
| 77 |
+
return chosen_row.to_dict()
|
| 78 |
|
| 79 |
+
# --- ์ด๊ธฐ ๋ํ ๋ถ๋ฌ์ค๊ธฐ ---
|
| 80 |
+
# ๋ฐ์ดํฐ์
์ text ์ปฌ๋ผ์ "[turn]"์ ๊ธฐ์ค์ผ๋ก ๋ํ๊ฐ ๊ตฌ๋ถ๋์ด ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
|
| 81 |
row = get_random_row_from_dataset()
|
| 82 |
if row is None:
|
| 83 |
human_message = "No valid conversation available."
|
|
|
|
| 88 |
ai_message = raw_text.split("[turn]")[1].strip()
|
| 89 |
|
| 90 |
#############################################
|
| 91 |
+
# ์ฑํ
์ธํฐํ์ด์ค ๊ด๋ จ ํจ์ (๋งํ์ , ํ์ดํ ํจ๊ณผ, ํธ์ง ๊ธฐ๋ฅ)
|
| 92 |
#############################################
|
| 93 |
|
| 94 |
def get_initial_human_html():
|
|
|
|
| 106 |
|
| 107 |
def stream_human_message():
|
| 108 |
"""
|
| 109 |
+
Start Typing ๋ฒํผ ํด๋ฆญ ์, ์ ์ญ ๋ณ์ human_message์ ๋ด์ฉ์ ํ ๊ธ์์ฉ ํ์ดํ ํจ๊ณผ๋ก ์ถ๋ ฅํฉ๋๋ค.
|
| 110 |
์ด์ ์ํ(โ๏ธ ์์ด์ฝ, ํ์ ์ฒ๋ฆฌ ๋ฑ)๋ ๋ฆฌ์
๋ฉ๋๋ค.
|
| 111 |
"""
|
| 112 |
bubble_content = ""
|
|
|
|
| 121 |
# ์ด๊ธฐ ์ํ: ๋น ๋งํ์ ๊ณผ ์ด๋ชจํฐ์ฝ
|
| 122 |
yield wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 123 |
|
| 124 |
+
# human_message๋ฅผ ํ ๊ธ์์ฉ ์ถ๊ฐ (ํ์ดํ ํจ๊ณผ)
|
| 125 |
for i, ch in enumerate(human_message):
|
| 126 |
bubble_content += f"<span data-index='{i}'>{ch}</span>"
|
| 127 |
current_html = wrapper_start + bubble_start + bubble_content + bubble_end + emoji_html + wrapper_end
|
|
|
|
| 133 |
Submit ๋ฒํผ ํด๋ฆญ ์ ํธ์ถ๋๋ ํจ์.
|
| 134 |
1. ํธ์ง๋ human ๋ฉ์์ง(โ๏ธ ์๋ถ๋ถ)๋ฅผ ์ ํ์ผ๋ก global_data์ ์ถ๊ฐํฉ๋๋ค.
|
| 135 |
2. get_random_row_from_dataset()์ ํตํด ์๋ก์ด ๋ํ๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์ ์ญ ๋ณ์ human_message์ ai_message๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค.
|
| 136 |
+
3. ์ด๊ธฐ ์ํ์ human ๋งํ์ ๊ณผ ai ๋งํ์ HTML์ ๋ฐํํ์ฌ ์ธํฐํ์ด์ค๋ฅผ ๋ฆฌ์
ํฉ๋๋ค.
|
| 137 |
"""
|
| 138 |
+
global human_message, ai_message
|
| 139 |
+
data = load_global_data()
|
| 140 |
new_row = {
|
| 141 |
+
"conversation_id": "edited_" + str(random.randint(1000, 9999)),
|
| 142 |
"used": False,
|
| 143 |
"overlapping": "",
|
| 144 |
"text": edited_text,
|
| 145 |
"human_message": edited_text,
|
| 146 |
"ai_message": ""
|
| 147 |
}
|
| 148 |
+
new_df = pd.DataFrame([new_row])
|
| 149 |
+
data = pd.concat([data, new_df], ignore_index=True)
|
| 150 |
+
save_global_data(data)
|
| 151 |
|
| 152 |
new_row_data = get_random_row_from_dataset()
|
| 153 |
if new_row_data is None:
|
|
|
|
| 172 |
#############################################
|
| 173 |
|
| 174 |
with gr.Blocks() as demo:
|
| 175 |
+
# (A) ํ์ด์ง ์๋จ ์คํฌ๋ฆฝํธ: Human ๋งํ์ ๋ด์ ๊ฐ <span data-index="...">๋ฅผ ํด๋ฆญํ๋ฉด,
|
| 176 |
+
# ํด๋น ์์น์ โ๏ธ ์์ด์ฝ์ด ์ฝ์
๋๊ณ , ๊ทธ ์ดํ ํ
์คํธ๊ฐ ํ์์ผ๋ก ๋ณ๊ฒฝ๋ฉ๋๋ค.
|
| 177 |
gr.HTML(
|
| 178 |
"""
|
| 179 |
<script>
|
|
|
|
| 278 |
gr.Markdown("## Chat Interface")
|
| 279 |
|
| 280 |
with gr.Column(elem_classes="chat-container"):
|
| 281 |
+
# Human ๋งํ์ (์ด๊ธฐ: ๋น ๋ฉ์์ง + ๐ง ์ด๋ชจํฐ์ฝ)
|
| 282 |
human_bubble = gr.HTML(get_initial_human_html())
|
| 283 |
+
# AI ๋งํ์ (์ผ์ชฝ: ๐ค ์ด๋ชจํฐ์ฝ + ๋ฉ์์ง)
|
| 284 |
ai_html = f"""
|
| 285 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
| 286 |
<div class="emoji">๐ค</div>
|
|
|
|
| 292 |
# ์จ๊น ํ
์คํธ๋ฐ์ค (ํธ์ง๋ ํ
์คํธ ์ ์ฅ์ฉ)
|
| 293 |
edited_text_input = gr.Textbox(visible=False, elem_id="edited_text_input")
|
| 294 |
|
| 295 |
+
# ๋ฒํผ ์์ญ: Start Typing๊ณผ Submit ๋ฒํผ์ ๊ฐ์ ํ์ ๋ฐฐ์น
|
| 296 |
with gr.Row():
|
| 297 |
start_button = gr.Button("Start Typing")
|
| 298 |
submit_button = gr.Button("Submit", elem_id="submit_btn")
|
| 299 |
|
| 300 |
+
# ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
| 301 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 302 |
submit_button.click(fn=submit_edit, inputs=edited_text_input, outputs=[human_bubble, ai_bubble])
|
| 303 |
|