KorLearnGame / app.py
harheem's picture
Upload 2 files
97b815c
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()