Spaces:
Runtime error
Runtime error
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() | |