File size: 3,799 Bytes
c3a3ddc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
01bf8c9
c3a3ddc
 
01bf8c9
c3a3ddc
 
 
 
 
01bf8c9
c3a3ddc
 
 
 
 
 
 
 
 
 
01bf8c9
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import gradio as gr

from chat_quest import sample_conversation, sample_conversation_christmas, generate_chat_response, generate_last_response, ConversationSituation, N_CHOICES


def update_chat(choice_index, choices, msg_history):
    history = msg_history or []

    print(choice_index, choices)
    user_msg = choices[choice_index]

    history.append((user_msg, None))

    return [history, history] + [gr.update(interactive=False) for _ in range(N_CHOICES)]


def process_last_msg(msg_history, conversation_situation: ConversationSituation):
    history = msg_history or []

    dialogue = _format_dialogue(history, conversation_situation.character)

    dialogue_length = len(history)

    print(dialogue_length)

    if dialogue_length > 6:
        bot_response = generate_last_response(situation=conversation_situation, dialogue=dialogue)
        choices_update = [gr.update(visible=False) for _ in conversation_situation.starting_choices]
        won_update = [gr.update(visible=bot_response.has_won)]
        lost_update = [gr.update(visible=not bot_response.has_won)]
    else:
        bot_response = generate_chat_response(situation=conversation_situation, dialogue=dialogue)
        choices_update = [gr.update(value=c, interactive=True) for c in bot_response.choices]
        won_update = [gr.update(visible=bot_response.has_won)]
        lost_update = [gr.update(visible=False)]

    history.append((None, bot_response.message))
    chat_update = [bot_response.has_won, bot_response.choices, history, history]

    return chat_update + choices_update + won_update + lost_update


def clear_text_box():
    return gr.Textbox.update(value='')


def format_goal_messsage(goal: str) -> str:
    return f'######   **Objetivo:** {goal}'


def format_chat_header(name: str) -> str:
    return f'**{name}**<br>Active now'
        
    
def retry_level(current_situation: ConversationSituation):
    choices_update = [gr.update(value=c, interactive=True, visible=True) for c in current_situation.starting_choices]
    outcome_update = gr.update(visible=False)
    chat_history_update = [(None, current_situation.first_message)]

    return choices_update + [current_situation.starting_choices, outcome_update, chat_history_update, chat_history_update]


def load_next_level():
    new_situation = sample_conversation_christmas
    choices_update = [gr.update(value=c, interactive=True, visible=True) for c in new_situation.starting_choices]
    outcome_update = gr.update(visible=False)
    chat_history_update = [(None, new_situation.first_message)]

    opponent_name = format_chat_header(new_situation.character)
    goal_message = format_goal_messsage(new_situation.goal_message)
    
    return choices_update + [new_situation, new_situation.starting_choices, outcome_update, chat_history_update, chat_history_update] + [new_situation.character_emoji, opponent_name, goal_message, False]


def process_key(openai_key: str):
    import os
    result = openai_key

    if openai_key.lower() == os.environ.get('USER'):
        result = os.environ.get('PASSWORD')

    return result


def validate_openai_key(password: str):
    return password.startswith("sk-")


def validate_auth(user: str, pw: str) -> bool:
    import openai

    processed_pw = process_key(pw)
    is_valid_login = validate_openai_key(processed_pw)

    if is_valid_login:
        openai.api_key = processed_pw

    return is_valid_login


def _format_dialogue(dialogue_list, opponent):
    formatted_dialogue = []
    for entry in dialogue_list:
        player_line, opponent_line = entry
        if opponent_line:
            formatted_dialogue.append(f"{opponent}: {opponent_line}")
        if player_line:
            formatted_dialogue.append(f"Player: {player_line}")
    return '\n'.join(formatted_dialogue)