Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,181 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import torch
|
| 3 |
-
from flask import Flask, request, Response
|
| 4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
-
from dicttoxml import dicttoxml
|
| 6 |
-
import traceback
|
| 7 |
-
import re
|
| 8 |
-
from threading import Lock
|
| 9 |
-
|
| 10 |
-
app = Flask(__name__)
|
| 11 |
-
|
| 12 |
-
# --- 1. ๋๋ฐ์ด์ค ์ค์ ---
|
| 13 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
-
print(f"์ฌ์ฉ ๋๋ฐ์ด์ค: {device}")
|
| 15 |
-
|
| 16 |
-
torch.set_grad_enabled(False)
|
| 17 |
-
|
| 18 |
-
# --- 2. ๋ชจ๋ธ ๋ก๋ ---
|
| 19 |
-
print("ํ ํฌ๋์ด์ ๋ก๋ฉ ์ค...")
|
| 20 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 21 |
-
"LiquidAI/LFM2.5-1.2B-Instruct",
|
| 22 |
-
trust_remote_code=True
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
print("๋ชจ๋ธ ๋ก๋ฉ ์ค...")
|
| 26 |
-
try:
|
| 27 |
-
# 8bit ๋ก๋ ์๋
|
| 28 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 29 |
-
"LiquidAI/LFM2.5-1.2B-Instruct",
|
| 30 |
-
device_map="auto",
|
| 31 |
-
load_in_8bit=True,
|
| 32 |
-
trust_remote_code=True
|
| 33 |
-
)
|
| 34 |
-
print("8bit ๋ก๋ฉ ์ฑ๊ณต")
|
| 35 |
-
except:
|
| 36 |
-
# ์คํจ ์ ์ผ๋ฐ ๋ก๋
|
| 37 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 38 |
-
"LiquidAI/LFM2.5-1.2B-Instruct",
|
| 39 |
-
trust_remote_code=True
|
| 40 |
-
).to(device)
|
| 41 |
-
print("์ผ๋ฐ ๋ก๋ฉ ์ฌ์ฉ")
|
| 42 |
-
|
| 43 |
-
# torch 2.0 ์ด์์ด๋ฉด ์ปดํ์ผ
|
| 44 |
-
try:
|
| 45 |
-
model = torch.compile(model)
|
| 46 |
-
print("torch.compile ์ ์ฉ ์๋ฃ")
|
| 47 |
-
except:
|
| 48 |
-
print("torch.compile ๋ฏธ์ ์ฉ (์ง์ ์ํจ)")
|
| 49 |
-
|
| 50 |
-
print("๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ!")
|
| 51 |
-
|
| 52 |
-
# --- 3. ๋ฐ์ดํฐ์
๋ก๋ ---
|
| 53 |
-
try:
|
| 54 |
-
df = pd.read_excel('dataset.xlsx')
|
| 55 |
-
knowledge_list = df['๋ฐ์ดํฐ์
์ ๋ฃ์ ๋ด์ฉ(*)'].tolist()
|
| 56 |
-
except Exception as e:
|
| 57 |
-
print(f"๋ฐ์ดํฐ์
๋ก๋ ์๋ฌ: {e}")
|
| 58 |
-
knowledge_list = []
|
| 59 |
-
|
| 60 |
-
# --- 4. ๋์ ์์ฒญ ์ ํ์ฉ Lock (๊ตฌ์กฐ ์ ์ง) ---
|
| 61 |
-
request_lock = Lock()
|
| 62 |
-
|
| 63 |
-
# --- 5. ์ง๋ฌธ๊ณผ ๊ด๋ จ๋ ์ง์ ๊ฒ์ (๊ธฐ์กด ๋ฐฉ์ ์ ์ง) ---
|
| 64 |
-
def find_relevant_context(query, top_n=2):
|
| 65 |
-
query_words = query.replace(" ", "").lower()
|
| 66 |
-
relevant_sentences = []
|
| 67 |
-
for s in knowledge_list:
|
| 68 |
-
s_text = str(s).replace(" ", "").replace("\n", "").lower()
|
| 69 |
-
if any(word.replace(" ", "") in s_text for word in query.split()):
|
| 70 |
-
relevant_sentences.append(s)
|
| 71 |
-
return " ".join(str(s) for s in relevant_sentences[:top_n]) if relevant_sentences else ""
|
| 72 |
-
|
| 73 |
-
# --- 6. Sayknow ๋ต๋ณ ์์ฑ ---
|
| 74 |
-
def ask_sayknow(query):
|
| 75 |
-
try:
|
| 76 |
-
context = find_relevant_context(query)
|
| 77 |
-
|
| 78 |
-
persona_guide = (
|
| 79 |
-
"๋๋ ์ง์ ๊ธฐ๋ฐ ํ๊ตญ์ด ์ฑ๋ด Sayknow์ผ. ์๊ธฐ์๊ฐ ์ง๋ฌธ์๋ '์ ๋ Sayknow์
๋๋ค.'๋ผ๊ณ ๋ตํด. "
|
| 80 |
-
"๊ทธ ์ธ์๋ ์๋ ์ฐธ๊ณ ํด์ ์ ํํ๊ณ ์์ฐ์ค๋ฌ์ด ํ๊ตญ์ด ๋ฌธ์ฅ์ผ๋ก 80์ ์ด๋ด๋ก ๋ตํด.\n"
|
| 81 |
-
"์์: Q: ๋ถ์์ ๋ง์
์ด ๋ญ์ผ?\nA: ๋ถ๋ชจ๊ฐ ๊ฐ์ ๋ ๋ถ์๋ผ๋ฆฌ ๋ํ๋ฉด ๋ฉ๋๋ค.\n"
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
info = context if context else "์ ๋ณด ์์"
|
| 85 |
-
|
| 86 |
-
prompt = f"{persona_guide}---\n[์ ๋ณด]\n{info}\n[์ง๋ฌธ]\n{query}\n[๋ต๋ณ] "
|
| 87 |
-
|
| 88 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 89 |
-
|
| 90 |
-
encoded_input = tokenizer.encode_plus(
|
| 91 |
-
prompt,
|
| 92 |
-
return_tensors='pt',
|
| 93 |
-
truncation=True,
|
| 94 |
-
padding=True
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
input_ids = encoded_input['input_ids'].to(device)
|
| 98 |
-
attention_mask = encoded_input['attention_mask'].to(device)
|
| 99 |
-
|
| 100 |
-
model.eval()
|
| 101 |
-
|
| 102 |
-
gen_ids = model.generate(
|
| 103 |
-
input_ids,
|
| 104 |
-
attention_mask=attention_mask,
|
| 105 |
-
max_new_tokens=60, # ์ค์
|
| 106 |
-
min_length=5,
|
| 107 |
-
repetition_penalty=1.2,
|
| 108 |
-
do_sample=True,
|
| 109 |
-
top_k=30,
|
| 110 |
-
top_p=0.8,
|
| 111 |
-
temperature=0.5,
|
| 112 |
-
num_beams=1,
|
| 113 |
-
pad_token_id=tokenizer.pad_token_id
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
raw_response = tokenizer.decode(gen_ids[0], skip_special_tokens=True)
|
| 117 |
-
|
| 118 |
-
# --- ๋ต๋ณ ์ถ์ถ ---
|
| 119 |
-
answer = raw_response.replace(prompt, '').strip()
|
| 120 |
-
|
| 121 |
-
if "๋ต๋ณ:" in answer:
|
| 122 |
-
answer = answer.split("๋ต๋ณ:", 1)[1].strip()
|
| 123 |
-
|
| 124 |
-
# --- ํ์ฒ๋ฆฌ (5๋ฒ ์ ์ง ์์ฒญ๋๋ก ๊ทธ๋๋ก ์ ์ง) ---
|
| 125 |
-
answer = re.sub(r"[^๊ฐ-ํฃ0-9 .,!?~\n]", "", answer)
|
| 126 |
-
answer = re.sub(r"([.,!?~])\1{2,}", r"\1", answer)
|
| 127 |
-
answer = re.sub(r"[a-zA-Z]+", "", answer)
|
| 128 |
-
answer = re.sub(r"[=^*/\\]+", "", answer)
|
| 129 |
-
answer = re.sub(r"\s+", " ", answer).strip()
|
| 130 |
-
|
| 131 |
-
# 80์ ์ ํ
|
| 132 |
-
answer = answer[:80]
|
| 133 |
-
|
| 134 |
-
if answer and answer[-1] not in ".!?":
|
| 135 |
-
answer += "."
|
| 136 |
-
elif not answer:
|
| 137 |
-
answer = "์ฃ์กํฉ๋๋ค. ์ง๋ฌธ์ ๋ํ ๋ต๋ณ์ ์ฐพ์ ์ ์์ต๋๋ค."
|
| 138 |
-
|
| 139 |
-
return answer
|
| 140 |
-
|
| 141 |
-
except Exception as e:
|
| 142 |
-
print(f"ask_sayknow ์๋ฌ: {e}")
|
| 143 |
-
traceback.print_exc()
|
| 144 |
-
return f"๋ด๋ถ ์ค๋ฅ: {str(e)}"
|
| 145 |
-
|
| 146 |
-
# --- 7. API (XML ์๋ต) ---
|
| 147 |
-
@app.route('/chatapi.html', methods=['GET'])
|
| 148 |
-
@app.route('/index.html', methods=['GET'])
|
| 149 |
-
def chat_api():
|
| 150 |
-
query = request.args.get('askdata', '')
|
| 151 |
-
|
| 152 |
-
if not query:
|
| 153 |
-
result = {"status": "error", "message": "No data"}
|
| 154 |
-
xml_output = dicttoxml(result, custom_root='SayknowAPI', attr_type=False)
|
| 155 |
-
return Response(xml_output, mimetype='text/xml')
|
| 156 |
-
|
| 157 |
-
# 6๋ฒ ์ ์ง ์์ฒญ โ Lock ์ ์ฒด ์ ์ง
|
| 158 |
-
with request_lock:
|
| 159 |
-
try:
|
| 160 |
-
answer = ask_sayknow(query)
|
| 161 |
-
result = {
|
| 162 |
-
"service": "Sayknow",
|
| 163 |
-
"question": query,
|
| 164 |
-
"answer": answer
|
| 165 |
-
}
|
| 166 |
-
except Exception as e:
|
| 167 |
-
print(f"chat_api ์๋ฌ: {e}")
|
| 168 |
-
traceback.print_exc()
|
| 169 |
-
result = {
|
| 170 |
-
"service": "Sayknow",
|
| 171 |
-
"question": query,
|
| 172 |
-
"answer": f"์๋ฌ ๋ฐ์: {str(e)}",
|
| 173 |
-
"error": str(e)
|
| 174 |
-
}
|
| 175 |
-
|
| 176 |
-
xml_output = dicttoxml(result, custom_root='SayknowAPI', attr_type=False)
|
| 177 |
-
return Response(xml_output, mimetype='text/xml')
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
if __name__ == '__main__':
|
| 181 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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