Spaces:
Sleeping
Sleeping
import gradio as gr | |
from core.context_manager import ContextManager | |
from core.make_pipeline import MakePipeline | |
from core.make_reply import generate_reply | |
def create_interface(ctx: ContextManager, makePipeline: MakePipeline): | |
with gr.Blocks(css=""" | |
.chat-box { max-height: 500px; overflow-y: auto; padding: 10px; border: 1px solid #ccc; border-radius: 10px; } | |
.bubble-left { background-color: #f1f0f0; border-radius: 10px; padding: 10px; margin: 5px; max-width: 70%; float: left; clear: both; } | |
.bubble-right { background-color: #d1e7ff; border-radius: 10px; padding: 10px; margin: 5px; max-width: 70%; float: right; clear: both; text-align: right; } | |
.reset-btn-container { text-align: right; margin-bottom: 10px; } | |
""") as demo: | |
gr.Markdown("### νμ§λ‘μ λννκΈ°") | |
with gr.Column(): | |
with gr.Row(): | |
gr.Markdown("") | |
reset_btn = gr.Button("π λν μ΄κΈ°ν", elem_classes="reset-btn-container", scale=1) | |
chat_output = gr.HTML(elem_id="chat-box") | |
user_input = gr.Textbox(label="λ©μμ§ μ λ ₯", placeholder="νμ§λ‘μκ² λ§μ κ±Έμ΄λ³΄μΈμ") | |
state = gr.State(ctx) | |
# history μ½μ΄μ νλ©΄μ λΏλ¦¬λ μν | |
def render_chat(ctx: ContextManager): | |
html = "" | |
for item in ctx.getHistory(): | |
if item["role"] == "user": | |
html += f"<div class='bubble-right'>{item['text']}</div>" | |
elif item["role"] == "bot": | |
html += f"<div class='bubble-left'>{item['text']}</div>" | |
return gr.update(value=html) | |
def on_submit(user_msg: str, ctx: ContextManager): | |
# μ¬μ©μ μ λ ₯ historyμ μΆκ° | |
ctx.addHistory("user", user_msg) | |
# μ¬μ©μ μ λ ₯μ ν¬ν¨ν μ±ν μ°μ λ λλ§ | |
html = render_chat(ctx) | |
yield html, "", ctx | |
# λ΄ μλ΅ μμ± | |
generate_reply(ctx, makePipeline, user_msg) | |
# μλ΅μ ν¬ν¨ν μ 체 history κΈ°λ° λ λλ§ | |
html = render_chat(ctx) | |
yield html, "", ctx | |
# history μ΄κΈ°ν | |
def reset_chat(): | |
ctx.resetHistory() | |
return gr.update(value=""), "", ctx.getHistory() | |
user_input.submit(on_submit, inputs=[user_input, state], outputs=[chat_output, user_input, state], queue=True) | |
reset_btn.click(reset_chat, inputs=None, outputs=[chat_output, user_input, state]) | |
return demo |