gschatbot_2 / gradio_interface.py
songhune's picture
case2
34c5c8e
import gradio as gr
from chatbot_utils import process_user_input, chatbot_response
import time
def create_interface():
def handle_user_response(user_input, selected_response, chatbot_history):
input_text = user_input if user_input else selected_response
if input_text.strip().lower() == "μ’…λ£Œ":
chatbot_history.append((input_text, "μ‹€ν—˜μ— μ°Έκ°€ν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. 후속 μ§€μ‹œλ₯Ό λ”°λΌμ£Όμ„Έμš”"))
return chatbot_history, gr.update(choices=[], interactive=False)
# Add user's input to history
chatbot_history.append((input_text, None))
yield chatbot_history, gr.update(choices=[]) # Immediately show user input
# Get offender's response
offender_response, _ = chatbot_response(input_text, 'offender', n=1)
time.sleep(1) # 1-second delay
# Add offender's response to history
chatbot_history.append((None, offender_response))
# Generate victim choices for the next turn
_, victim_choices = chatbot_response(offender_response, 'victim', n=3)
yield chatbot_history, gr.update(choices=victim_choices)
def handle_case_selection():
initial_message = "이번 여름에 경주에 같이 κ°€λŠ” 게 μ–΄λ–„? μ˜ˆμ „μ— ν•œ 번 가보고 정말 μ’‹μ•˜λŠ”λ°, μ΄λ²ˆμ— λ‹€μ‹œ κ°€κ³  μ‹Άμ–΄."
chatbot_history = [(initial_message, None)]
yield chatbot_history, gr.update(choices=[])
offender_response, _ = chatbot_response(initial_message, 'offender', n=1)
time.sleep(1) # 1-second delay
chatbot_history.append((None, offender_response))
_, victim_choices = chatbot_response(offender_response, 'victim', n=3)
yield chatbot_history, gr.update(choices=victim_choices)
with gr.Blocks() as demo:
case_selection_button = gr.Button("이번 여름에 경주에 같이 κ°€λŠ” 게 μ–΄λ–„? μ˜ˆμ „μ— ν•œ 번 가보고 정말 μ’‹μ•˜λŠ”λ°, μ΄λ²ˆμ— λ‹€μ‹œ κ°€κ³  μ‹Άμ–΄.")
screen = gr.Chatbot()
user_input = gr.Textbox(label="이곳에 λŒ€λ‹΅μ„ μž…λ ₯ν•˜μ„Έμš”")
response_choices = gr.Dropdown(label="λ˜λŠ” λŒ€λ‹΅μ„ 선택해 μ£Όμ„Έμš”", choices=[], interactive=True)
submit_button = gr.Button(value="제좜")
case_selection_button.click(handle_case_selection, inputs=[], outputs=[screen, response_choices])
submit_button.click(handle_user_response, inputs=[user_input, response_choices, screen], outputs=[screen, response_choices])
return demo