Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,76 @@
|
|
| 1 |
-
import time
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
for ch in text:
|
| 8 |
-
out += ch
|
| 9 |
-
time.sleep(0.05) # ๊ธ์๋น ์ง์ฐ (0.05์ด)
|
| 10 |
-
yield out
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
html += "</div>"
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# HTML์ ์ฝ์
ํ ์๋ฐ์คํฌ๋ฆฝํธ
|
| 29 |
-
custom_js = """
|
| 30 |
-
<script>
|
| 31 |
-
function handleClick(index) {
|
| 32 |
-
document.getElementById("click_index").value = index;
|
| 33 |
-
document.getElementById("trigger_button").click();
|
| 34 |
-
}
|
| 35 |
-
</script>
|
| 36 |
-
"""
|
| 37 |
-
|
| 38 |
-
# "Start" ๋ฒํผ ํด๋ฆญ ์ ์คํ๋๋ ํจ์
|
| 39 |
-
def start_sentence(text):
|
| 40 |
-
current_cut = 0
|
| 41 |
-
clickable = get_clickable_html(text, current_cut) + custom_js
|
| 42 |
-
# ์คํธ๋ฆฌ๋ฐ์ ์ ์ฒด ๋ฌธ์ฅ์ ํ ๊ธ์์ฉ ์ถ๋ ฅ
|
| 43 |
-
return stream_text(text), text, current_cut, clickable
|
| 44 |
-
|
| 45 |
-
# "Restart Streaming" ๋ฒํผ ํด๋ฆญ ์ ์คํ๋๋ ํจ์
|
| 46 |
-
def stream_current(original_sentence, current_cut):
|
| 47 |
-
new_text = original_sentence[current_cut:]
|
| 48 |
-
return stream_text(new_text)
|
| 49 |
-
|
| 50 |
-
# HTML์ ํน์ ๊ธ์๋ฅผ ํด๋ฆญํ ๋ ์คํ๋๋ ์ฝ๋ฐฑ ํจ์
|
| 51 |
-
def update_cut(index, original_sentence, current_cut):
|
| 52 |
-
index = int(index)
|
| 53 |
-
new_cut = current_cut
|
| 54 |
-
if index > current_cut:
|
| 55 |
-
new_cut = index
|
| 56 |
-
clickable = get_clickable_html(original_sentence, new_cut) + custom_js
|
| 57 |
-
return original_sentence, new_cut, clickable
|
| 58 |
|
| 59 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 60 |
with gr.Blocks() as demo:
|
|
|
|
| 61 |
with gr.Row():
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
original_sentence_state = gr.State("")
|
| 77 |
-
current_cut_state = gr.State(0)
|
| 78 |
-
|
| 79 |
-
# โ "Start" ๋ฒํผ ํด๋ฆญ ์: ์
๋ ฅ ๋ฌธ์ฅ์ ์ํ์ ์ ์ฅํ๊ณ ์คํธ๋ฆฌ๋ฐ๊ณผ ํ๋จ HTML ์ด๊ธฐํ
|
| 80 |
-
start_btn.click(
|
| 81 |
-
start_sentence,
|
| 82 |
-
inputs=inp,
|
| 83 |
-
outputs=[stream_box, original_sentence_state, current_cut_state, clickable_display]
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
# โก HTML ๋ด ๊ธ์ ํด๋ฆญ โ ์จ๊ฒจ์ง ๋ฒํผ ํด๋ฆญ ์: update_cut ํจ์ ์คํ
|
| 87 |
-
trigger_button.click(
|
| 88 |
-
update_cut,
|
| 89 |
-
inputs=[click_index, original_sentence_state, current_cut_state],
|
| 90 |
-
outputs=[original_sentence_state, current_cut_state, clickable_display]
|
| 91 |
-
)
|
| 92 |
-
|
| 93 |
-
# โข "Restart Streaming" ๋ฒํผ ํด๋ฆญ ์: ํ์ฌ ์ํ(์๋ฆฐ ํ์ ๋ฌธ์ฅ)๋ฅผ ์คํธ๋ฆฌ๋ฐ
|
| 94 |
-
# ์ฌ๊ธฐ์ stream ์ธ์(stream=True)๋ฅผ ์ ๊ฑฐํฉ๋๋ค.
|
| 95 |
-
stream_btn.click(
|
| 96 |
-
stream_current,
|
| 97 |
-
inputs=[original_sentence_state, current_cut_state],
|
| 98 |
-
outputs=stream_box
|
| 99 |
-
)
|
| 100 |
|
| 101 |
demo.launch()
|
| 102 |
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import time
|
| 3 |
|
| 4 |
+
# ๋ฉ์์ง ๋ด์ฉ ์ค์
|
| 5 |
+
human_message = "Hello. I'd like to find a round trip commercial airline flight from San Francisco to Denver."
|
| 6 |
+
ai_message = "Hello, how can I help you?"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def stream_human_message():
|
| 9 |
+
"""
|
| 10 |
+
Human์ ๋ฉ์์ง๋ฅผ ๋ฌธ์ ๋จ์๋ก ํ์ดํํ๋ ๊ฒ์ฒ๋ผ ์คํธ๋ฆฌ๋ฐํฉ๋๋ค.
|
| 11 |
+
๊ฐ ๋ฌธ์๋ <span> ํ๊ทธ๋ก ๊ฐ์ธ์ onclick ์ด๋ฒคํธ๋ก ์ฌ์ฉ์๊ฐ ํด๋ฆญํ ์ ์๋๋ก ํฉ๋๋ค.
|
| 12 |
+
ํด๋ฆญ ์ JavaScript ํจ์ handleClick()๊ฐ ํธ์ถ๋์ด,
|
| 13 |
+
ํด๋น ๋ฌธ์์ ๋ฐ๋ก ๋ค์ โ๏ธ ์ด๋ชจํฐ์ฝ์ ์ฝ์
ํ๊ณ ๊ทธ ๋ค์ ๋ฌธ์๋ค์ ํ์์ผ๋ก ํ์๋ฉ๋๋ค.
|
| 14 |
+
"""
|
| 15 |
+
# ํด๋ฆญ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋ฅผ ์ํ JavaScript ์ฝ๋.
|
| 16 |
+
# (ํ์ด์ง์ ๋จ ํ ๋ฒ๋ง ๋ก๋๋๋๋ก window.cutDone ํ๋๊ทธ๋ก ์ค๋ณต ์คํ์ ๋ฐฉ์งํฉ๋๋ค.)
|
| 17 |
+
script = """
|
| 18 |
+
<script>
|
| 19 |
+
if (window.cutDone === undefined) { window.cutDone = false; }
|
| 20 |
+
function handleClick(span) {
|
| 21 |
+
if (window.cutDone) return; // ์ด๋ฏธ ํด๋ฆญํ ๊ฒฝ์ฐ ๋ฌด์
|
| 22 |
+
window.cutDone = true;
|
| 23 |
+
// ํด๋ฆญํ span ๋ฐ๋ก ๋ค์ โ๏ธ ์ด๋ชจํฐ์ฝ ์ฝ์
|
| 24 |
+
var scissor = document.createElement('span');
|
| 25 |
+
scissor.textContent = 'โ๏ธ';
|
| 26 |
+
span.parentNode.insertBefore(scissor, span.nextSibling);
|
| 27 |
+
// ํด๋ฆญํ ๋ฌธ์์ data-index ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ดํ ๋ฌธ์๋ค์ ์์์ ํ์์ผ๋ก ๋ณ๊ฒฝ
|
| 28 |
+
var cutIndex = parseInt(span.getAttribute('data-index'));
|
| 29 |
+
var spans = span.parentNode.querySelectorAll('span[data-index]');
|
| 30 |
+
spans.forEach(function(s) {
|
| 31 |
+
var idx = parseInt(s.getAttribute('data-index'));
|
| 32 |
+
if (idx > cutIndex) {
|
| 33 |
+
s.style.color = 'grey';
|
| 34 |
+
}
|
| 35 |
+
});
|
| 36 |
+
}
|
| 37 |
+
</script>
|
| 38 |
+
"""
|
| 39 |
+
# human ๋ฉ์์ง๋ฅผ ๊ฐ์ธ๋ HTML ์ปจํ
์ด๋ ์์ (์ค๋ฅธ์ชฝ ์ ๋ ฌ, ๋ฐฐ๊ฒฝ ๋ฐ ํ
๋๋ฆฌ ์คํ์ผ)
|
| 40 |
+
html = script + (
|
| 41 |
+
"<div id='human_message' "
|
| 42 |
+
"style='text-align: right; background-color: #d0f0d0; padding: 10px; "
|
| 43 |
+
"border-radius: 10px; display: inline-block;'>"
|
| 44 |
+
)
|
| 45 |
+
# ์ด๊ธฐ ์ํ: ์์ง ๋ด์ฉ์ด ์์ ๋
|
| 46 |
+
yield html
|
| 47 |
+
# ๋ฌธ์ ํ๋์ฉ <span> ํ๊ทธ๋ก ์ถ๊ฐํ๋ฉด์ ์คํธ๋ฆฌ๋ฐ ํจ๊ณผ๋ฅผ ์ค๋๋ค.
|
| 48 |
+
for i, ch in enumerate(human_message):
|
| 49 |
+
html += f"<span data-index='{i}' onclick='handleClick(this)'>{ch}</span>"
|
| 50 |
+
yield html
|
| 51 |
+
time.sleep(0.05) # ํ์ดํ ํจ๊ณผ๋ฅผ ์ํ ๋๋ ์ด (0.05์ด)
|
| 52 |
+
# ์ปจํ
์ด๋ ์ข
๋ฃ ํ๊ทธ ์ถ๊ฐ
|
| 53 |
html += "</div>"
|
| 54 |
+
yield html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 57 |
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("## Chat Interface")
|
| 59 |
with gr.Row():
|
| 60 |
+
# ์ผ์ชฝ: AI ๋งํ์ (๊ณ ์ ํ
์คํธ)
|
| 61 |
+
with gr.Column():
|
| 62 |
+
ai_bubble = gr.HTML(
|
| 63 |
+
f"<div style='text-align: left; background-color: #e0e0e0; padding: 10px; "
|
| 64 |
+
"border-radius: 10px; display: inline-block;'>"
|
| 65 |
+
f"{ai_message}"
|
| 66 |
+
"</div>"
|
| 67 |
+
)
|
| 68 |
+
# ์ค๋ฅธ์ชฝ: Human ๋งํ์ (์คํธ๋ฆฌ๋ฐ ํ
์คํธ)
|
| 69 |
+
with gr.Column():
|
| 70 |
+
human_bubble = gr.HTML("")
|
| 71 |
+
# ์คํธ๋ฆฌ๋ฐ(ํ์ดํ ํจ๊ณผ)๋ฅผ ์์ํ ๋ฒํผ
|
| 72 |
+
start_button = gr.Button("Start Typing")
|
| 73 |
+
start_button.click(fn=stream_human_message, outputs=human_bubble, stream=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
demo.launch()
|
| 76 |
|