File size: 2,421 Bytes
5dfcc08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)  # 2-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)  # 2-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