Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# --- ์คํธ๋ฆฌ๋ฐ ํจ์: ํ ๊ธ์์ฉ ์ถ๋ ฅ (ํ์ดํ ํจ๊ณผ)
|
| 5 |
+
def stream_text(text):
|
| 6 |
+
out = ""
|
| 7 |
+
for ch in text:
|
| 8 |
+
out += ch
|
| 9 |
+
time.sleep(0.05) # ๊ธ์๋น ์ง์ฐ (0.05์ด)
|
| 10 |
+
yield out
|
| 11 |
+
|
| 12 |
+
# --- ํ๋จ์ ์๋ณธ ๋ฌธ์ฅ์ HTML๋ก ์์ฑํ๋ ํจ์
|
| 13 |
+
# ํ์ฌ ์ปท ์ธ๋ฑ์ค(cut_index)๋ณด๋ค ์์ชฝ์ ํ์์ผ๋ก ํ์(์ด๋ฏธ ์๋ฆฐ ๋ถ๋ถ),
|
| 14 |
+
# ํ์ฌ ์ปท ์ธ๋ฑ์ค ๊ธ์์๋ โ๏ธ ์ด๋ชจํฐ์ฝ์ ๋ถ์ฌ ํ์ํ๊ณ , ๊ทธ ์ดํ์ ๊ธ์๋ ๊ธฐ๋ณธ ์คํ์ผ๋ก ํ์
|
| 15 |
+
def get_clickable_html(sentence, cut_index=0):
|
| 16 |
+
html = "<div style='font-size:24px; font-family: monospace;'>"
|
| 17 |
+
for i, ch in enumerate(sentence):
|
| 18 |
+
if i < cut_index:
|
| 19 |
+
# ์ด๋ฏธ ์๋ฆฐ ๋ถ๋ถ์ ํ์์ผ๋ก ํ์ํ๋ฉฐ ํด๋ฆญ ๋ถ๊ฐ
|
| 20 |
+
html += f"<span style='color:gray;'>{ch}</span>"
|
| 21 |
+
elif i == cut_index:
|
| 22 |
+
# ํ์ฌ ์ปท ์์น: ํ๋์๊ณผ ํจ๊ป โ๏ธ ์ด๋ชจํฐ์ฝ ํ์
|
| 23 |
+
html += f"<span style='cursor:pointer; color:blue;' onclick='handleClick({i})'>{ch}โ๏ธ</span>"
|
| 24 |
+
else:
|
| 25 |
+
# ์์ง ํด๋ฆญ ๊ฐ๋ฅํ ๋ถ๋ถ: ํด๋ฆญ ์ handleClick(i) ํธ์ถ
|
| 26 |
+
html += f"<span style='cursor:pointer;' onclick='handleClick({i})'>{ch}</span>"
|
| 27 |
+
html += "</div>"
|
| 28 |
+
return html
|
| 29 |
+
|
| 30 |
+
# --- HTML์ ์ฝ์
ํ ์๋ฐ์คํฌ๋ฆฝํธ (์จ๊ฒจ์ง Textbox์ Button์ ํ์ฉ)
|
| 31 |
+
custom_js = """
|
| 32 |
+
<script>
|
| 33 |
+
function handleClick(index) {
|
| 34 |
+
// ํด๋ฆญํ ๊ธ์์ ์ธ๋ฑ์ค๋ฅผ ์จ๊ฒจ์ง input์ ์ ์ฅํ ํ,
|
| 35 |
+
// ์จ๊ฒจ์ง ๋ฒํผ์ ๊ฐ์ ๋ก ํด๋ฆญํด Python ์ฝ๋ฐฑ์ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค.
|
| 36 |
+
document.getElementById("click_index").value = index;
|
| 37 |
+
document.getElementById("trigger_button").click();
|
| 38 |
+
}
|
| 39 |
+
</script>
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
# --- "Start" ๋ฒํผ ํด๋ฆญ ์:
|
| 43 |
+
# ์๋ณธ ๋ฌธ์ฅ๊ณผ ํ์ฌ ์ปท ์ธ๋ฑ์ค(0)๋ฅผ ์ํ๋ก ์ ์ฅํ๊ณ ,
|
| 44 |
+
# ์๋จ์๋ ์คํธ๋ฆฌ๋ฐ์ฉ ์ ๋๋ ์ดํฐ๋ฅผ, ํ๋จ์๋ HTML ํ์๋ฅผ ์์ฑํฉ๋๋ค.
|
| 45 |
+
def start_sentence(text):
|
| 46 |
+
current_cut = 0
|
| 47 |
+
clickable = get_clickable_html(text, current_cut) + custom_js
|
| 48 |
+
# ์๋จ ์คํธ๋ฆฌ๋ฐ์ ์ ์ฒด ๋ฌธ์ฅ์ ์์ฐจ์ ์ผ๋ก ์ถ๋ ฅ
|
| 49 |
+
return stream_text(text), text, current_cut, clickable
|
| 50 |
+
|
| 51 |
+
# --- "Restart Streaming" ๋ฒํผ ํด๋ฆญ ์:
|
| 52 |
+
# ํ์ฌ ์ํ(์๋ณธ ๋ฌธ์ฅ์ ํ์ฌ ์ปท ์ธ๋ฑ์ค)์์ ์๋ฆฐ ํ์ ๋ฌธ์ฅ์ ์คํธ๋ฆฌ๋ฐ์ผ๋ก ์ถ๋ ฅ
|
| 53 |
+
def stream_current(original_sentence, current_cut):
|
| 54 |
+
new_text = original_sentence[current_cut:]
|
| 55 |
+
return stream_text(new_text)
|
| 56 |
+
|
| 57 |
+
# --- HTML์ ํน์ ๊ธ์๋ฅผ ํด๋ฆญ(์จ์ ๋ฒํผ์ ํตํด)ํ๋ฉด ์คํ๋๋ ์ฝ๋ฐฑ ํจ์
|
| 58 |
+
# ํด๋ฆญํ ์ธ๋ฑ์ค(index)๊ฐ ํ์ฌ ์ปท ์ธ๋ฑ์ค๋ณด๋ค ํฌ๋ฉด(์ฆ, ์์ง ์๋ฆฌ์ง ์์ ๋ถ๋ถ์์ ํด๋ฆญ๋๋ฉด)
|
| 59 |
+
# ํ์ฌ ์ปท ์ธ๋ฑ์ค๋ฅผ ๊ฐฑ์ ํ๊ณ , ํ๋จ HTML ํ์๋ฅผ ์
๋ฐ์ดํธํฉ๋๋ค.
|
| 60 |
+
def update_cut(index, original_sentence, current_cut):
|
| 61 |
+
index = int(index)
|
| 62 |
+
new_cut = current_cut
|
| 63 |
+
if index > current_cut:
|
| 64 |
+
new_cut = index
|
| 65 |
+
clickable = get_clickable_html(original_sentence, new_cut) + custom_js
|
| 66 |
+
return original_sentence, new_cut, clickable
|
| 67 |
+
|
| 68 |
+
# --- Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 69 |
+
with gr.Blocks() as demo:
|
| 70 |
+
with gr.Row():
|
| 71 |
+
inp = gr.Textbox(label="Enter text", placeholder="๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์...")
|
| 72 |
+
start_btn = gr.Button("Start")
|
| 73 |
+
# ์๋จ ์คํธ๋ฆฌ๋ฐ ํ
์คํธ ์ถ๋ ฅ ์์ญ
|
| 74 |
+
stream_box = gr.Textbox(label="Streaming Text", interactive=False)
|
| 75 |
+
# ํ์ฌ ์ํ(์๋ฆฐ ํ์ ๋ฌธ์ฅ)๋ฅผ ์คํธ๋ฆฌ๋ฐํ๋ ๋ฒํผ
|
| 76 |
+
stream_btn = gr.Button("Restart Streaming")
|
| 77 |
+
# ํ๋จ ์๋ณธ ๋ฌธ์ฅ์ HTML๋ก ํ์ (๊ฐ ๊ธ์๊ฐ ํด๋ฆญ ๊ฐ๋ฅ)
|
| 78 |
+
clickable_display = gr.HTML(label="Clickable Text")
|
| 79 |
+
|
| 80 |
+
# ์๋ ๋ ์ปดํฌ๋ํธ๋ HTML ๋ด JavaScript์ ์ฐ๋ํ์ฌ ์ฌ์ฉ (์จ๊น)
|
| 81 |
+
click_index = gr.Textbox(visible=False, elem_id="click_index")
|
| 82 |
+
trigger_button = gr.Button("trigger", visible=False, elem_id="trigger_button")
|
| 83 |
+
|
| 84 |
+
# ์๋ณธ ๋ฌธ์ฅ๊ณผ ํ์ฌ ์ปท ์ธ๋ฑ์ค๋ฅผ ์ํ๋ก ์ ์ฅ
|
| 85 |
+
original_sentence_state = gr.State("")
|
| 86 |
+
current_cut_state = gr.State(0)
|
| 87 |
+
|
| 88 |
+
# โ "Start" ๋ฒํผ ํด๋ฆญ ์: ์
๋ ฅ ๋ฌธ์ฅ์ ์ํ์ ์ ์ฅํ๊ณ ์คํธ๋ฆฌ๋ฐ๊ณผ ํ๋จ HTML์ ์ด๊ธฐํ
|
| 89 |
+
start_btn.click(
|
| 90 |
+
start_sentence,
|
| 91 |
+
inputs=inp,
|
| 92 |
+
outputs=[stream_box, original_sentence_state, current_cut_state, clickable_display]
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# โก HTML ๋ด ๊ธ์ ํด๋ฆญ โ ์จ๊ฒจ์ง trigger_button ํด๋ฆญ ์: update_cut ํจ์ ์คํ
|
| 96 |
+
trigger_button.click(
|
| 97 |
+
update_cut,
|
| 98 |
+
inputs=[click_index, original_sentence_state, current_cut_state],
|
| 99 |
+
outputs=[original_sentence_state, current_cut_state, clickable_display]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# โข "Restart Streaming" ๋ฒํผ ํด๋ฆญ ์: ํ์ฌ ์ํ์ ๋ฌธ์ฅ(์๋ฆฐ ํ)์ ์คํธ๋ฆฌ๋ฐ
|
| 103 |
+
stream_btn.click(
|
| 104 |
+
stream_current,
|
| 105 |
+
inputs=[original_sentence_state, current_cut_state],
|
| 106 |
+
outputs=stream_box,
|
| 107 |
+
stream=True
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
demo.launch()
|