SayknowLab commited on
Commit
233097c
ยท
verified ยท
1 Parent(s): d061c72

Delete app.py

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