Spaces:
Sleeping
Sleeping
새 수정
Browse files- chatbot_utils.py +15 -17
- gradio_interface.py +18 -20
chatbot_utils.py
CHANGED
@@ -3,7 +3,7 @@ from openai import OpenAI
|
|
3 |
import json
|
4 |
from datetime import datetime
|
5 |
from scenario_handler import ScenarioHandler
|
6 |
-
import
|
7 |
|
8 |
client = OpenAI(api_key="sk-proj-3IEelWYK3Wl251k9qNriT3BlbkFJ9M7GpUGBijobUj1LETdu")
|
9 |
|
@@ -32,18 +32,6 @@ def chatbot_response(response, handler_type='offender', n=1):
|
|
32 |
choices = [choice.message.content for choice in api_response.choices]
|
33 |
return choices[0], choices
|
34 |
|
35 |
-
async def async_chatbot_response(response, handler_type='offender', n=1):
|
36 |
-
return chatbot_response(response, handler_type, n)
|
37 |
-
|
38 |
-
async def get_both_responses(user_input):
|
39 |
-
victim_task = asyncio.create_task(async_chatbot_response(user_input, 'victim', n=3))
|
40 |
-
offender_task = asyncio.create_task(async_chatbot_response(user_input, 'offender', n=1))
|
41 |
-
|
42 |
-
victim_response, victim_choices = await victim_task
|
43 |
-
offender_response, _ = await offender_task
|
44 |
-
|
45 |
-
return victim_response, victim_choices, offender_response
|
46 |
-
|
47 |
def save_history(history):
|
48 |
os.makedirs('logs', exist_ok=True)
|
49 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
@@ -52,12 +40,22 @@ def save_history(history):
|
|
52 |
json.dump(history, file, ensure_ascii=False, indent=4)
|
53 |
print(f"History saved to {filename}")
|
54 |
|
55 |
-
|
56 |
if user_input.strip().lower() == "종료":
|
57 |
save_history(chatbot_history)
|
58 |
return chatbot_history + [("종료", "실험에 참가해 주셔서 감사합니다. 후속 지시를 따라주세요")], []
|
59 |
|
60 |
-
|
61 |
-
new_history = chatbot_history + [(user_input,
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import json
|
4 |
from datetime import datetime
|
5 |
from scenario_handler import ScenarioHandler
|
6 |
+
import time
|
7 |
|
8 |
client = OpenAI(api_key="sk-proj-3IEelWYK3Wl251k9qNriT3BlbkFJ9M7GpUGBijobUj1LETdu")
|
9 |
|
|
|
32 |
choices = [choice.message.content for choice in api_response.choices]
|
33 |
return choices[0], choices
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
def save_history(history):
|
36 |
os.makedirs('logs', exist_ok=True)
|
37 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|
|
40 |
json.dump(history, file, ensure_ascii=False, indent=4)
|
41 |
print(f"History saved to {filename}")
|
42 |
|
43 |
+
def process_user_input(user_input, chatbot_history):
|
44 |
if user_input.strip().lower() == "종료":
|
45 |
save_history(chatbot_history)
|
46 |
return chatbot_history + [("종료", "실험에 참가해 주셔서 감사합니다. 후속 지시를 따라주세요")], []
|
47 |
|
48 |
+
# First, add the user's input to the history
|
49 |
+
new_history = chatbot_history + [(user_input, None)]
|
50 |
|
51 |
+
# Then, get the offender's response
|
52 |
+
offender_response, _ = chatbot_response(user_input, 'offender', n=1)
|
53 |
+
|
54 |
+
# Generate victim choices for the next turn
|
55 |
+
_, victim_choices = chatbot_response(offender_response, 'victim', n=3)
|
56 |
+
|
57 |
+
return new_history, offender_response, victim_choices
|
58 |
+
|
59 |
+
def delayed_offender_response(history, offender_response):
|
60 |
+
# This function will be called after a delay to add the offender's response
|
61 |
+
return history + [(None, offender_response)]
|
gradio_interface.py
CHANGED
@@ -1,41 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
from chatbot_utils import process_user_input,
|
3 |
-
import asyncio
|
4 |
|
5 |
def create_interface():
|
6 |
-
|
7 |
input_text = user_input if user_input else selected_response
|
|
|
8 |
|
9 |
if input_text.strip().lower() == "종료":
|
10 |
-
new_history = chatbot_history + [(input_text, "실험에 참가해 주셔서 감사합니다. 후속 지시를 따라주세요")]
|
11 |
return new_history, gr.update(choices=[], interactive=False)
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
|
16 |
-
|
17 |
initial_message = "발표가 망한 건 제 잘못도 좀 있지만, 팀장님은 아무것도 안 하면서 이러는 건 선 넘은거죠"
|
|
|
|
|
|
|
18 |
|
19 |
-
|
|
|
20 |
|
21 |
-
chatbot_history = [(initial_message, victim_response), (None, offender_response)]
|
22 |
return chatbot_history, gr.update(choices=victim_choices)
|
23 |
|
24 |
with gr.Blocks() as demo:
|
25 |
-
case_selection_button = gr.Button("발표가 망한 건 제 잘못도 좀 있지만, 팀장님은 아무것도 안 하면서 이러는 건 선 넘은거죠")
|
26 |
-
|
27 |
screen = gr.Chatbot()
|
28 |
-
user_input = gr.Textbox(label="
|
29 |
-
response_choices = gr.Dropdown(label="
|
30 |
-
submit_button = gr.Button(value="
|
31 |
|
|
|
32 |
case_selection_button.click(handle_case_selection, inputs=[], outputs=[screen, response_choices])
|
33 |
|
34 |
submit_button.click(handle_user_response, inputs=[user_input, response_choices, screen], outputs=[screen, response_choices])
|
35 |
|
36 |
-
return demo
|
37 |
-
|
38 |
-
# Gradio의 launch 함수를 사용하여 인터페이스를 실행합니다.
|
39 |
-
if __name__ == "__main__":
|
40 |
-
demo = create_interface()
|
41 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from chatbot_utils import process_user_input, chatbot_response, delayed_offender_response
|
|
|
3 |
|
4 |
def create_interface():
|
5 |
+
def handle_user_response(user_input, selected_response, chatbot_history):
|
6 |
input_text = user_input if user_input else selected_response
|
7 |
+
new_history, offender_response, choices = process_user_input(input_text, chatbot_history)
|
8 |
|
9 |
if input_text.strip().lower() == "종료":
|
|
|
10 |
return new_history, gr.update(choices=[], interactive=False)
|
11 |
|
12 |
+
# Schedule the offender's response to be added after a delay
|
13 |
+
gr.Result(delayed_offender_response, [new_history, offender_response], _js="() => setTimeout(() => _js_return_(), 2000)")
|
14 |
+
|
15 |
+
return new_history, gr.update(choices=choices)
|
16 |
|
17 |
+
def handle_case_selection():
|
18 |
initial_message = "발표가 망한 건 제 잘못도 좀 있지만, 팀장님은 아무것도 안 하면서 이러는 건 선 넘은거죠"
|
19 |
+
chatbot_history = [(initial_message, None)]
|
20 |
+
offender_response, _ = chatbot_response(initial_message, 'offender', n=1)
|
21 |
+
_, victim_choices = chatbot_response(offender_response, 'victim', n=3)
|
22 |
|
23 |
+
# Schedule the offender's response to be added after a delay
|
24 |
+
gr.Result(delayed_offender_response, [chatbot_history, offender_response], _js="() => setTimeout(() => _js_return_(), 2000)")
|
25 |
|
|
|
26 |
return chatbot_history, gr.update(choices=victim_choices)
|
27 |
|
28 |
with gr.Blocks() as demo:
|
|
|
|
|
29 |
screen = gr.Chatbot()
|
30 |
+
user_input = gr.Textbox(label="Your input")
|
31 |
+
response_choices = gr.Dropdown(label="Select a Response", choices=[], interactive=True)
|
32 |
+
submit_button = gr.Button(value="Submit")
|
33 |
|
34 |
+
case_selection_button = gr.Button("발표가 망한 건 제 잘못도 좀 있지만, 팀장님은 아무것도 안 하면서 이러는 건 선 넘은거죠")
|
35 |
case_selection_button.click(handle_case_selection, inputs=[], outputs=[screen, response_choices])
|
36 |
|
37 |
submit_button.click(handle_user_response, inputs=[user_input, response_choices, screen], outputs=[screen, response_choices])
|
38 |
|
39 |
+
return demo
|
|
|
|
|
|
|
|
|
|