Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +83 -0
- requirements.txt +7 -0
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import random
|
| 4 |
+
import re
|
| 5 |
+
from langchain.chat_models import ChatOpenAI
|
| 6 |
+
from langchain.schema import AIMessage, HumanMessage, SystemMessage
|
| 7 |
+
import os
|
| 8 |
+
|
| 9 |
+
korean_word_data = pd.read_csv("korean_word_data.csv")
|
| 10 |
+
llm = ChatOpenAI(temperature=1.0, model="gpt-3.5-turbo")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def response(message, history):
|
| 14 |
+
levels = ["์ด๊ธ", "์ค๊ธ", "์๊ธ"]
|
| 15 |
+
print(history)
|
| 16 |
+
|
| 17 |
+
# ๋์ด๋ ์ ํ ๋ฉ์์ง์ธ ๊ฒฝ์ฐ
|
| 18 |
+
if message in levels:
|
| 19 |
+
level_int = levels.index(message) + 1
|
| 20 |
+
this_level_word = korean_word_data[korean_word_data["level"] == level_int]
|
| 21 |
+
word_info = this_level_word.iloc[random.randint(0, len(this_level_word))]
|
| 22 |
+
description = word_info["description"]
|
| 23 |
+
word_type = word_info["type"]
|
| 24 |
+
return f"""๐น๏ธ {message} ๋์ด๋๋ก ๊ฒ์์ ์์ํ ๊ฒ์ ๐น๏ธ
|
| 25 |
+
|
| 26 |
+
์ค๋ช
ํ๋ ์ด ๋จ์ด๋ฅผ ๋ง์ถฐ๋ณด์ธ์.
|
| 27 |
+
<{word_type}>[{description}]"""
|
| 28 |
+
|
| 29 |
+
# ์ด์ ๋ํ ํ์ธ
|
| 30 |
+
for user, bot in reversed(history):
|
| 31 |
+
if "๋์ด๋๋ก ๊ฒ์์ ์์ํ ๊ฒ์ ๐น๏ธ" in bot:
|
| 32 |
+
description_pattern = r"\[([^\]]*)\]"
|
| 33 |
+
word_info = korean_word_data[
|
| 34 |
+
korean_word_data["description"]
|
| 35 |
+
== re.search(description_pattern, bot).group(1)
|
| 36 |
+
].iloc[0]
|
| 37 |
+
level, word_type, word, description, example, word_len, choseong = (
|
| 38 |
+
word_info["level"],
|
| 39 |
+
word_info["type"],
|
| 40 |
+
word_info["word"],
|
| 41 |
+
word_info["description"],
|
| 42 |
+
word_info["example"],
|
| 43 |
+
word_info["word_len"],
|
| 44 |
+
word_info["choseong"],
|
| 45 |
+
)
|
| 46 |
+
prompt = f"""You are a chatbot conducting a Korean vocabulary matching game. The current word's information is as follows:
|
| 47 |
+
Correct Word: {word}, Difficulty: {levels[level-1]}, Part of Speech: {word_type}, Meaning: [{description}], Example Sentence: [{example}], Word Length: {word_len}, Consonant: {choseong}
|
| 48 |
+
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.
|
| 49 |
+
If you think the content of the message is not related to the vocabulary matching game, respond appropriately to the situation.
|
| 50 |
+
If the user seems to be struggling, inform them that there are hints available.
|
| 51 |
+
Provide hints in this order: Example Sentence -> Word Length -> Consonant, and give only one hint at a time.
|
| 52 |
+
|
| 53 |
+
What you must follow:
|
| 54 |
+
1. Never say the correct word during a conversation.
|
| 55 |
+
2. Do not give hints unless there is a direct request for them from the user.
|
| 56 |
+
3. If the user answers correctly, ask them to enter the new difficulty level.
|
| 57 |
+
4. Give all answers in Korean."""
|
| 58 |
+
|
| 59 |
+
history_langchain_format = []
|
| 60 |
+
history_langchain_format.append(SystemMessage(content=prompt))
|
| 61 |
+
for human, ai in history:
|
| 62 |
+
history_langchain_format.append(HumanMessage(content=human))
|
| 63 |
+
history_langchain_format.append(AIMessage(content=ai))
|
| 64 |
+
history_langchain_format.append(HumanMessage(content=message))
|
| 65 |
+
|
| 66 |
+
gpt_response = llm(history_langchain_format)
|
| 67 |
+
return gpt_response.content
|
| 68 |
+
|
| 69 |
+
# ๊ฒ์ ์์ ๋ฉ์์ง๊ฐ ์๋ ๊ฒฝ์ฐ
|
| 70 |
+
return "์ด๊ธ, ์ค๊ธ, ์๊ธ ์ค ๋ง์ถ ๋จ์ด์ ๋์ด๋๋ฅผ ๊ณจ๋ผ์ฃผ์ธ์. ๐"
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
gr.ChatInterface(
|
| 74 |
+
fn=response,
|
| 75 |
+
textbox=gr.Textbox(container=False, scale=10),
|
| 76 |
+
title="๐ฎ ํ๊ตญ์ด ์ดํ ๋ง์ถ๊ธฐ ๊ฒ์: Korean Learning Game ๐ฎ",
|
| 77 |
+
description="์๋
ํ์ธ์! ์ด ๊ฒ์์ ๋์ด๋๋ ์ด๊ธ, ์ค๊ธ, ์๊ธ์ผ๋ก ์ฌ์ฉ์๊ฐ ์์ ๋กญ๊ฒ ๋์ด๋๋ฅผ ์ ํํ ์ ์์ต๋๋ค. ์์๋ฅผ ์ฐธ๊ณ ํ์ฌ ๋์ด๋๋ฅผ ์ ํํ๊ฒ ์
๋ ฅํด์ฃผ์ธ์. ๋์ด๋๋ฅผ ์
๋ ฅํ๋ฉด ๊ฒ์์ด ์์๋ฉ๋๋ค.",
|
| 78 |
+
theme="soft",
|
| 79 |
+
examples=[["์ด๊ธ"], ["์ค๊ธ"], ["์๊ธ"]],
|
| 80 |
+
retry_btn="๋ค์๋ณด๋ด๊ธฐ โฉ",
|
| 81 |
+
undo_btn="์ด์ ๋ํ ์ง์ฐ๊ธฐ",
|
| 82 |
+
clear_btn="์ ์ฒด ๋ํ ์ง์ฐ๊ธฐ",
|
| 83 |
+
).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas==2.1.2
|
| 2 |
+
numpy==1.26.1
|
| 3 |
+
gradio==4.1.2
|
| 4 |
+
gradio_client==0.7.0
|
| 5 |
+
langchain==0.0.333
|
| 6 |
+
async-timeout==4.0.3
|
| 7 |
+
openai==1.2.2
|