Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,20 @@
|
|
| 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 |
-
import re
|
| 7 |
import traceback
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
|
| 13 |
-
# 1. ๋ชจ๋ธ ๋ก๋ (๊ธฐ์กด๊ณผ ๋์ผ)
|
| 14 |
print("ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained("skt/kogpt2-base-v2", trust_remote_code=True)
|
| 16 |
print("๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 17 |
model = GPT2LMHeadModel.from_pretrained("skt/kogpt2-base-v2", trust_remote_code=True)
|
| 18 |
print("๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 19 |
|
| 20 |
-
# 2. ๋ฐ์ดํฐ์
๋ก๋
|
| 21 |
try:
|
| 22 |
df = pd.read_excel('dataset.xlsx')
|
| 23 |
knowledge_list = df['๋ฐ์ดํฐ์
์ ๋ฃ์ ๋ด์ฉ(*)'].tolist()
|
|
@@ -26,7 +23,7 @@ except Exception as e:
|
|
| 26 |
knowledge_list = []
|
| 27 |
|
| 28 |
def find_relevant_context(query, top_n=2):
|
| 29 |
-
"""์ง๋ฌธ๊ณผ ๊ด๋ จ๋
|
| 30 |
query_words = query.replace(" ", "").lower()
|
| 31 |
relevant_sentences = []
|
| 32 |
for s in knowledge_list:
|
|
@@ -41,14 +38,13 @@ def ask_sayknow(query):
|
|
| 41 |
try:
|
| 42 |
context = find_relevant_context(query)
|
| 43 |
persona_guide = (
|
| 44 |
-
"๋๋ ์ง์ ๊ธฐ๋ฐ ํ๊ตญ์ด ์ฑ๋ด Sayknow์ผ. ์๊ธฐ์๊ฐ
|
| 45 |
-
"๊ทธ
|
| 46 |
"์์: Q: ๋ถ์์ ๋ง์
์ด ๋ญ์ผ?\nA: ๋ถ๋ชจ๊ฐ ๊ฐ์ ๋ ๋ถ์๋ผ๋ฆฌ ๋ํ๋ฉด ๋ฉ๋๋ค.\n"
|
| 47 |
)
|
| 48 |
info = context if context else "์ ๋ณด ์์"
|
| 49 |
prompt = f"{persona_guide}---\n[์ ๋ณด]\n{info}\n[์ง๋ฌธ]\n{query}\n[๋ต๋ณ] "
|
| 50 |
|
| 51 |
-
# ์ด์ ๋ต๋ณ ๋ก์ง ๊ฐ์ (attention_mask ์ถ๊ฐ) - ์ด ๋ถ๋ถ์ ์ ์๋ํ๊ณ ์์ ๊ฑฐ์ผ!
|
| 52 |
tokenizer.pad_token = tokenizer.eos_token
|
| 53 |
encoded_input = tokenizer.encode_plus(
|
| 54 |
prompt,
|
|
@@ -58,13 +54,13 @@ def ask_sayknow(query):
|
|
| 58 |
)
|
| 59 |
input_ids = encoded_input['input_ids']
|
| 60 |
attention_mask = encoded_input['attention_mask']
|
| 61 |
-
|
| 62 |
model.eval()
|
| 63 |
with torch.no_grad():
|
| 64 |
gen_ids = model.generate(
|
| 65 |
input_ids,
|
| 66 |
attention_mask=attention_mask,
|
| 67 |
-
max_new_tokens=200,
|
| 68 |
min_length=5,
|
| 69 |
repetition_penalty=1.3,
|
| 70 |
do_sample=True,
|
|
@@ -74,29 +70,27 @@ def ask_sayknow(query):
|
|
| 74 |
temperature=0.5,
|
| 75 |
num_beams=1
|
| 76 |
)
|
| 77 |
-
raw_response = tokenizer.decode(gen_ids[0], skip_special_tokens=True)
|
| 78 |
-
|
| 79 |
-
#
|
| 80 |
-
# 1. ๋ชจ๋ธ์ด ์์ฑํ ์ ์ฒด ํ
์คํธ์์ ํ๋กฌํํธ ๋ถ๋ถ ์๋ฅด๊ธฐ (๋ฐ๋ณต๋๋ ๋ฌธ์ ๋ฐฉ์ง)
|
| 81 |
-
# prompt๊ฐ raw_response์ ์์ ๋ถ๋ถ์ ์๋ค๋ฉด ๊ทธ ๋ถ๋ถ์ ์๋ผ๋ผ๊ฒ.
|
| 82 |
if raw_response.startswith(prompt):
|
| 83 |
-
|
| 84 |
else:
|
| 85 |
-
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
if answer and answer[-1] not in ".!?":
|
| 89 |
answer += "."
|
| 90 |
-
elif not answer:
|
| 91 |
-
answer = "์ ์ ์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค."
|
| 92 |
|
| 93 |
return answer
|
| 94 |
except Exception as e:
|
| 95 |
print(f"ask_sayknow ์๋ฌ: {e}")
|
| 96 |
traceback.print_exc()
|
| 97 |
-
return f"๋ด๋ถ ์ค๋ฅ: {str(e)}"
|
| 98 |
|
| 99 |
-
# 3. API (XML ์๋ต
|
| 100 |
@app.route('/chatapi.html', methods=['GET'])
|
| 101 |
@app.route('/index.html', methods=['GET'])
|
| 102 |
def chat_api():
|
|
@@ -124,34 +118,5 @@ def chat_api():
|
|
| 124 |
xml_output = dicttoxml(result, custom_root='SayknowAPI', attr_type=False)
|
| 125 |
return Response(xml_output, mimetype='text/xml')
|
| 126 |
|
| 127 |
-
# 4. ์น UI (๊ฐ๋จํ ์ง๋ฌธ ํผ + ๋ต๋ณ) - hCaptcha ์ฝ๋ ์ ๋ถ ์ ๊ฑฐ!
|
| 128 |
-
@app.route('/', methods=['GET', 'POST'])
|
| 129 |
-
def index():
|
| 130 |
-
answer = ""
|
| 131 |
-
question = ""
|
| 132 |
-
# error_message ์ ๊ฑฐ
|
| 133 |
-
|
| 134 |
-
if request.method == "POST":
|
| 135 |
-
question = request.form.get('question', '')
|
| 136 |
-
# hcaptcha_response ๊ด๋ จ ๋ก์ง ์ ๊ฑฐ
|
| 137 |
-
|
| 138 |
-
# hCaptcha ๊ฒ์ฆ ๋ก์ง ์ ๊ฑฐ
|
| 139 |
-
if question: # ์ง๋ฌธ์ด ์์ผ๋ฉด ๋ฐ๋ก ๋ต๋ณ ์์ฑ!
|
| 140 |
-
answer = ask_sayknow(question)
|
| 141 |
-
|
| 142 |
-
html = f"""
|
| 143 |
-
<html>
|
| 144 |
-
<head>
|
| 145 |
-
<title>Sayknow ์ฑ๋ด</title>
|
| 146 |
-
<!-- hCaptcha ์คํฌ๋ฆฝํธ ์ ๊ฑฐ -->
|
| 147 |
-
</head>
|
| 148 |
-
<body>
|
| 149 |
-
<h2>Sayknow ํ๊ตญ์ด ์ฑ๋ด</h2>
|
| 150 |
-
<h1>์๋น์ค๋ฅผ ์ฌ์ฉํ์๋ ค๋ฉด <a herf=sayknow.ggm.kr>sayknow.ggm.kr<a>๋ก ์ด๋ํด์ฃผ์ธ์.
|
| 151 |
-
</body>
|
| 152 |
-
</html>
|
| 153 |
-
"""
|
| 154 |
-
return render_template_string(html)
|
| 155 |
-
|
| 156 |
if __name__ == '__main__':
|
| 157 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 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 |
import traceback
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
+
# --- 1. ๋ชจ๋ธ ๋ก๋ ---
|
|
|
|
|
|
|
| 11 |
print("ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained("skt/kogpt2-base-v2", trust_remote_code=True)
|
| 13 |
print("๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 14 |
model = GPT2LMHeadModel.from_pretrained("skt/kogpt2-base-v2", trust_remote_code=True)
|
| 15 |
print("๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 16 |
|
| 17 |
+
# --- 2. ๋ฐ์ดํฐ์
๋ก๋ ---
|
| 18 |
try:
|
| 19 |
df = pd.read_excel('dataset.xlsx')
|
| 20 |
knowledge_list = df['๋ฐ์ดํฐ์
์ ๋ฃ์ ๋ด์ฉ(*)'].tolist()
|
|
|
|
| 23 |
knowledge_list = []
|
| 24 |
|
| 25 |
def find_relevant_context(query, top_n=2):
|
| 26 |
+
"""์ง๋ฌธ๊ณผ ๊ด๋ จ๋ ์ง์ ๋ฐ์ดํฐ ๋ฌธ์ฅ ์ต๋ top_n๊ฐ ๋ฐํ"""
|
| 27 |
query_words = query.replace(" ", "").lower()
|
| 28 |
relevant_sentences = []
|
| 29 |
for s in knowledge_list:
|
|
|
|
| 38 |
try:
|
| 39 |
context = find_relevant_context(query)
|
| 40 |
persona_guide = (
|
| 41 |
+
"๋๋ ์ง์ ๊ธฐ๋ฐ ํ๊ตญ์ด ์ฑ๋ด Sayknow์ผ. ์๊ธฐ์๊ฐ ์ง๋ฌธ์๋ '์ ๋ Sayknow์
๋๋ค.'๋ผ๊ณ ๋ตํด. "
|
| 42 |
+
"๊ทธ ์ธ์๋ ์๋ ์ฐธ๊ณ ํด์ ์ ํํ๊ณ ์์ฐ์ค๋ฌ์ด ํ๊ตญ์ด ๋ฌธ์ฅ์ผ๋ก 80์ ์ด๋ด๋ก ๋ตํด.\n"
|
| 43 |
"์์: Q: ๋ถ์์ ๋ง์
์ด ๋ญ์ผ?\nA: ๋ถ๋ชจ๊ฐ ๊ฐ์ ๋ ๋ถ์๋ผ๋ฆฌ ๋ํ๋ฉด ๋ฉ๋๋ค.\n"
|
| 44 |
)
|
| 45 |
info = context if context else "์ ๋ณด ์์"
|
| 46 |
prompt = f"{persona_guide}---\n[์ ๋ณด]\n{info}\n[์ง๋ฌธ]\n{query}\n[๋ต๋ณ] "
|
| 47 |
|
|
|
|
| 48 |
tokenizer.pad_token = tokenizer.eos_token
|
| 49 |
encoded_input = tokenizer.encode_plus(
|
| 50 |
prompt,
|
|
|
|
| 54 |
)
|
| 55 |
input_ids = encoded_input['input_ids']
|
| 56 |
attention_mask = encoded_input['attention_mask']
|
| 57 |
+
|
| 58 |
model.eval()
|
| 59 |
with torch.no_grad():
|
| 60 |
gen_ids = model.generate(
|
| 61 |
input_ids,
|
| 62 |
attention_mask=attention_mask,
|
| 63 |
+
max_new_tokens=200,
|
| 64 |
min_length=5,
|
| 65 |
repetition_penalty=1.3,
|
| 66 |
do_sample=True,
|
|
|
|
| 70 |
temperature=0.5,
|
| 71 |
num_beams=1
|
| 72 |
)
|
| 73 |
+
raw_response = tokenizer.decode(gen_ids[0], skip_special_tokens=True)
|
| 74 |
+
|
| 75 |
+
# ํ๋กฌํํธ ์ ๊ฑฐ ํ ์ค์ ๋ต๋ณ ์ถ์ถ
|
|
|
|
|
|
|
| 76 |
if raw_response.startswith(prompt):
|
| 77 |
+
answer = raw_response[len(prompt):].strip()
|
| 78 |
else:
|
| 79 |
+
answer = raw_response.strip()
|
| 80 |
|
| 81 |
+
# ๋ฌธ์ฅ ๋ ์ฒ๋ฆฌ
|
| 82 |
+
if answer and answer[-1] not in ".!?":
|
| 83 |
answer += "."
|
| 84 |
+
elif not answer:
|
| 85 |
+
answer = "์ ์ ์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค."
|
| 86 |
|
| 87 |
return answer
|
| 88 |
except Exception as e:
|
| 89 |
print(f"ask_sayknow ์๋ฌ: {e}")
|
| 90 |
traceback.print_exc()
|
| 91 |
+
return f"๋ด๋ถ ์ค๋ฅ: {str(e)}"
|
| 92 |
|
| 93 |
+
# --- 3. API (XML ์๋ต) ---
|
| 94 |
@app.route('/chatapi.html', methods=['GET'])
|
| 95 |
@app.route('/index.html', methods=['GET'])
|
| 96 |
def chat_api():
|
|
|
|
| 118 |
xml_output = dicttoxml(result, custom_root='SayknowAPI', attr_type=False)
|
| 119 |
return Response(xml_output, mimetype='text/xml')
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
if __name__ == '__main__':
|
| 122 |
+
app.run(host='0.0.0.0', port=7860)
|