Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,41 +7,44 @@ ai_message = "Hello, how can I help you?"
|
|
| 7 |
|
| 8 |
def stream_human_message():
|
| 9 |
"""
|
| 10 |
-
Human ๋ฉ์์ง๋ฅผ ํ ๊ธ์์ฉ
|
| 11 |
-
๊ฐ
|
| 12 |
"""
|
|
|
|
| 13 |
html = "<div id='human_message' class='speech-bubble human'>"
|
| 14 |
-
yield html # ์ด๊ธฐ
|
| 15 |
# ํ ๊ธ์์ฉ ์ถ๊ฐ
|
| 16 |
for i, ch in enumerate(human_message):
|
| 17 |
html += f"<span data-index='{i}'>{ch}</span>"
|
| 18 |
yield html
|
| 19 |
-
time.sleep(0.05)
|
| 20 |
html += "</div>"
|
| 21 |
yield html
|
| 22 |
|
| 23 |
-
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
-
# ํ์ด์ง ์๋จ์ ํ
|
| 26 |
gr.HTML(
|
| 27 |
"""
|
| 28 |
<script>
|
| 29 |
-
//
|
| 30 |
-
window.cutDone = false;
|
| 31 |
-
// ๋ถ๋ชจ ์ปจํ
์ด๋์ ์ด๋ฒคํธ ์์์ผ๋ก ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
| 32 |
document.addEventListener("click", function(event) {
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
if (event.target && event.target.matches("span[data-index]")) {
|
| 36 |
var span = event.target;
|
| 37 |
-
|
| 38 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
var scissor = document.createElement('span');
|
| 40 |
scissor.textContent = 'โ๏ธ';
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
var cutIndex = parseInt(span.getAttribute('data-index'));
|
| 44 |
-
var spans = span.parentNode.querySelectorAll('span[data-index]');
|
| 45 |
spans.forEach(function(s) {
|
| 46 |
var idx = parseInt(s.getAttribute('data-index'));
|
| 47 |
if (idx > cutIndex) {
|
|
@@ -54,15 +57,17 @@ with gr.Blocks() as demo:
|
|
| 54 |
"""
|
| 55 |
)
|
| 56 |
|
| 57 |
-
# CSS
|
| 58 |
gr.HTML(
|
| 59 |
"""
|
| 60 |
<style>
|
|
|
|
| 61 |
.chat-container {
|
| 62 |
display: flex;
|
| 63 |
flex-direction: column;
|
| 64 |
gap: 10px;
|
| 65 |
}
|
|
|
|
| 66 |
.speech-bubble {
|
| 67 |
position: relative;
|
| 68 |
padding: 10px 15px;
|
|
@@ -72,6 +77,7 @@ with gr.Blocks() as demo:
|
|
| 72 |
font-size: 16px;
|
| 73 |
line-height: 1.4;
|
| 74 |
}
|
|
|
|
| 75 |
.human {
|
| 76 |
background: #d0f0d0;
|
| 77 |
align-self: flex-end;
|
|
@@ -86,6 +92,7 @@ with gr.Blocks() as demo:
|
|
| 86 |
border-style: solid;
|
| 87 |
border-color: transparent transparent transparent #d0f0d0;
|
| 88 |
}
|
|
|
|
| 89 |
.ai {
|
| 90 |
background: #e0e0e0;
|
| 91 |
align-self: flex-start;
|
|
@@ -98,7 +105,7 @@ with gr.Blocks() as demo:
|
|
| 98 |
top: 10px;
|
| 99 |
border-width: 10px 10px 10px 0;
|
| 100 |
border-style: solid;
|
| 101 |
-
border-color: transparent #
|
| 102 |
}
|
| 103 |
</style>
|
| 104 |
"""
|
|
@@ -106,12 +113,12 @@ with gr.Blocks() as demo:
|
|
| 106 |
|
| 107 |
gr.Markdown("## Chat Interface")
|
| 108 |
with gr.Column(elem_classes="chat-container"):
|
| 109 |
-
#
|
| 110 |
human_bubble = gr.HTML("")
|
| 111 |
-
#
|
| 112 |
ai_bubble = gr.HTML(f"<div id='ai_message' class='speech-bubble ai'>{ai_message}</div>")
|
| 113 |
|
| 114 |
-
# ํ์ดํ
|
| 115 |
start_button = gr.Button("Start Typing")
|
| 116 |
start_button.click(fn=stream_human_message, outputs=human_bubble)
|
| 117 |
|
|
|
|
| 7 |
|
| 8 |
def stream_human_message():
|
| 9 |
"""
|
| 10 |
+
Human ๋ฉ์์ง๋ฅผ ํ ๊ธ์์ฉ ํ์ดํํ๋ ํจ๊ณผ๋ก ์คํธ๋ฆฌ๋ฐํฉ๋๋ค.
|
| 11 |
+
๊ฐ ๊ธ์๋ <span> ํ๊ทธ๋ก ๊ฐ์ธ์ง๋ฉฐ, Start Typing ๋ฒํผ์ ๋๋ฅด๋ฉด ์๋กญ๊ฒ ์์ฑ๋์ด ๊ธฐ์กด์ โ๏ธ ์์น ๋ฐ ํ์ ์ฒ๋ฆฌ ๋ฑ์ด ๋ฆฌ์
๋ฉ๋๋ค.
|
| 12 |
"""
|
| 13 |
+
# ์ด๊ธฐ ๋งํ์ ์ปจํ
์ด๋ ์์ฑ
|
| 14 |
html = "<div id='human_message' class='speech-bubble human'>"
|
| 15 |
+
yield html # ์ด๊ธฐ ์ปจํ
์ด๋ ์ถ๋ ฅ
|
| 16 |
# ํ ๊ธ์์ฉ ์ถ๊ฐ
|
| 17 |
for i, ch in enumerate(human_message):
|
| 18 |
html += f"<span data-index='{i}'>{ch}</span>"
|
| 19 |
yield html
|
| 20 |
+
time.sleep(0.05) # ํ์ดํ ํจ๊ณผ ๋๋ ์ด
|
| 21 |
html += "</div>"
|
| 22 |
yield html
|
| 23 |
|
|
|
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
+
# ํ์ด์ง ์๋จ์ ๋จ ํ ๋ฒ ๋ก๋๋ ์คํฌ๋ฆฝํธ: ์ด๋ฒคํธ ์์ ๋ฐฉ์์ผ๋ก ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
| 26 |
gr.HTML(
|
| 27 |
"""
|
| 28 |
<script>
|
| 29 |
+
// ๋ฌธ์ ์ ์ฒด์์ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ๊ฐ์งํ์ฌ, human ๋งํ์ ๋ด์ span ์์์ ๋ํด ์ฒ๋ฆฌํฉ๋๋ค.
|
|
|
|
|
|
|
| 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 |
+
var container = span.parentNode;
|
| 35 |
+
// ๊ธฐ์กด์ ์ฝ์
๋ ๋ชจ๋ scissor ์์ ์ ๊ฑฐ
|
| 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 ๋ค์ โ๏ธ ์์ด์ฝ์ ์ฝ์
(ํด๋์ค "scissor" ์ถ๊ฐ)
|
| 42 |
var scissor = document.createElement('span');
|
| 43 |
scissor.textContent = 'โ๏ธ';
|
| 44 |
+
scissor.classList.add('scissor');
|
| 45 |
+
container.insertBefore(scissor, span.nextSibling);
|
| 46 |
+
// ํด๋ฆญํ ๋ฌธ์ ์ดํ์ ๋ชจ๋ span์ ๋ํด ํ
์คํธ ์์์ ํ์์ผ๋ก ๋ณ๊ฒฝ
|
| 47 |
var cutIndex = parseInt(span.getAttribute('data-index'));
|
|
|
|
| 48 |
spans.forEach(function(s) {
|
| 49 |
var idx = parseInt(s.getAttribute('data-index'));
|
| 50 |
if (idx > cutIndex) {
|
|
|
|
| 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 |
font-size: 16px;
|
| 78 |
line-height: 1.4;
|
| 79 |
}
|
| 80 |
+
/* Human ๋งํ์ : ์ค๋ฅธ์ชฝ ์ ๋ ฌ, ์ด๋ก์ ๋ฐฐ๊ฒฝ ๋ฐ ์ค๋ฅธ์ชฝ ๊ผฌ๋ฆฌ */
|
| 81 |
.human {
|
| 82 |
background: #d0f0d0;
|
| 83 |
align-self: flex-end;
|
|
|
|
| 92 |
border-style: solid;
|
| 93 |
border-color: transparent transparent transparent #d0f0d0;
|
| 94 |
}
|
| 95 |
+
/* AI ๋งํ์ : ์ผ์ชฝ ์ ๋ ฌ, ํ์ ๋ฐฐ๊ฒฝ ๋ฐ ์ผ์ชฝ ๊ผฌ๋ฆฌ */
|
| 96 |
.ai {
|
| 97 |
background: #e0e0e0;
|
| 98 |
align-self: flex-start;
|
|
|
|
| 105 |
top: 10px;
|
| 106 |
border-width: 10px 10px 10px 0;
|
| 107 |
border-style: solid;
|
| 108 |
+
border-color: transparent #e0f0e0 transparent transparent;
|
| 109 |
}
|
| 110 |
</style>
|
| 111 |
"""
|
|
|
|
| 113 |
|
| 114 |
gr.Markdown("## Chat Interface")
|
| 115 |
with gr.Column(elem_classes="chat-container"):
|
| 116 |
+
# ์์ชฝ: Human ๋งํ์ (์คํธ๋ฆฌ๋ฐ ์
๋ฐ์ดํธ๋จ)
|
| 117 |
human_bubble = gr.HTML("")
|
| 118 |
+
# ์๋์ชฝ: AI ๋งํ์ (๊ณ ์ ๋ฉ์์ง)
|
| 119 |
ai_bubble = gr.HTML(f"<div id='ai_message' class='speech-bubble ai'>{ai_message}</div>")
|
| 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 |
|