Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,130 +3,74 @@ import gradio as gr
|
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
import threading
|
|
|
|
| 6 |
from datasets import load_dataset
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
overlapping = Column(String)
|
| 29 |
-
text = Column(Text)
|
| 30 |
-
human_message = Column(Text)
|
| 31 |
-
ai_message = Column(Text)
|
| 32 |
-
|
| 33 |
-
# ํ
์ด๋ธ ์์ฑ
|
| 34 |
-
Base.metadata.create_all(bind=engine)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def init_db():
|
| 44 |
-
"""
|
| 45 |
-
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋ฐ์ดํฐ๊ฐ ์์ผ๋ฉด, Hugging Face ๋ฐ์ดํฐ์
์ ๋ก๋ํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฝ์
ํฉ๋๋ค.
|
| 46 |
-
"""
|
| 47 |
-
session = SessionLocal()
|
| 48 |
-
try:
|
| 49 |
-
count = session.query(Conversation).count()
|
| 50 |
-
if count == 0:
|
| 51 |
-
ds = load_dataset("gaeunseo/Taskmaster_sample_data", split="train")
|
| 52 |
-
data = list(ds)
|
| 53 |
-
for row in data:
|
| 54 |
-
# ๊ธฐ๋ณธ๊ฐ ์ฑ์ฐ๊ธฐ: used๋ False, overlapping์ row์ ์๋ค๋ฉด ๋น ๋ฌธ์์ด๋ก ์ฒ๋ฆฌ
|
| 55 |
-
text = row.get("text", "")
|
| 56 |
-
conv = Conversation(
|
| 57 |
-
conversation_id = row.get("conversation_id", ""),
|
| 58 |
-
used = row.get("used", False),
|
| 59 |
-
overlapping = row.get("overlapping", ""),
|
| 60 |
-
text = text,
|
| 61 |
-
human_message = text.split("[turn]")[0].strip() if "[turn]" in text else text,
|
| 62 |
-
ai_message = text.split("[turn]")[1].strip() if "[turn]" in text and len(text.split("[turn]")) > 1 else ""
|
| 63 |
-
)
|
| 64 |
-
session.add(conv)
|
| 65 |
-
session.commit()
|
| 66 |
-
finally:
|
| 67 |
-
session.close()
|
| 68 |
|
| 69 |
-
|
| 70 |
-
init_db()
|
| 71 |
-
|
| 72 |
-
#############################################
|
| 73 |
-
# 3. ๋ํ ๊ทธ๋ฃน์ ํ ๋นํ๋ ํจ์ (๋์์ฑ ๋ฌธ์ ๋ฅผ ๋ฐฉ์ง)
|
| 74 |
-
#############################################
|
| 75 |
|
| 76 |
def get_random_row_from_dataset():
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
if all(not conv.used for conv in group) and any(conv.overlapping == "TT" for conv in group):
|
| 95 |
-
valid_groups.append(group)
|
| 96 |
-
if not valid_groups:
|
| 97 |
-
return None
|
| 98 |
-
chosen_group = random.choice(valid_groups)
|
| 99 |
-
for conv in chosen_group:
|
| 100 |
-
conv.used = True
|
| 101 |
-
session.commit()
|
| 102 |
-
for conv in chosen_group:
|
| 103 |
-
if conv.overlapping == "TT":
|
| 104 |
-
return conv
|
| 105 |
-
return None
|
| 106 |
-
finally:
|
| 107 |
-
session.close()
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
#############################################
|
| 112 |
-
|
| 113 |
-
initial_conv = get_random_row_from_dataset()
|
| 114 |
-
if initial_conv is None:
|
| 115 |
human_message = "No valid conversation available."
|
| 116 |
ai_message = "No valid conversation available."
|
| 117 |
else:
|
| 118 |
-
raw_text =
|
| 119 |
-
human_message = raw_text.split("[turn]")[0].strip()
|
| 120 |
-
ai_message = raw_text.split("[turn]")[1].strip()
|
| 121 |
|
| 122 |
#############################################
|
| 123 |
-
#
|
| 124 |
#############################################
|
| 125 |
|
| 126 |
def get_initial_human_html():
|
| 127 |
-
"""
|
| 128 |
-
ํ์ด์ง ๋ก๋ ์, ๋น Human ๋งํ์ ๊ณผ ์ค๋ฅธ์ชฝ ๐ง ์ด๋ชจํฐ์ฝ์ ํฌํจํ ์ด๊ธฐ HTML ๋ฐํ
|
| 129 |
-
"""
|
| 130 |
wrapper_start = (
|
| 131 |
"""<div class="human-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-end; gap: 5px; width: 100%;">"""
|
| 132 |
)
|
|
@@ -137,10 +81,6 @@ def get_initial_human_html():
|
|
| 137 |
return wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 138 |
|
| 139 |
def stream_human_message():
|
| 140 |
-
"""
|
| 141 |
-
Start Typing ๋ฒํผ ํด๋ฆญ ์, ์ ์ญ ๋ณ์ human_message์ ๋ด์ฉ์ ํ ๊ธ์์ฉ ํ์ดํ ํจ๊ณผ๋ก ์ถ๋ ฅํฉ๋๋ค.
|
| 142 |
-
์ด์ ์ํ(โ๏ธ ์์ด์ฝ, ํ์ ์ฒ๋ฆฌ ๋ฑ)๋ ๋ฆฌ์
๋ฉ๋๋ค.
|
| 143 |
-
"""
|
| 144 |
bubble_content = ""
|
| 145 |
wrapper_start = (
|
| 146 |
"""<div class="human-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-end; gap: 5px; width: 100%;">"""
|
|
@@ -149,44 +89,41 @@ def stream_human_message():
|
|
| 149 |
bubble_end = "</div>"
|
| 150 |
emoji_html = "<div class='emoji'>๐ง</div>"
|
| 151 |
wrapper_end = "</div>"
|
|
|
|
|
|
|
| 152 |
yield wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
|
|
|
|
|
|
| 153 |
for i, ch in enumerate(human_message):
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
|
| 159 |
def submit_edit(edited_text):
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
ai_message = "No valid conversation available."
|
| 184 |
-
else:
|
| 185 |
-
raw_text = new_conv_data.text
|
| 186 |
-
human_message = raw_text.split("[turn]")[0].strip() if "[turn]" in raw_text else raw_text
|
| 187 |
-
ai_message = raw_text.split("[turn]")[1].strip() if "[turn]" in raw_text and len(raw_text.split("[turn]")) > 1 else ""
|
| 188 |
-
finally:
|
| 189 |
-
session.close()
|
| 190 |
new_human_html = get_initial_human_html()
|
| 191 |
new_ai_html = f"""
|
| 192 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
|
@@ -197,27 +134,31 @@ def submit_edit(edited_text):
|
|
| 197 |
return new_human_html, new_ai_html
|
| 198 |
|
| 199 |
#############################################
|
| 200 |
-
#
|
| 201 |
#############################################
|
| 202 |
|
| 203 |
with gr.Blocks() as demo:
|
| 204 |
-
# (A)
|
| 205 |
-
# ํด๋น ์์น์ โ๏ธ ์์ด์ฝ์ด ์ฝ์
๋๊ณ , ๊ทธ ์ดํ ํ
์คํธ๊ฐ ํ์์ผ๋ก ๋ณ๊ฒฝ๋ฉ๋๋ค.
|
| 206 |
gr.HTML(
|
| 207 |
"""
|
| 208 |
<script>
|
| 209 |
document.addEventListener("click", function(event) {
|
|
|
|
| 210 |
if (event.target && event.target.matches("div.speech-bubble.human span[data-index]")) {
|
| 211 |
var span = event.target;
|
| 212 |
var container = span.closest("div.speech-bubble.human");
|
|
|
|
| 213 |
var oldScissors = container.querySelectorAll("span.scissor");
|
| 214 |
oldScissors.forEach(function(s) { s.remove(); });
|
|
|
|
| 215 |
var spans = container.querySelectorAll("span[data-index]");
|
| 216 |
spans.forEach(function(s) { s.style.color = ''; });
|
|
|
|
| 217 |
var scissor = document.createElement('span');
|
| 218 |
scissor.textContent = 'โ๏ธ';
|
| 219 |
scissor.classList.add("scissor");
|
| 220 |
container.insertBefore(scissor, span.nextSibling);
|
|
|
|
| 221 |
var cutIndex = parseInt(span.getAttribute("data-index"));
|
| 222 |
spans.forEach(function(s) {
|
| 223 |
var idx = parseInt(s.getAttribute("data-index"));
|
|
@@ -225,30 +166,21 @@ with gr.Blocks() as demo:
|
|
| 225 |
s.style.color = "grey";
|
| 226 |
}
|
| 227 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
}
|
| 229 |
});
|
| 230 |
</script>
|
| 231 |
"""
|
| 232 |
)
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
document.addEventListener("DOMContentLoaded", function() {
|
| 238 |
-
var submitBtn = document.getElementById("submit_btn");
|
| 239 |
-
if(submitBtn){
|
| 240 |
-
submitBtn.addEventListener("click", function(){
|
| 241 |
-
var humanDiv = document.getElementById("human_message");
|
| 242 |
-
if(humanDiv){
|
| 243 |
-
var edited_text = humanDiv.innerText.split("โ๏ธ")[0];
|
| 244 |
-
document.getElementById("edited_text_input").value = edited_text;
|
| 245 |
-
}
|
| 246 |
-
});
|
| 247 |
-
}
|
| 248 |
-
});
|
| 249 |
-
</script>
|
| 250 |
-
"""
|
| 251 |
-
)
|
| 252 |
# (C) CSS ์คํ์ผ
|
| 253 |
gr.HTML(
|
| 254 |
"""
|
|
@@ -305,9 +237,7 @@ with gr.Blocks() as demo:
|
|
| 305 |
gr.Markdown("## Chat Interface")
|
| 306 |
|
| 307 |
with gr.Column(elem_classes="chat-container"):
|
| 308 |
-
# Human ๋งํ์ (์ด๊ธฐ: ๋น ๋ฉ์์ง + ๐ง ์ด๋ชจํฐ์ฝ)
|
| 309 |
human_bubble = gr.HTML(get_initial_human_html())
|
| 310 |
-
# AI ๋งํ์ (์ผ์ชฝ: ๐ค ์ด๋ชจํฐ์ฝ + ๋ฉ์์ง)
|
| 311 |
ai_html = f"""
|
| 312 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
| 313 |
<div class="emoji">๐ค</div>
|
|
@@ -319,12 +249,10 @@ with gr.Blocks() as demo:
|
|
| 319 |
# ์จ๊น ํ
์คํธ๋ฐ์ค (ํธ์ง๋ ํ
์คํธ ์ ์ฅ์ฉ)
|
| 320 |
edited_text_input = gr.Textbox(visible=False, elem_id="edited_text_input")
|
| 321 |
|
| 322 |
-
# ๋ฒํผ ์์ญ: Start Typing๊ณผ Submit ๋ฒํผ์ ๊ฐ์ ํ์ ๋ฐฐ์น
|
| 323 |
with gr.Row():
|
| 324 |
start_button = gr.Button("Start Typing")
|
| 325 |
submit_button = gr.Button("Submit", elem_id="submit_btn")
|
| 326 |
|
| 327 |
-
# ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
| 328 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 329 |
submit_button.click(fn=submit_edit, inputs=edited_text_input, outputs=[human_bubble, ai_bubble])
|
| 330 |
|
|
|
|
| 3 |
import random
|
| 4 |
import os
|
| 5 |
import threading
|
| 6 |
+
import pandas as pd
|
| 7 |
from datasets import load_dataset
|
| 8 |
|
| 9 |
+
# CSV ํ์ผ ๊ฒฝ๋ก์ ๋์ ์ ๊ทผ์ ์ํ Lock ์ ์ธ
|
| 10 |
+
DATA_FILE = "global_data.csv"
|
| 11 |
+
data_lock = threading.Lock()
|
| 12 |
|
| 13 |
+
def initialize_global_data():
|
| 14 |
+
if not os.path.exists(DATA_FILE):
|
| 15 |
+
ds = load_dataset("gaeunseo/Taskmaster_sample_data", split="train")
|
| 16 |
+
data = ds.to_pandas()
|
| 17 |
+
if "used" not in data.columns:
|
| 18 |
+
data["used"] = False
|
| 19 |
+
if "overlapping" not in data.columns:
|
| 20 |
+
data["overlapping"] = ""
|
| 21 |
+
if "text" not in data.columns:
|
| 22 |
+
data["text"] = ""
|
| 23 |
+
data.to_csv(DATA_FILE, index=False)
|
| 24 |
+
return data
|
| 25 |
+
else:
|
| 26 |
+
with data_lock:
|
| 27 |
+
df = pd.read_csv(DATA_FILE)
|
| 28 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
def load_global_data():
|
| 31 |
+
with data_lock:
|
| 32 |
+
df = pd.read_csv(DATA_FILE)
|
| 33 |
+
return df
|
| 34 |
|
| 35 |
+
def save_global_data(df):
|
| 36 |
+
with data_lock:
|
| 37 |
+
df.to_csv(DATA_FILE, index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
global_data = initialize_global_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
def get_random_row_from_dataset():
|
| 42 |
+
global global_data
|
| 43 |
+
global_data = load_global_data() # ์ต์ ๋ฐ์ดํฐ ๋ก๋
|
| 44 |
+
groups = global_data.groupby('conversation_id')
|
| 45 |
+
valid_groups = []
|
| 46 |
+
for cid, group in groups:
|
| 47 |
+
if group['used'].apply(lambda x: bool(x) == False).all() and (group['overlapping'] == "TT").any():
|
| 48 |
+
valid_groups.append((cid, group))
|
| 49 |
+
if not valid_groups:
|
| 50 |
+
return None
|
| 51 |
+
chosen_cid, chosen_group = random.choice(valid_groups)
|
| 52 |
+
global_data.loc[global_data['conversation_id'] == chosen_cid, 'used'] = True
|
| 53 |
+
save_global_data(global_data)
|
| 54 |
+
chosen_rows = chosen_group[chosen_group['overlapping'] == "TT"]
|
| 55 |
+
if chosen_rows.empty:
|
| 56 |
+
return None
|
| 57 |
+
chosen_row = chosen_rows.iloc[0]
|
| 58 |
+
return chosen_row.to_dict()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
row = get_random_row_from_dataset()
|
| 61 |
+
if row is None:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
human_message = "No valid conversation available."
|
| 63 |
ai_message = "No valid conversation available."
|
| 64 |
else:
|
| 65 |
+
raw_text = row['text']
|
| 66 |
+
human_message = raw_text.split("[turn]")[0].strip()
|
| 67 |
+
ai_message = raw_text.split("[turn]")[1].strip()
|
| 68 |
|
| 69 |
#############################################
|
| 70 |
+
# ์ฑํ
์ธํฐํ์ด์ค ๊ด๋ จ ํจ์
|
| 71 |
#############################################
|
| 72 |
|
| 73 |
def get_initial_human_html():
|
|
|
|
|
|
|
|
|
|
| 74 |
wrapper_start = (
|
| 75 |
"""<div class="human-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-end; gap: 5px; width: 100%;">"""
|
| 76 |
)
|
|
|
|
| 81 |
return wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 82 |
|
| 83 |
def stream_human_message():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
bubble_content = ""
|
| 85 |
wrapper_start = (
|
| 86 |
"""<div class="human-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-end; gap: 5px; width: 100%;">"""
|
|
|
|
| 89 |
bubble_end = "</div>"
|
| 90 |
emoji_html = "<div class='emoji'>๐ง</div>"
|
| 91 |
wrapper_end = "</div>"
|
| 92 |
+
|
| 93 |
+
# ์ด๊ธฐ ์ํ
|
| 94 |
yield wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 95 |
+
|
| 96 |
+
# ํ ๊ธ์์ฉ ํ์ดํ ํจ๊ณผ ์ ์ฉ
|
| 97 |
for i, ch in enumerate(human_message):
|
| 98 |
+
bubble_content += f"<span data-index='{i}'>{ch}</span>"
|
| 99 |
+
current_html = wrapper_start + bubble_start + bubble_content + bubble_end + emoji_html + wrapper_end
|
| 100 |
+
yield current_html
|
| 101 |
+
time.sleep(0.05)
|
| 102 |
|
| 103 |
def submit_edit(edited_text):
|
| 104 |
+
global human_message, ai_message
|
| 105 |
+
data = load_global_data()
|
| 106 |
+
new_row = {
|
| 107 |
+
"conversation_id": "edited_" + str(random.randint(1000, 9999)),
|
| 108 |
+
"used": False,
|
| 109 |
+
"overlapping": "",
|
| 110 |
+
"text": edited_text,
|
| 111 |
+
"human_message": edited_text,
|
| 112 |
+
"ai_message": ""
|
| 113 |
+
}
|
| 114 |
+
new_df = pd.DataFrame([new_row])
|
| 115 |
+
data = pd.concat([data, new_df], ignore_index=True)
|
| 116 |
+
save_global_data(data)
|
| 117 |
+
|
| 118 |
+
new_row_data = get_random_row_from_dataset()
|
| 119 |
+
if new_row_data is None:
|
| 120 |
+
human_message = "No valid conversation available."
|
| 121 |
+
ai_message = "No valid conversation available."
|
| 122 |
+
else:
|
| 123 |
+
raw_text = new_row_data['text']
|
| 124 |
+
human_message = raw_text.split("[turn]")[0].strip()
|
| 125 |
+
ai_message = raw_text.split("[turn]")[1].strip()
|
| 126 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
new_human_html = get_initial_human_html()
|
| 128 |
new_ai_html = f"""
|
| 129 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
|
|
|
| 134 |
return new_human_html, new_ai_html
|
| 135 |
|
| 136 |
#############################################
|
| 137 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 138 |
#############################################
|
| 139 |
|
| 140 |
with gr.Blocks() as demo:
|
| 141 |
+
# (A) ํ
์คํธ ํด๋ฆญ ์ โ๏ธ ์์ด์ฝ ์ถ๊ฐ ๋ฐ ํ์ ์ฒ๋ฆฌ, ๊ทธ๋ฆฌ๊ณ ๋ฐ๋ก hidden input ์
๋ฐ์ดํธ
|
|
|
|
| 142 |
gr.HTML(
|
| 143 |
"""
|
| 144 |
<script>
|
| 145 |
document.addEventListener("click", function(event) {
|
| 146 |
+
// human ๋ฉ์์ง์ span์ ํด๋ฆญํ๋ฉด ์ฒ๋ฆฌ
|
| 147 |
if (event.target && event.target.matches("div.speech-bubble.human span[data-index]")) {
|
| 148 |
var span = event.target;
|
| 149 |
var container = span.closest("div.speech-bubble.human");
|
| 150 |
+
// ์ด์ ์ ์ถ๊ฐ๋ โ๏ธ ์์ด์ฝ ์ ๊ฑฐ
|
| 151 |
var oldScissors = container.querySelectorAll("span.scissor");
|
| 152 |
oldScissors.forEach(function(s) { s.remove(); });
|
| 153 |
+
// ๋ชจ๋ span์ ์์ ์ด๊ธฐํ
|
| 154 |
var spans = container.querySelectorAll("span[data-index]");
|
| 155 |
spans.forEach(function(s) { s.style.color = ''; });
|
| 156 |
+
// โ๏ธ ์์ด์ฝ ์์ฑ ํ span ๋ฐ๋ก ๋ค์ ์ฝ์
|
| 157 |
var scissor = document.createElement('span');
|
| 158 |
scissor.textContent = 'โ๏ธ';
|
| 159 |
scissor.classList.add("scissor");
|
| 160 |
container.insertBefore(scissor, span.nextSibling);
|
| 161 |
+
// ํด๋ฆญํ span์ data-index๋ฅผ ๊ธฐ์ค์ผ๋ก ๊ทธ ๋ค ํ
์คํธ ํ์ ์ฒ๋ฆฌ
|
| 162 |
var cutIndex = parseInt(span.getAttribute("data-index"));
|
| 163 |
spans.forEach(function(s) {
|
| 164 |
var idx = parseInt(s.getAttribute("data-index"));
|
|
|
|
| 166 |
s.style.color = "grey";
|
| 167 |
}
|
| 168 |
});
|
| 169 |
+
// ์ฌ๊ธฐ์ ๋ฐ๋ก human ๋ฉ์์ง์์ โ๏ธ ์ด์ ํ
์คํธ๋ฅผ hidden input์ ์
๋ฐ์ดํธ
|
| 170 |
+
var edited_text = container.innerText.split("โ๏ธ")[0];
|
| 171 |
+
var hiddenInput = document.getElementById("edited_text_input");
|
| 172 |
+
if(hiddenInput) {
|
| 173 |
+
hiddenInput.value = edited_text;
|
| 174 |
+
}
|
| 175 |
}
|
| 176 |
});
|
| 177 |
</script>
|
| 178 |
"""
|
| 179 |
)
|
| 180 |
+
|
| 181 |
+
# (B) ๋ ์ด์ submit ํด๋ฆญ ์ ๋ณ๋์ ์คํฌ๋ฆฝํธ๋ก ์
๋ฐ์ดํธํ ํ์ ์์
|
| 182 |
+
# ์๋ ์๋ DOMContentLoaded ์คํฌ๋ฆฝํธ๋ ์ ๊ฑฐํฉ๋๋ค.
|
| 183 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
# (C) CSS ์คํ์ผ
|
| 185 |
gr.HTML(
|
| 186 |
"""
|
|
|
|
| 237 |
gr.Markdown("## Chat Interface")
|
| 238 |
|
| 239 |
with gr.Column(elem_classes="chat-container"):
|
|
|
|
| 240 |
human_bubble = gr.HTML(get_initial_human_html())
|
|
|
|
| 241 |
ai_html = f"""
|
| 242 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
| 243 |
<div class="emoji">๐ค</div>
|
|
|
|
| 249 |
# ์จ๊น ํ
์คํธ๋ฐ์ค (ํธ์ง๋ ํ
์คํธ ์ ์ฅ์ฉ)
|
| 250 |
edited_text_input = gr.Textbox(visible=False, elem_id="edited_text_input")
|
| 251 |
|
|
|
|
| 252 |
with gr.Row():
|
| 253 |
start_button = gr.Button("Start Typing")
|
| 254 |
submit_button = gr.Button("Submit", elem_id="submit_btn")
|
| 255 |
|
|
|
|
| 256 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 257 |
submit_button.click(fn=submit_edit, inputs=edited_text_input, outputs=[human_bubble, ai_bubble])
|
| 258 |
|