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}**
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)