Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,85 @@
|
|
| 1 |
import time
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def stream_human_message():
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
Start Typing์ ๋๋ฅด๋ฉด ์ด์ ์ํ๊ฐ ๋ฆฌ์
๋ฉ๋๋ค.
|
| 13 |
"""
|
| 14 |
bubble_content = ""
|
| 15 |
-
# wrapper ์์: ์ ์ฒด ๋๋น๋ฅผ ์ฑ์ฐ๊ณ , ์ค๋ฅธ์ชฝ ์ ๋ ฌํ๋ flex ์ปจํ
์ด๋
|
| 16 |
wrapper_start = (
|
| 17 |
"""<div class="human-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-end; gap: 5px; width: 100%;">"""
|
| 18 |
)
|
| 19 |
-
# ๋งํ์ ์ปจํ
์ด๋ ์์
|
| 20 |
bubble_start = """<div id="human_message" class="speech-bubble human">"""
|
| 21 |
bubble_end = "</div>"
|
| 22 |
-
# ์ค๋ฅธ์ชฝ์ ๊ณ ์ ๋ ์ด๋ชจํฐ์ฝ
|
| 23 |
emoji_html = "<div class='emoji'>๐ง</div>"
|
| 24 |
wrapper_end = "</div>"
|
| 25 |
-
|
| 26 |
-
# ์ด๊ธฐ ๋น ๋งํ์ ๊ณผ ์ด๋ชจํฐ์ฝ ์ถ๋ ฅ
|
| 27 |
-
html = wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 28 |
-
yield html
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
| 31 |
for i, ch in enumerate(human_message):
|
| 32 |
bubble_content += f"<span data-index='{i}'>{ch}</span>"
|
| 33 |
current_html = wrapper_start + bubble_start + bubble_content + bubble_end + emoji_html + wrapper_end
|
|
@@ -35,23 +87,24 @@ def stream_human_message():
|
|
| 35 |
time.sleep(0.05)
|
| 36 |
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
-
#
|
|
|
|
|
|
|
| 39 |
gr.HTML(
|
| 40 |
"""
|
| 41 |
<script>
|
| 42 |
-
// human ๋งํ์ ๋ด๋ถ์ span[data-index]์ ๋ํด ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
| 43 |
document.addEventListener("click", function(event) {
|
|
|
|
| 44 |
if (event.target && event.target.matches("div.speech-bubble.human span[data-index]")) {
|
| 45 |
var span = event.target;
|
| 46 |
-
// ๊ฐ์ฅ ๊ฐ๊น์ด human ๋งํ์ ์ปจํ
์ด๋ ์ ํ
|
| 47 |
var container = span.closest("div.speech-bubble.human");
|
| 48 |
// ๊ธฐ์กด์ ์ฝ์
๋ โ๏ธ ์ด๋ชจํฐ์ฝ(ํด๋์ค "scissor") ์ ๊ฑฐ
|
| 49 |
var oldScissors = container.querySelectorAll("span.scissor");
|
| 50 |
oldScissors.forEach(function(s) { s.remove(); });
|
| 51 |
-
// ๋ชจ๋ span์ ์์์
|
| 52 |
var spans = container.querySelectorAll("span[data-index]");
|
| 53 |
spans.forEach(function(s) { s.style.color = ''; });
|
| 54 |
-
// ํด๋ฆญํ span ๋ฐ๋ก ๋ค์ โ๏ธ ์ด
|
| 55 |
var scissor = document.createElement('span');
|
| 56 |
scissor.textContent = 'โ๏ธ';
|
| 57 |
scissor.classList.add("scissor");
|
|
@@ -70,18 +123,18 @@ with gr.Blocks() as demo:
|
|
| 70 |
"""
|
| 71 |
)
|
| 72 |
|
| 73 |
-
# CSS ์คํ์ผ
|
| 74 |
gr.HTML(
|
| 75 |
"""
|
| 76 |
<style>
|
| 77 |
-
/* ์ ์ฒด ์ฑํ
์์ญ
|
| 78 |
.chat-container {
|
| 79 |
display: flex;
|
| 80 |
flex-direction: column;
|
| 81 |
gap: 10px;
|
| 82 |
width: 100%;
|
| 83 |
}
|
| 84 |
-
/* ๋งํ์
|
| 85 |
.speech-bubble {
|
| 86 |
position: relative;
|
| 87 |
padding: 10px 15px;
|
|
@@ -91,12 +144,11 @@ with gr.Blocks() as demo:
|
|
| 91 |
font-size: 16px;
|
| 92 |
line-height: 1.4;
|
| 93 |
}
|
| 94 |
-
/* Human ๋งํ์
|
| 95 |
.human {
|
| 96 |
background: #d0f0d0;
|
| 97 |
margin-right: 10px;
|
| 98 |
}
|
| 99 |
-
/* Human ๋งํ์ ์ ๊ผฌ๋ฆฌ (์ค๋ฅธ์ชฝ) */
|
| 100 |
.human::after {
|
| 101 |
content: "";
|
| 102 |
position: absolute;
|
|
@@ -106,12 +158,11 @@ with gr.Blocks() as demo:
|
|
| 106 |
border-style: solid;
|
| 107 |
border-color: transparent transparent transparent #d0f0d0;
|
| 108 |
}
|
| 109 |
-
/* AI ๋งํ์
|
| 110 |
.ai {
|
| 111 |
background: #e0e0e0;
|
| 112 |
margin-left: 10px;
|
| 113 |
}
|
| 114 |
-
/* AI ๋งํ์ ์ ๊ผฌ๋ฆฌ (์ผ์ชฝ) */
|
| 115 |
.ai::after {
|
| 116 |
content: "";
|
| 117 |
position: absolute;
|
|
@@ -121,7 +172,7 @@ with gr.Blocks() as demo:
|
|
| 121 |
border-style: solid;
|
| 122 |
border-color: transparent #e0e0e0 transparent transparent;
|
| 123 |
}
|
| 124 |
-
/*
|
| 125 |
.emoji {
|
| 126 |
font-size: 24px;
|
| 127 |
line-height: 1;
|
|
@@ -133,9 +184,10 @@ with gr.Blocks() as demo:
|
|
| 133 |
gr.Markdown("## Chat Interface")
|
| 134 |
|
| 135 |
with gr.Column(elem_classes="chat-container"):
|
| 136 |
-
# Human ๋ฉ์์ง
|
| 137 |
-
human_bubble = gr.HTML(
|
| 138 |
-
|
|
|
|
| 139 |
ai_html = """
|
| 140 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
| 141 |
<div class="emoji">๐ค</div>
|
|
@@ -144,7 +196,9 @@ with gr.Blocks() as demo:
|
|
| 144 |
"""
|
| 145 |
ai_bubble = gr.HTML(ai_html)
|
| 146 |
|
|
|
|
| 147 |
start_button = gr.Button("Start Typing")
|
| 148 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 149 |
|
| 150 |
demo.launch()
|
|
|
|
|
|
| 1 |
import time
|
| 2 |
import gradio as gr
|
| 3 |
+
import random
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
|
| 6 |
+
def get_random_row_from_dataset():
|
| 7 |
+
"""
|
| 8 |
+
Hugging Face Dataset repository 'gaeunseo/Taskmaster_sample_data'์ train split์์
|
| 9 |
+
conversation_id๋ณ๋ก ๊ทธ๋ฃนํํ ํ, ๊ฐ์ conversation_id๋ฅผ ๊ฐ์ง ๋ชจ๋ ํ๋ค์ used ์ปฌ๋ผ์ด False์ด๊ณ ,
|
| 10 |
+
๊ทธ ๊ทธ๋ฃน ๋ด์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ์ด ์กด์ฌํ๋ ๊ทธ๋ฃน๋ค ์ค์์ ๋๋คํ๊ฒ ํ๋์ ๊ทธ๋ฃน์ ์ ํํ๊ณ ,
|
| 11 |
+
ํด๋น ๊ทธ๋ฃน ๋ด์์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ์ ์ ํํ์ฌ ๋ฐํํฉ๋๋ค.
|
| 12 |
+
"""
|
| 13 |
+
# ๋ฐ์ดํฐ์
๋ก๋ (split ์ด๋ฆ์ ์ํฉ์ ๋ง๊ฒ ์กฐ์ )
|
| 14 |
+
ds = load_dataset("gaeunseo/Taskmaster_sample_data", split="train")
|
| 15 |
+
|
| 16 |
+
# conversation_id๋ณ๋ก ํ๋ค์ ๊ทธ๋ฃนํ
|
| 17 |
+
conversation_groups = {}
|
| 18 |
+
for row in ds:
|
| 19 |
+
cid = row["conversation_id"]
|
| 20 |
+
conversation_groups.setdefault(cid, []).append(row)
|
| 21 |
+
|
| 22 |
+
# ๋ชจ๋ ํ๋ค์ used ์ปฌ๋ผ์ด False์ด๊ณ , ๊ทธ๋ฃน ๋ด์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ์ด ์กด์ฌํ๋ ๊ทธ๋ฃน๋ง ํํฐ๋ง
|
| 23 |
+
valid_groups = [
|
| 24 |
+
group for group in conversation_groups.values()
|
| 25 |
+
if all(not row["used"] for row in group) and any(row["overlapping"] == "TT" for row in group)
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
if not valid_groups:
|
| 29 |
+
return None # ์ ํจํ ๋ํ ๊ทธ๋ฃน์ด ์์ผ๋ฉด None ๋ฐํ
|
| 30 |
+
|
| 31 |
+
# ์ ํจํ ๊ทธ๋ฃน ์ค ๋๋คํ๊ฒ ํ๋ ์ ํ
|
| 32 |
+
chosen_group = random.choice(valid_groups)
|
| 33 |
+
# ์ ํ๋ ๊ทธ๋ฃน ๋ด์์ overlapping ์ปฌ๋ผ์ด "TT"์ธ ํ์ ์ ํ (์ฌ๋ฌ ๊ฐ๋ผ๋ฉด ์ฒซ ๋ฒ์งธ ๊ฒ์ ์ฌ์ฉ)
|
| 34 |
+
for row in chosen_group:
|
| 35 |
+
if row["overlapping"] == "TT":
|
| 36 |
+
return text
|
| 37 |
+
|
| 38 |
+
# ๋ฐ์ดํฐ์
์์ ํ๋์ ํ์ ๊ฐ์ ธ์์ human_message์ ai_message์ ํ ๋น
|
| 39 |
+
row = get_random_row_from_dataset()
|
| 40 |
+
if row is None:
|
| 41 |
+
human_message = "No valid conversation available."
|
| 42 |
+
ai_message = "No valid conversation available."
|
| 43 |
+
else:
|
| 44 |
+
raw_text = row['text']
|
| 45 |
+
human_message = raw_text.split("[turn]")[0].strip()
|
| 46 |
+
ai_message = raw_text.split("[turn]")[1].strip()
|
| 47 |
+
|
| 48 |
+
#############################################
|
| 49 |
+
# ์ดํ ์๋๋ ๊ธฐ์กด ์ฑํ
์ธํฐํ์ด์ค ์ฝ๋ (๋งํ์ , ์ด๋ชจํฐ์ฝ, ํ์ดํ ํจ๊ณผ, ํด๋ฆญ ์ธํฐ๋์
) ์
๋๋ค.
|
| 50 |
+
#############################################
|
| 51 |
+
|
| 52 |
+
def get_initial_human_html():
|
| 53 |
+
"""
|
| 54 |
+
ํ์ด์ง ๋ก๋ ์ Human ๋งํ์ (๋น ๋ด์ฉ)๊ณผ ์ค๋ฅธ์ชฝ์ ๐ง ์ด๋ชจํฐ์ฝ์ด ๋ณด์ด๋๋ก ์ด๊ธฐ HTML ๋ฐํ
|
| 55 |
+
"""
|
| 56 |
+
wrapper_start = (
|
| 57 |
+
"""<div class="human-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-end; gap: 5px; width: 100%;">"""
|
| 58 |
+
)
|
| 59 |
+
bubble_start = """<div id="human_message" class="speech-bubble human">"""
|
| 60 |
+
bubble_end = "</div>"
|
| 61 |
+
emoji_html = "<div class='emoji'>๐ง</div>"
|
| 62 |
+
wrapper_end = "</div>"
|
| 63 |
+
return wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 64 |
|
| 65 |
def stream_human_message():
|
| 66 |
"""
|
| 67 |
+
Start Typing ๋ฒํผ ํด๋ฆญ ์ human_message๋ฅผ ํ ๊ธ์์ฉ ํ์ดํ ํจ๊ณผ๋ก ์ถ๋ ฅ.
|
| 68 |
+
์ด์ ์ํ(โ๏ธ ์์ด์ฝ, ํ์ ์ฒ๋ฆฌ ๋ฑ)๋ ๋ฆฌ์
๋ฉ๋๋ค.
|
|
|
|
| 69 |
"""
|
| 70 |
bubble_content = ""
|
|
|
|
| 71 |
wrapper_start = (
|
| 72 |
"""<div class="human-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-end; gap: 5px; width: 100%;">"""
|
| 73 |
)
|
|
|
|
| 74 |
bubble_start = """<div id="human_message" class="speech-bubble human">"""
|
| 75 |
bubble_end = "</div>"
|
|
|
|
| 76 |
emoji_html = "<div class='emoji'>๐ง</div>"
|
| 77 |
wrapper_end = "</div>"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
# ์ด๊ธฐ ์ํ (๋น ๋งํ์ ๊ณผ ์ด๋ชจํฐ์ฝ)
|
| 80 |
+
yield wrapper_start + bubble_start + bubble_end + emoji_html + wrapper_end
|
| 81 |
+
|
| 82 |
+
# human_message๋ฅผ ํ ๊ธ์์ฉ ์ถ๊ฐ (ํ์ดํ ํจ๊ณผ)
|
| 83 |
for i, ch in enumerate(human_message):
|
| 84 |
bubble_content += f"<span data-index='{i}'>{ch}</span>"
|
| 85 |
current_html = wrapper_start + bubble_start + bubble_content + bubble_end + emoji_html + wrapper_end
|
|
|
|
| 87 |
time.sleep(0.05)
|
| 88 |
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
+
# ํ์ด์ง ์๋จ์ ๋จ ํ ๋ฒ ๋ก๋๋ ์คํฌ๋ฆฝํธ:
|
| 91 |
+
# - Human ๋งํ์ ๋ด์ ๏ฟฝ๏ฟฝ๏ฟฝ <span data-index="...">๋ฅผ ํด๋ฆญํ๋ฉด โ๏ธ ์ด๋ชจํฐ์ฝ์ด ํด๋น ์์น์ ์ฝ์
๋๊ณ ,
|
| 92 |
+
# ๊ทธ ์ดํ์ ํ
์คํธ ์์์ด ํ์์ผ๋ก ๋ณ๊ฒฝ๋๋๋ก ์ด๋ฒคํธ ์์ ๋ฐฉ์์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค.
|
| 93 |
gr.HTML(
|
| 94 |
"""
|
| 95 |
<script>
|
|
|
|
| 96 |
document.addEventListener("click", function(event) {
|
| 97 |
+
// Human ๋งํ์ ๋ด์ <span data-index="...">๊ฐ ํด๋ฆญ๋์์ ๋๋ง ์ฒ๋ฆฌ
|
| 98 |
if (event.target && event.target.matches("div.speech-bubble.human span[data-index]")) {
|
| 99 |
var span = event.target;
|
|
|
|
| 100 |
var container = span.closest("div.speech-bubble.human");
|
| 101 |
// ๊ธฐ์กด์ ์ฝ์
๋ โ๏ธ ์ด๋ชจํฐ์ฝ(ํด๋์ค "scissor") ์ ๊ฑฐ
|
| 102 |
var oldScissors = container.querySelectorAll("span.scissor");
|
| 103 |
oldScissors.forEach(function(s) { s.remove(); });
|
| 104 |
+
// ๋ชจ๋ span์ ์์์ ์ด๊ธฐํ
|
| 105 |
var spans = container.querySelectorAll("span[data-index]");
|
| 106 |
spans.forEach(function(s) { s.style.color = ''; });
|
| 107 |
+
// ํด๋ฆญํ span ๋ฐ๋ก ๋ค์ โ๏ธ ์์ด์ฝ ์ฝ์
|
| 108 |
var scissor = document.createElement('span');
|
| 109 |
scissor.textContent = 'โ๏ธ';
|
| 110 |
scissor.classList.add("scissor");
|
|
|
|
| 123 |
"""
|
| 124 |
)
|
| 125 |
|
| 126 |
+
# CSS ์คํ์ผ (๋งํ์ ๋ชจ์, ์ด๋ชจํฐ์ฝ, ์์น ๋ฑ)
|
| 127 |
gr.HTML(
|
| 128 |
"""
|
| 129 |
<style>
|
| 130 |
+
/* ์ ์ฒด ์ฑํ
์์ญ */
|
| 131 |
.chat-container {
|
| 132 |
display: flex;
|
| 133 |
flex-direction: column;
|
| 134 |
gap: 10px;
|
| 135 |
width: 100%;
|
| 136 |
}
|
| 137 |
+
/* ๊ณตํต ๋งํ์ ์คํ์ผ */
|
| 138 |
.speech-bubble {
|
| 139 |
position: relative;
|
| 140 |
padding: 10px 15px;
|
|
|
|
| 144 |
font-size: 16px;
|
| 145 |
line-height: 1.4;
|
| 146 |
}
|
| 147 |
+
/* Human ๋งํ์ (์ค๋ฅธ์ชฝ ์ ๋ ฌ, ์ด๋ก ๋ฐฐ๊ฒฝ) */
|
| 148 |
.human {
|
| 149 |
background: #d0f0d0;
|
| 150 |
margin-right: 10px;
|
| 151 |
}
|
|
|
|
| 152 |
.human::after {
|
| 153 |
content: "";
|
| 154 |
position: absolute;
|
|
|
|
| 158 |
border-style: solid;
|
| 159 |
border-color: transparent transparent transparent #d0f0d0;
|
| 160 |
}
|
| 161 |
+
/* AI ๋งํ์ (์ผ์ชฝ ์ ๋ ฌ, ํ์ ๋ฐฐ๊ฒฝ) */
|
| 162 |
.ai {
|
| 163 |
background: #e0e0e0;
|
| 164 |
margin-left: 10px;
|
| 165 |
}
|
|
|
|
| 166 |
.ai::after {
|
| 167 |
content: "";
|
| 168 |
position: absolute;
|
|
|
|
| 172 |
border-style: solid;
|
| 173 |
border-color: transparent #e0e0e0 transparent transparent;
|
| 174 |
}
|
| 175 |
+
/* ์ด๋ชจํฐ์ฝ ์คํ์ผ */
|
| 176 |
.emoji {
|
| 177 |
font-size: 24px;
|
| 178 |
line-height: 1;
|
|
|
|
| 184 |
gr.Markdown("## Chat Interface")
|
| 185 |
|
| 186 |
with gr.Column(elem_classes="chat-container"):
|
| 187 |
+
# ํ์ด์ง ๋ก๋ ์ Human ๋งํ์ (๋น ๋ฉ์์ง)๊ณผ ์ค๋ฅธ์ชฝ ๐ง ์ด๋ชจํฐ์ฝ์ด ๋ณด์ด๋๋ก ์ด๊ธฐ HTML ์ ์ฉ
|
| 188 |
+
human_bubble = gr.HTML(get_initial_human_html())
|
| 189 |
+
|
| 190 |
+
# AI ๋งํ์ : ์ผ์ชฝ์ ๐ค ์ด๋ชจํฐ์ฝ๊ณผ ํจ๊ป ๊ณ ์ ๋ฉ์์ง ์ถ๋ ฅ
|
| 191 |
ai_html = """
|
| 192 |
<div class="ai-wrapper" style="display: flex; align-items: flex-end; justify-content: flex-start; gap: 5px; width: 100%;">
|
| 193 |
<div class="emoji">๐ค</div>
|
|
|
|
| 196 |
"""
|
| 197 |
ai_bubble = gr.HTML(ai_html)
|
| 198 |
|
| 199 |
+
# Start Typing ๋ฒํผ์ ๋๋ฅด๋ฉด human_message๊ฐ ์คํธ๋ฆฌ๋ฐ(ํ์ดํ ํจ๊ณผ)๋๋ฉฐ, ์ด์ ์ ์๋ โ๏ธ/ํ์ ์ฒ๋ฆฌ ๋ฑ์ ๋ฆฌ์
๋ฉ๋๋ค.
|
| 200 |
start_button = gr.Button("Start Typing")
|
| 201 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 202 |
|
| 203 |
demo.launch()
|
| 204 |
+
|