import gradio as gr import pandas as pd import random import re from langchain.chat_models import ChatOpenAI from langchain.schema import AIMessage, HumanMessage, SystemMessage import os korean_word_data = pd.read_csv("korean_word_data.csv") llm = ChatOpenAI(temperature=1.0, model="gpt-3.5-turbo") def response(message, history): levels = ["초급", "중급", "상급"] print(history) # 난이도 선택 메시지인 경우 if message in levels: level_int = levels.index(message) + 1 this_level_word = korean_word_data[korean_word_data["level"] == level_int] word_info = this_level_word.iloc[random.randint(0, len(this_level_word))] description = word_info["description"] word_type = word_info["type"] return f"""🕹️ {message} 난이도로 게임을 시작할게요 🕹️ 설명하는 이 단어를 맞춰보세요. <{word_type}>[{description}]""" # 이전 대화 확인 for user, bot in reversed(history): if "난이도로 게임을 시작할게요 🕹️" in bot: description_pattern = r"\[([^\]]*)\]" word_info = korean_word_data[ korean_word_data["description"] == re.search(description_pattern, bot).group(1) ].iloc[0] level, word_type, word, description, example, word_len, choseong = ( word_info["level"], word_info["type"], word_info["word"], word_info["description"], word_info["example"], word_info["word_len"], word_info["choseong"], ) prompt = f"""You are a chatbot conducting a Korean vocabulary matching game. The current word's information is as follows: Correct Word: {word}, Difficulty: {levels[level-1]}, Part of Speech: {word_type}, Meaning: [{description}], Example Sentence: [{example}], Word Length: {word_len}, Consonant: {choseong} The user's input message is [{message}]. If the user is playing the Korean vocabulary matching game, compare the correct word with the message. If they are similar, respond with an expression of near-miss. If not similar, send words of encouragement. If you think the content of the message is not related to the vocabulary matching game, respond appropriately to the situation. If the user seems to be struggling, inform them that there are hints available. Provide hints in this order: Example Sentence -> Word Length -> Consonant, and give only one hint at a time. What you must follow: 1. Never say the correct word during a conversation. 2. Do not give hints unless there is a direct request for them from the user. 3. If the user answers correctly, ask them to enter the new difficulty level. 4. Give all answers in Korean.""" history_langchain_format = [] history_langchain_format.append(SystemMessage(content=prompt)) for human, ai in history: history_langchain_format.append(HumanMessage(content=human)) history_langchain_format.append(AIMessage(content=ai)) history_langchain_format.append(HumanMessage(content=message)) gpt_response = llm(history_langchain_format) return gpt_response.content # 게임 시작 메시지가 없는 경우 return "초급, 중급, 상급 중 맞출 단어의 난이도를 골라주세요. 😎" gr.ChatInterface( fn=response, textbox=gr.Textbox(container=False, scale=10), title="🎮 한국어 어휘 맞추기 게임: Korean Learning Game 🎮", description="안녕하세요! 이 게임의 난이도는 초급, 중급, 상급으로 사용자가 자유롭게 난이도를 선택할 수 있습니다. 예시를 참고하여 난이도를 정확하게 입력해주세요. 난이도를 입력하면 게임이 시작됩니다.", theme="soft", examples=[["초급"], ["중급"], ["상급"]], retry_btn="다시보내기 ↩", undo_btn="이전 대화 지우기", clear_btn="전체 대화 지우기", ).launch()