Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import torch
|
| 3 |
-
from flask import Flask, request, Response
|
| 4 |
from transformers import AutoTokenizer, GPT2LMHeadModel
|
| 5 |
from dicttoxml import dicttoxml
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
|
@@ -13,26 +15,22 @@ print("๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
|
| 13 |
model = GPT2LMHeadModel.from_pretrained("EleutherAI/polyglot-ko-1.3b", trust_remote_code=True)
|
| 14 |
print("๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 15 |
|
| 16 |
-
# 2. ๋ฐ์ดํฐ์
๋ก๋ (
|
| 17 |
try:
|
| 18 |
df = pd.read_excel('dataset.xlsx')
|
| 19 |
-
knowledge_list = df['๋ฐ์ดํฐ์
์ ๋ฃ์ ๋ด์ฉ(*)'].tolist()
|
| 20 |
except Exception as e:
|
| 21 |
print(f"๋ฐ์ดํฐ์
๋ก๋ ์๋ฌ: {e}")
|
| 22 |
knowledge_list = []
|
| 23 |
|
| 24 |
def find_relevant_context(query, top_n=2):
|
| 25 |
-
"""
|
| 26 |
-
# ํค์๋ ๊ธฐ๋ฐ ํํฐ๋ง - ๊ณต๋ฐฑ ์ ๊ฑฐํด์ ๊ฒ์
|
| 27 |
query_words = query.replace(" ", "").lower()
|
| 28 |
relevant_sentences = []
|
| 29 |
-
|
| 30 |
for s in knowledge_list:
|
| 31 |
s_text = str(s).replace(" ", "").replace("\n", "").lower()
|
| 32 |
-
# ํค์๋๊ฐ ๋ฌธ์ฅ์ ํฌํจ๋์ด ์๋์ง ํ์ธ
|
| 33 |
if any(word.replace(" ", "") in s_text for word in query.split()):
|
| 34 |
relevant_sentences.append(s)
|
| 35 |
-
|
| 36 |
if relevant_sentences:
|
| 37 |
return " ".join(str(s) for s in relevant_sentences[:top_n])
|
| 38 |
return ""
|
|
@@ -40,18 +38,13 @@ def find_relevant_context(query, top_n=2):
|
|
| 40 |
def ask_sayknow(query):
|
| 41 |
try:
|
| 42 |
context = find_relevant_context(query)
|
| 43 |
-
|
| 44 |
-
# ์๊ธฐ์๊ฐ/์ธ์ฌ ์ง๋ฌธ์ ๋ํ ๋ช
์์ ์๋ด ์ถ๊ฐ
|
| 45 |
-
# ์ญํ , ์์, ์ ๋ณด/์ง๋ฌธ ๊ตฌ๋ถ, context ์์ ๋ '์ ๋ณด ์์' ๋ช
์
|
| 46 |
persona_guide = (
|
| 47 |
-
"๋๋ ์ง์ ๊ธฐ๋ฐ ํ๊ตญ์ด ์ฑ๋ด Sayknow์ผ. ์๊ธฐ์๊ฐ(์ด๋ฆ, ์ ์ฒด, ์ธ์ฌ ๋ฑ)
|
| 48 |
-
"๊ทธ
|
| 49 |
"์์: Q: ๋ถ์์ ๋ง์
์ด ๋ญ์ผ?\nA: ๋ถ๋ชจ๊ฐ ๊ฐ์ ๋ ๋ถ์๋ผ๋ฆฌ ๋ํ๋ฉด ๋ฉ๋๋ค.\n"
|
| 50 |
)
|
| 51 |
info = context if context else "์ ๋ณด ์์"
|
| 52 |
-
prompt =
|
| 53 |
-
f"{persona_guide}---\n[์ ๋ณด]\n{info}\n[์ง๋ฌธ]\n{query}\n[๋ต๋ณ] "
|
| 54 |
-
)
|
| 55 |
|
| 56 |
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
| 57 |
model.eval()
|
|
@@ -69,30 +62,21 @@ def ask_sayknow(query):
|
|
| 69 |
num_beams=1
|
| 70 |
)
|
| 71 |
response = tokenizer.decode(gen_ids[0], skip_special_tokens=True)
|
| 72 |
-
#
|
| 73 |
-
if "๋ต๋ณ:" in response
|
| 74 |
-
answer = response.split("๋ต๋ณ:")[-1].strip()
|
| 75 |
-
else:
|
| 76 |
-
answer = response.strip()
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
import re
|
| 80 |
-
# ํ๊ธ, ์ซ์, ๊ธฐ๋ณธ ๊ตฌ๋์ ๋ง ํ์ฉ
|
| 81 |
answer = re.sub(r"[^๊ฐ-ํฃ0-9 .,!?~\n]", "", answer)
|
| 82 |
-
# ๋ฐ๋ณต๋๋ ํน์๋ฌธ์, ์ซ์, ์๋ฌธ ์ ๊ฑฐ
|
| 83 |
answer = re.sub(r"([.,!?~])\1{2,}", r"\1", answer)
|
| 84 |
answer = re.sub(r"[a-zA-Z]+", "", answer)
|
| 85 |
-
# ์์(=, ^, *, / ๋ฑ) ์ ๊ฑฐ
|
| 86 |
answer = re.sub(r"[=^*/\\]+", "", answer)
|
| 87 |
-
# ์ฐ์ ๊ณต๋ฐฑ ์ ๋ฆฌ
|
| 88 |
answer = re.sub(r"\s+", " ", answer).strip()
|
| 89 |
|
| 90 |
-
#
|
| 91 |
def truncate_korean(text, max_len=80):
|
| 92 |
count = 0
|
| 93 |
result = ""
|
| 94 |
for ch in text:
|
| 95 |
-
# ํ๊ธ, ํ์, ์๋ฌธ, ์ซ์, ๊ตฌ๋์ ๋ชจ๋ 1์๋ก ์ทจ๊ธ
|
| 96 |
result += ch
|
| 97 |
count += 1
|
| 98 |
if count >= max_len:
|
|
@@ -100,23 +84,20 @@ def ask_sayknow(query):
|
|
| 100 |
return result
|
| 101 |
answer = truncate_korean(answer, 80)
|
| 102 |
|
| 103 |
-
#
|
| 104 |
if answer and answer[-1] not in ".!?":
|
| 105 |
answer += "."
|
| 106 |
return answer
|
| 107 |
except Exception as e:
|
| 108 |
print(f"ask_sayknow ์๋ฌ: {e}")
|
| 109 |
-
import traceback
|
| 110 |
traceback.print_exc()
|
| 111 |
return f"์ค๋ฅ: {str(e)}"
|
| 112 |
|
| 113 |
-
# 3.
|
| 114 |
@app.route('/chatapi.html', methods=['GET'])
|
| 115 |
@app.route('/index.html', methods=['GET'])
|
| 116 |
-
@app.route('/', methods=['GET'])
|
| 117 |
def chat_api():
|
| 118 |
query = request.args.get('askdata', '')
|
| 119 |
-
|
| 120 |
if not query:
|
| 121 |
result = {"status": "error", "message": "No data"}
|
| 122 |
else:
|
|
@@ -129,7 +110,6 @@ def chat_api():
|
|
| 129 |
}
|
| 130 |
except Exception as e:
|
| 131 |
print(f"chat_api ์๋ฌ: {e}")
|
| 132 |
-
import traceback
|
| 133 |
traceback.print_exc()
|
| 134 |
result = {
|
| 135 |
"service": "Sayknow",
|
|
@@ -138,9 +118,37 @@ def chat_api():
|
|
| 138 |
"error": str(e)
|
| 139 |
}
|
| 140 |
|
| 141 |
-
# XML ๋ณํ
|
| 142 |
xml_output = dicttoxml(result, custom_root='SayknowAPI', attr_type=False)
|
| 143 |
return Response(xml_output, mimetype='text/xml')
|
| 144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
if __name__ == '__main__':
|
| 146 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
import torch
|
| 3 |
+
from flask import Flask, request, Response, render_template_string
|
| 4 |
from transformers import AutoTokenizer, GPT2LMHeadModel
|
| 5 |
from dicttoxml import dicttoxml
|
| 6 |
+
import re
|
| 7 |
+
import traceback
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
|
|
|
| 15 |
model = GPT2LMHeadModel.from_pretrained("EleutherAI/polyglot-ko-1.3b", trust_remote_code=True)
|
| 16 |
print("๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 17 |
|
| 18 |
+
# 2. ๋ฐ์ดํฐ์
๋ก๋ (์์
ํ ์ปฌ๋ผ์ ์ง์ ๋ฐ์ดํฐ ์์ ๋)
|
| 19 |
try:
|
| 20 |
df = pd.read_excel('dataset.xlsx')
|
| 21 |
+
knowledge_list = df['๋ฐ์ดํฐ์
์ ๋ฃ์ ๋ด์ฉ(*)'].tolist() # ์ปฌ๋ผ๋ช
์ ํํ ๋ง์ถฐ์ผ ํด!
|
| 22 |
except Exception as e:
|
| 23 |
print(f"๋ฐ์ดํฐ์
๋ก๋ ์๋ฌ: {e}")
|
| 24 |
knowledge_list = []
|
| 25 |
|
| 26 |
def find_relevant_context(query, top_n=2):
|
| 27 |
+
"""์ง๋ฌธ๊ณผ ๊ด๋ จ๋ ์ง์๋ฐ์ดํฐ ๋ฌธ์ฅ ์ต๋ top_n๊ฐ ์ฐพ์์ ๋ฐํ"""
|
|
|
|
| 28 |
query_words = query.replace(" ", "").lower()
|
| 29 |
relevant_sentences = []
|
|
|
|
| 30 |
for s in knowledge_list:
|
| 31 |
s_text = str(s).replace(" ", "").replace("\n", "").lower()
|
|
|
|
| 32 |
if any(word.replace(" ", "") in s_text for word in query.split()):
|
| 33 |
relevant_sentences.append(s)
|
|
|
|
| 34 |
if relevant_sentences:
|
| 35 |
return " ".join(str(s) for s in relevant_sentences[:top_n])
|
| 36 |
return ""
|
|
|
|
| 38 |
def ask_sayknow(query):
|
| 39 |
try:
|
| 40 |
context = find_relevant_context(query)
|
|
|
|
|
|
|
|
|
|
| 41 |
persona_guide = (
|
| 42 |
+
"๋๋ ์ง์ ๊ธฐ๋ฐ ํ๊ตญ์ด ์ฑ๋ด Sayknow์ผ. ์๊ธฐ์๊ฐ(์ด๋ฆ, ์ ์ฒด, ์ธ์ฌ ๋ฑ) ์ง๋ฌธ์ '์ ๋ Sayknow์
๋๋ค.'๋ผ๊ณ ๋ตํด. "
|
| 43 |
+
"๊ทธ ์ธ์ ์๋ ์ฐธ๊ณ ํด์ ์ ํํ๊ณ ์์ฐ์ค๋ฌ์ด ํ๊ตญ์ด 80์ ์ด๋ด๋ก ๋ตํด.\n"
|
| 44 |
"์์: Q: ๋ถ์์ ๋ง์
์ด ๋ญ์ผ?\nA: ๋ถ๋ชจ๊ฐ ๊ฐ์ ๋ ๋ถ์๋ผ๋ฆฌ ๋ํ๋ฉด ๋ฉ๋๋ค.\n"
|
| 45 |
)
|
| 46 |
info = context if context else "์ ๋ณด ์์"
|
| 47 |
+
prompt = f"{persona_guide}---\n[์ ๋ณด]\n{info}\n[์ง๋ฌธ]\n{query}\n[๋ต๋ณ] "
|
|
|
|
|
|
|
| 48 |
|
| 49 |
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
| 50 |
model.eval()
|
|
|
|
| 62 |
num_beams=1
|
| 63 |
)
|
| 64 |
response = tokenizer.decode(gen_ids[0], skip_special_tokens=True)
|
| 65 |
+
# ๋ต๋ณ๋ง ์ถ์ถ
|
| 66 |
+
answer = response.split("๋ต๋ณ:")[-1].strip() if "๋ต๋ณ:" in response else response.strip()
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
# ์๋ฏธ ์๋ ๋ฌธ์ ํํฐ๋ง
|
|
|
|
|
|
|
| 69 |
answer = re.sub(r"[^๊ฐ-ํฃ0-9 .,!?~\n]", "", answer)
|
|
|
|
| 70 |
answer = re.sub(r"([.,!?~])\1{2,}", r"\1", answer)
|
| 71 |
answer = re.sub(r"[a-zA-Z]+", "", answer)
|
|
|
|
| 72 |
answer = re.sub(r"[=^*/\\]+", "", answer)
|
|
|
|
| 73 |
answer = re.sub(r"\s+", " ", answer).strip()
|
| 74 |
|
| 75 |
+
# 80์ ์ด๋ด ์๋ฅด๊ธฐ
|
| 76 |
def truncate_korean(text, max_len=80):
|
| 77 |
count = 0
|
| 78 |
result = ""
|
| 79 |
for ch in text:
|
|
|
|
| 80 |
result += ch
|
| 81 |
count += 1
|
| 82 |
if count >= max_len:
|
|
|
|
| 84 |
return result
|
| 85 |
answer = truncate_korean(answer, 80)
|
| 86 |
|
| 87 |
+
# ๋ฌธ์ฅ ๋ ์์ฐ์ค๋ฝ๊ฒ
|
| 88 |
if answer and answer[-1] not in ".!?":
|
| 89 |
answer += "."
|
| 90 |
return answer
|
| 91 |
except Exception as e:
|
| 92 |
print(f"ask_sayknow ์๋ฌ: {e}")
|
|
|
|
| 93 |
traceback.print_exc()
|
| 94 |
return f"์ค๋ฅ: {str(e)}"
|
| 95 |
|
| 96 |
+
# 3. API (XML ์๋ต ์ ์ง)
|
| 97 |
@app.route('/chatapi.html', methods=['GET'])
|
| 98 |
@app.route('/index.html', methods=['GET'])
|
|
|
|
| 99 |
def chat_api():
|
| 100 |
query = request.args.get('askdata', '')
|
|
|
|
| 101 |
if not query:
|
| 102 |
result = {"status": "error", "message": "No data"}
|
| 103 |
else:
|
|
|
|
| 110 |
}
|
| 111 |
except Exception as e:
|
| 112 |
print(f"chat_api ์๋ฌ: {e}")
|
|
|
|
| 113 |
traceback.print_exc()
|
| 114 |
result = {
|
| 115 |
"service": "Sayknow",
|
|
|
|
| 118 |
"error": str(e)
|
| 119 |
}
|
| 120 |
|
|
|
|
| 121 |
xml_output = dicttoxml(result, custom_root='SayknowAPI', attr_type=False)
|
| 122 |
return Response(xml_output, mimetype='text/xml')
|
| 123 |
|
| 124 |
+
# 4. ์น UI (๊ฐ๋จํ ์ง๋ฌธ ํผ + ๋ต๋ณ)
|
| 125 |
+
@app.route('/', methods=['GET', 'POST'])
|
| 126 |
+
def index():
|
| 127 |
+
answer = ""
|
| 128 |
+
question = ""
|
| 129 |
+
if request.method == "POST":
|
| 130 |
+
question = request.form.get('question', '')
|
| 131 |
+
if question:
|
| 132 |
+
answer = ask_sayknow(question)
|
| 133 |
+
|
| 134 |
+
html = f"""
|
| 135 |
+
<html>
|
| 136 |
+
<head>
|
| 137 |
+
<title>Sayknow ์ฑ๋ด</title>
|
| 138 |
+
</head>
|
| 139 |
+
<body>
|
| 140 |
+
<h2>Sayknow ํ๊ตญ์ด ์ฑ๋ด</h2>
|
| 141 |
+
<form method="post" action="/">
|
| 142 |
+
<input type="text" name="question" value="{question}" placeholder="์ง๋ฌธ์ ์
๋ ฅํ์ธ์" style="width:300px;" autofocus />
|
| 143 |
+
<input type="submit" value="์ง๋ฌธํ๊ธฐ" />
|
| 144 |
+
</form>
|
| 145 |
+
<hr>
|
| 146 |
+
<h3>๋ต๋ณ:</h3>
|
| 147 |
+
<p style="white-space: pre-wrap;">{answer}</p>
|
| 148 |
+
</body>
|
| 149 |
+
</html>
|
| 150 |
+
"""
|
| 151 |
+
return render_template_string(html)
|
| 152 |
+
|
| 153 |
if __name__ == '__main__':
|
| 154 |
+
app.run(host='0.0.0.0', port=7860)
|