Spaces:
Running
Running
Commit
ยท
6188838
1
Parent(s):
f27b5bc
sayknow upload
Browse files- Dockerfile +15 -0
- app.py +134 -58
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Dockerfile (์ด๊ฑฐ ๊ทธ๋๋ก ๋ณต์ฌํด์ Dockerfile ์ด๋ผ๋ ์ด๋ฆ์ผ๋ก ์ ์ฅ!)
|
| 2 |
+
FROM python:3.9-slim-buster
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY app.py .
|
| 10 |
+
COPY dataset.xlsx .
|
| 11 |
+
|
| 12 |
+
EXPOSE 7860
|
| 13 |
+
|
| 14 |
+
# ๋ค Flask ์ฑ์ ์น ์๋ฒ์ฒ๋ผ ์คํ์์ผ์ฃผ๋ ๋ช
๋ น์ด (Gunicorn์ด ๋์์ค)
|
| 15 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
|
app.py
CHANGED
|
@@ -1,70 +1,146 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |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andas 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 |
|
| 9 |
+
# 1. ๋ชจ๋ธ ๋ก๋
|
| 10 |
+
print("ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/polyglot-ko-1.3b", trust_remote_code=True)
|
| 12 |
+
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 ""
|
| 39 |
|
| 40 |
+
def ask_sayknow(query):
|
| 41 |
+
try:
|
| 42 |
+
context = find_relevant_context(query)
|
| 43 |
+
|
| 44 |
+
# ์๊ธฐ์๊ฐ/์ธ์ฌ ์ง๋ฌธ์ ๋ํ ๋ช
์์ ์๋ด ์ถ๊ฐ
|
| 45 |
+
# ์ญํ , ์์, ์ ๋ณด/์ง๋ฌธ ๊ตฌ๋ถ, context ์์ ๋ '์ ๋ณด ์์' ๋ช
์
|
| 46 |
+
persona_guide = (
|
| 47 |
+
"๋๋ ์ง์ ๊ธฐ๋ฐ ํ๊ตญ์ด ์ฑ๋ด Sayknow์ผ. ์๊ธฐ์๊ฐ(์ด๋ฆ, ์ ์ฒด, ์ธ์ฌ ๋ฑ) ์ง๋ฌธ์ '์ ๋ Sayknow์
๋๋ค.'๋ผ๊ณ ๋ตํด. "
|
| 48 |
+
"๊ทธ ์ธ์๋ ์๋ ์ ๋ณด๋ฅผ ์ฐธ๊ณ ํด ์ง๋ฌธ์ ๋ํด ์ ํํ๊ณ ์์ฐ์ค๋ฌ์ด ํ๊ตญ์ด ๋ฌธ์ฅ์ผ๋ก 80์ ์ด๋ด๋ก ๋ตํด.\n"
|
| 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()
|
| 58 |
+
with torch.no_grad():
|
| 59 |
+
gen_ids = model.generate(
|
| 60 |
+
input_ids,
|
| 61 |
+
max_new_tokens=60,
|
| 62 |
+
min_length=5,
|
| 63 |
+
repetition_penalty=1.3,
|
| 64 |
+
do_sample=True,
|
| 65 |
+
top_k=30,
|
| 66 |
+
top_p=0.85,
|
| 67 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 68 |
+
temperature=0.5,
|
| 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 |
+
# 1. ์๋ฏธ ์๋ ์์/์๋ฌธ/ํน์๋ฌธ์/๋ฐ๋ณต๋ฌธ์ ๋ฑ ํํฐ๋ง
|
| 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 |
+
# 2. 80์ ์ด๋ด๋ก ์๋ฅด๊ธฐ (ํ๊ธ ๊ธฐ์ค)
|
| 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:
|
| 99 |
+
break
|
| 100 |
+
return result
|
| 101 |
+
answer = truncate_korean(answer, 80)
|
| 102 |
|
| 103 |
+
# 3. ๋ฌธ์ฅ ๋์ด ์์ฐ์ค๋ฝ์ง ์์ผ๋ฉด ๋ง์นจํ ์ถ๊ฐ
|
| 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. REST API ์๋ํฌ์ธํธ
|
| 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:
|
| 123 |
+
try:
|
| 124 |
+
answer = ask_sayknow(query)
|
| 125 |
+
result = {
|
| 126 |
+
"service": "Sayknow",
|
| 127 |
+
"question": query,
|
| 128 |
+
"answer": answer
|
| 129 |
+
}
|
| 130 |
+
except Exception as e:
|
| 131 |
+
print(f"chat_api ์๋ฌ: {e}")
|
| 132 |
+
import traceback
|
| 133 |
+
traceback.print_exc()
|
| 134 |
+
result = {
|
| 135 |
+
"service": "Sayknow",
|
| 136 |
+
"question": query,
|
| 137 |
+
"answer": f"์๋ฌ ๋ฐ์: {str(e)}",
|
| 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)
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# requirements.txt
|
| 2 |
+
pandas
|
| 3 |
+
torch
|
| 4 |
+
flask
|
| 5 |
+
transformers
|
| 6 |
+
dicttoxml
|
| 7 |
+
gunicorn # โ
โ
โ
โ
์ด๊ฑฐ ์ค์! ๋ค์์ ์ค๋ช
ํ Flask ์ฑ ์คํ ๋์ฐ๋ฏธ!
|