gschatbot_1 / gradio_interface.py
songhune's picture
μ• μ΄ˆμ— ν•¨μˆ˜ μžμ²΄κ°€ λ™κΈ°μ μœΌλ‘œ λ˜μ–΄μžˆμœΌλ‹ˆκΉŒ...
72a7b9b
raw
history blame
2.04 kB
import gradio as gr
from chatbot_utils import process_user_input, get_both_responses
import asyncio
def create_interface():
async 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() == "μ’…λ£Œ":
new_history = chatbot_history + [(input_text, "μ‹€ν—˜μ— μ°Έκ°€ν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. 후속 μ§€μ‹œλ₯Ό λ”°λΌμ£Όμ„Έμš”")]
return new_history, gr.update(choices=[], interactive=False)
new_history, victim_choices = await process_user_input(input_text, chatbot_history)
return new_history, gr.update(choices=victim_choices)
async def handle_case_selection():
initial_message = "λ°œν‘œκ°€ λ§ν•œ 건 제 잘λͺ»λ„ μ’€ μžˆμ§€λ§Œ, νŒ€μž₯λ‹˜μ€ 아무것도 μ•ˆ ν•˜λ©΄μ„œ μ΄λŸ¬λŠ” 건 μ„  λ„˜μ€κ±°μ£ "
victim_response, victim_choices, offender_response = await get_both_responses(initial_message)
chatbot_history = [(initial_message, victim_response), (None, offender_response)]
return 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
# Gradio의 launch ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€.
if __name__ == "__main__":
demo = create_interface()
demo.launch()