harheem commited on
Commit
97b815c
โ€ข
1 Parent(s): 824e199

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +83 -0
  2. 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