GodSaveMoney / core /launch_gradio.py
Jeong-hun Kim
debug fix
54c8c61
raw
history blame
2.55 kB
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