Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,48 +7,61 @@ ai_message = "Hello, how can I help you?"
|
|
| 7 |
|
| 8 |
def stream_human_message():
|
| 9 |
"""
|
| 10 |
-
Human ๋ฉ์์ง๋ฅผ ํ ๊ธ์์ฉ
|
| 11 |
-
|
|
|
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
yield html
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
-
#
|
| 26 |
gr.HTML(
|
| 27 |
"""
|
| 28 |
<script>
|
| 29 |
-
//
|
| 30 |
document.addEventListener("click", function(event) {
|
| 31 |
-
// human ๋งํ์ ๋ด๋ถ์ data-index ์์ฑ์ ๊ฐ์ง span์ด ํด๋ฆญ๋ ๊ฒฝ์ฐ์๋ง ์ฒ๋ฆฌ
|
| 32 |
if (event.target && event.target.matches("div.speech-bubble.human span[data-index]")) {
|
| 33 |
var span = event.target;
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
var oldScissors = container.querySelectorAll("span.scissor");
|
| 37 |
oldScissors.forEach(function(s) { s.remove(); });
|
| 38 |
-
// ๋ชจ๋ span์
|
| 39 |
var spans = container.querySelectorAll("span[data-index]");
|
| 40 |
spans.forEach(function(s) { s.style.color = ''; });
|
| 41 |
-
// ํด๋ฆญํ span ๋ค์ โ๏ธ
|
| 42 |
var scissor = document.createElement('span');
|
| 43 |
scissor.textContent = 'โ๏ธ';
|
| 44 |
-
scissor.classList.add(
|
| 45 |
container.insertBefore(scissor, span.nextSibling);
|
| 46 |
-
// ํด๋ฆญํ
|
| 47 |
-
var cutIndex = parseInt(span.getAttribute(
|
| 48 |
spans.forEach(function(s) {
|
| 49 |
-
var idx = parseInt(s.getAttribute(
|
| 50 |
if (idx > cutIndex) {
|
| 51 |
-
s.style.color =
|
| 52 |
}
|
| 53 |
});
|
| 54 |
}
|
|
@@ -57,17 +70,18 @@ with gr.Blocks() as demo:
|
|
| 57 |
"""
|
| 58 |
)
|
| 59 |
|
| 60 |
-
# CSS
|
| 61 |
gr.HTML(
|
| 62 |
"""
|
| 63 |
<style>
|
| 64 |
-
/* ์ ์ฒด ์ฑํ
์์ญ: ์ธ๋ก ๋ฐฐ์น */
|
| 65 |
.chat-container {
|
| 66 |
display: flex;
|
| 67 |
flex-direction: column;
|
| 68 |
gap: 10px;
|
|
|
|
| 69 |
}
|
| 70 |
-
/*
|
| 71 |
.speech-bubble {
|
| 72 |
position: relative;
|
| 73 |
padding: 10px 15px;
|
|
@@ -77,12 +91,12 @@ with gr.Blocks() as demo:
|
|
| 77 |
font-size: 16px;
|
| 78 |
line-height: 1.4;
|
| 79 |
}
|
| 80 |
-
/* Human ๋งํ์ :
|
| 81 |
.human {
|
| 82 |
background: #d0f0d0;
|
| 83 |
-
align-self: flex-end;
|
| 84 |
margin-right: 10px;
|
| 85 |
}
|
|
|
|
| 86 |
.human::after {
|
| 87 |
content: "";
|
| 88 |
position: absolute;
|
|
@@ -92,12 +106,12 @@ with gr.Blocks() as demo:
|
|
| 92 |
border-style: solid;
|
| 93 |
border-color: transparent transparent transparent #d0f0d0;
|
| 94 |
}
|
| 95 |
-
/* AI ๋งํ์ :
|
| 96 |
.ai {
|
| 97 |
background: #e0e0e0;
|
| 98 |
-
align-self: flex-start;
|
| 99 |
margin-left: 10px;
|
| 100 |
}
|
|
|
|
| 101 |
.ai::after {
|
| 102 |
content: "";
|
| 103 |
position: absolute;
|
|
@@ -105,20 +119,31 @@ with gr.Blocks() as demo:
|
|
| 105 |
top: 10px;
|
| 106 |
border-width: 10px 10px 10px 0;
|
| 107 |
border-style: solid;
|
| 108 |
-
border-color: transparent #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
}
|
| 110 |
</style>
|
| 111 |
"""
|
| 112 |
)
|
| 113 |
|
| 114 |
gr.Markdown("## Chat Interface")
|
|
|
|
| 115 |
with gr.Column(elem_classes="chat-container"):
|
| 116 |
-
#
|
| 117 |
human_bubble = gr.HTML("")
|
| 118 |
-
#
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
-
# ํ์ดํ ์์ ๋ฒํผ: ๋ฒํผ ํด๋ฆญ ์ stream_human_message๊ฐ ํธ์ถ๋์ด ์ HTML์ ์์ฑ (์ฆ, โ๏ธ ์์น๊ฐ ๋ฆฌ์
๋จ)
|
| 122 |
start_button = gr.Button("Start Typing")
|
| 123 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 124 |
|
|
|
|
| 7 |
|
| 8 |
def stream_human_message():
|
| 9 |
"""
|
| 10 |
+
Human ๋ฉ์์ง๋ฅผ ํ ๊ธ์์ฉ ํ์ดํ ํจ๊ณผ๋ก ์คํธ๋ฆฌ๋ฐํ๋ฉด์,
|
| 11 |
+
์ค๋ฅธ์ชฝ์ ๐ง ์ด๋ชจํฐ์ฝ์ด ์๋ wrapper ์์ ๋งํ์ ์ ๊ตฌ์ฑํฉ๋๋ค.
|
| 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 |
+
# ํ ๊ธ์์ฉ ์ถ๊ฐํ์ฌ streaming ์
๋ฐ์ดํธ
|
| 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
|
| 34 |
+
yield current_html
|
| 35 |
+
time.sleep(0.05)
|
| 36 |
+
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
+
# ์ ์ญ์ ์ผ๋ก ํ ๋ฒ๋ง ๋ก๋๋๋ ์คํฌ๋ฆฝํธ: ์ด๋ฒคํธ ์์์ผ๋ก ๋์ ์ผ๋ก ์ถ๊ฐ๋๋ span์ ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
| 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");
|
| 58 |
container.insertBefore(scissor, span.nextSibling);
|
| 59 |
+
// ํด๋ฆญํ span ์ดํ์ ๋ชจ๋ span ํ
์คํธ ์์์ ํ์์ผ๋ก ๋ณ๊ฒฝ
|
| 60 |
+
var cutIndex = parseInt(span.getAttribute("data-index"));
|
| 61 |
spans.forEach(function(s) {
|
| 62 |
+
var idx = parseInt(s.getAttribute("data-index"));
|
| 63 |
if (idx > cutIndex) {
|
| 64 |
+
s.style.color = "grey";
|
| 65 |
}
|
| 66 |
});
|
| 67 |
}
|
|
|
|
| 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 |
font-size: 16px;
|
| 92 |
line-height: 1.4;
|
| 93 |
}
|
| 94 |
+
/* Human ๋งํ์ : ๋ฐฐ๊ฒฝ์ ๋ฐ ์ค๋ฅธ์ชฝ์ ๋ฐฐ์น (wrapper์์ ์ ๋ ฌ) */
|
| 95 |
.human {
|
| 96 |
background: #d0f0d0;
|
|
|
|
| 97 |
margin-right: 10px;
|
| 98 |
}
|
| 99 |
+
/* Human ๋งํ์ ์ ๊ผฌ๋ฆฌ (์ค๋ฅธ์ชฝ) */
|
| 100 |
.human::after {
|
| 101 |
content: "";
|
| 102 |
position: absolute;
|
|
|
|
| 106 |
border-style: solid;
|
| 107 |
border-color: transparent transparent transparent #d0f0d0;
|
| 108 |
}
|
| 109 |
+
/* AI ๋งํ์ : ๋ฐฐ๊ฒฝ์ ๋ฐ ์ผ์ชฝ์ ๋ฐฐ์น (wrapper์์ ์ ๋ ฌ) */
|
| 110 |
.ai {
|
| 111 |
background: #e0e0e0;
|
|
|
|
| 112 |
margin-left: 10px;
|
| 113 |
}
|
| 114 |
+
/* AI ๋งํ์ ์ ๊ผฌ๋ฆฌ (์ผ์ชฝ) */
|
| 115 |
.ai::after {
|
| 116 |
content: "";
|
| 117 |
position: absolute;
|
|
|
|
| 119 |
top: 10px;
|
| 120 |
border-width: 10px 10px 10px 0;
|
| 121 |
border-style: solid;
|
| 122 |
+
border-color: transparent #e0e0e0 transparent transparent;
|
| 123 |
+
}
|
| 124 |
+
/* Emoji ์์ด์ฝ ์คํ์ผ */
|
| 125 |
+
.emoji {
|
| 126 |
+
font-size: 24px;
|
| 127 |
+
line-height: 1;
|
| 128 |
}
|
| 129 |
</style>
|
| 130 |
"""
|
| 131 |
)
|
| 132 |
|
| 133 |
gr.Markdown("## Chat Interface")
|
| 134 |
+
|
| 135 |
with gr.Column(elem_classes="chat-container"):
|
| 136 |
+
# Human ๋ฉ์์ง (์์ชฝ, streaming)
|
| 137 |
human_bubble = gr.HTML("")
|
| 138 |
+
# AI ๋ฉ์์ง (์๋์ชฝ, ๊ณ ์ ) - ์ผ์ชฝ์ ๐ค ์ด๋ชจํฐ์ฝ์ ์ถ๊ฐํ๋ wrapper ์ฌ์ฉ
|
| 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>
|
| 142 |
+
<div id="ai_message" class="speech-bubble ai">""" + ai_message + """</div>
|
| 143 |
+
</div>
|
| 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 |
|