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