Update app.py
Browse files
app.py
CHANGED
@@ -2,68 +2,73 @@ import streamlit as st
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
from sentence_transformers import SentenceTransformer
|
4 |
from langchain.vectorstores import Chroma
|
5 |
-
import
|
|
|
6 |
|
7 |
-
#
|
8 |
-
model_id = "hewoo/hehehehe"
|
9 |
-
token = os.getenv("HF_API_TOKEN") # Hugging Face API ํ ํฐ (ํ์ ์ ์ค์ )
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
# ์ฌ์ฉ์ ์ ์ ์๋ฒ ๋ฉ ํด๋์ค ์์ฑ
|
21 |
class CustomEmbedding:
|
22 |
def __init__(self, model):
|
23 |
self.model = model
|
24 |
|
25 |
def embed_query(self, text):
|
26 |
-
return self.model.encode(text, convert_to_tensor=
|
27 |
|
28 |
def embed_documents(self, texts):
|
29 |
-
return [self.model.encode(text, convert_to_tensor=
|
30 |
-
|
31 |
-
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ์ค์
|
32 |
-
embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
33 |
-
embedding_function = CustomEmbedding(embedding_model)
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
# ๊ฒ์ ๊ฒฐ๊ณผ ์์ฝ ํจ์
|
44 |
-
def summarize_results(search_results):
|
45 |
-
combined_text = "\n".join([result.page_content for result in search_results])
|
46 |
-
summary = summarizer(combined_text, max_length=100, min_length=30, do_sample=False)[0]["summary_text"]
|
47 |
-
return summary
|
48 |
-
|
49 |
-
# ๊ฒ์ ๋ฐ ์๋ต ์์ฑ ํจ์
|
50 |
def generate_response(user_input):
|
51 |
-
|
52 |
search_results = retriever.get_relevant_documents(user_input)
|
53 |
context = "\n".join([result.page_content for result in search_results])
|
54 |
-
|
55 |
-
# ๋ชจ๋ธ์ ๋งฅ๋ฝ๊ณผ ์ง๋ฌธ ์ ๋ฌ
|
56 |
input_text = f"๋งฅ๋ฝ: {context}\n์ง๋ฌธ: {user_input}"
|
57 |
response = pipe(input_text)[0]["generated_text"]
|
58 |
-
|
59 |
return response
|
60 |
|
|
|
|
|
|
|
|
|
|
|
61 |
# Streamlit ์ฑ UI
|
62 |
-
st.title("
|
63 |
st.write("Llama 3.2-3B ๋ชจ๋ธ์ ์ฌ์ฉํ ์ฑ๋ด์
๋๋ค. ์ง๋ฌธ์ ์
๋ ฅํด ์ฃผ์ธ์.")
|
64 |
|
|
|
|
|
65 |
# ์ฌ์ฉ์ ์
๋ ฅ ๋ฐ๊ธฐ
|
66 |
user_input = st.text_input("์ง๋ฌธ")
|
67 |
if user_input:
|
68 |
response = generate_response(user_input)
|
69 |
st.write("์ฑ๋ด ์๋ต:", response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
from sentence_transformers import SentenceTransformer
|
4 |
from langchain.vectorstores import Chroma
|
5 |
+
import gc
|
6 |
+
import psutil
|
7 |
|
8 |
+
# ๋ชจ๋ธ ID (๊ณต๊ฐ๋ ๋ชจ๋ธ์ด์ด์ผ ํจ)
|
9 |
+
model_id = "hewoo/hehehehe"
|
|
|
10 |
|
11 |
+
# ๋ฉ๋ชจ๋ฆฌ ๋ชจ๋ํฐ๋ง ํจ์
|
12 |
+
def monitor_memory():
|
13 |
+
memory_info = psutil.virtual_memory()
|
14 |
+
st.write(f"ํ์ฌ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋: {memory_info.percent}%")
|
15 |
|
16 |
+
# ์บ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ ๋ฐ ํ์ดํ๋ผ์ธ ๋ก๋
|
17 |
+
@st.cache_resource
|
18 |
+
def load_model():
|
19 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
20 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
21 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=150, temperature=0.5, top_p=0.85, top_k=40, repetition_penalty=1.2)
|
22 |
|
23 |
+
# ์ฌ์ฉ์ ์ ์ ์๋ฒ ๋ฉ ํด๋์ค
|
|
|
|
|
24 |
class CustomEmbedding:
|
25 |
def __init__(self, model):
|
26 |
self.model = model
|
27 |
|
28 |
def embed_query(self, text):
|
29 |
+
return self.model.encode(text, convert_to_tensor=True).tolist()
|
30 |
|
31 |
def embed_documents(self, texts):
|
32 |
+
return [self.model.encode(text, convert_to_tensor=True).tolist() for text in texts]
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ฐ ๋ฒกํฐ ์คํ ์ด ์ค์
|
35 |
+
@st.cache_resource
|
36 |
+
def load_embedding_model():
|
37 |
+
return SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
38 |
|
39 |
+
@st.cache_resource
|
40 |
+
def load_vectorstore(embedding_model):
|
41 |
+
embedding_function = CustomEmbedding(embedding_model)
|
42 |
+
return Chroma(persist_directory="./chroma_batch_vectors", embedding_function=embedding_function)
|
43 |
|
44 |
+
# ์ง๋ฌธ์ ๋ํ ์๋ต ์์ฑ ํจ์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def generate_response(user_input):
|
46 |
+
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
47 |
search_results = retriever.get_relevant_documents(user_input)
|
48 |
context = "\n".join([result.page_content for result in search_results])
|
|
|
|
|
49 |
input_text = f"๋งฅ๋ฝ: {context}\n์ง๋ฌธ: {user_input}"
|
50 |
response = pipe(input_text)[0]["generated_text"]
|
|
|
51 |
return response
|
52 |
|
53 |
+
# ๋ชจ๋ธ ๋ฐ ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋
|
54 |
+
pipe = load_model()
|
55 |
+
embedding_model = load_embedding_model()
|
56 |
+
vectorstore = load_vectorstore(embedding_model)
|
57 |
+
|
58 |
# Streamlit ์ฑ UI
|
59 |
+
st.title("์ฑ๋ด ๋ฐ๋ชจ")
|
60 |
st.write("Llama 3.2-3B ๋ชจ๋ธ์ ์ฌ์ฉํ ์ฑ๋ด์
๋๋ค. ์ง๋ฌธ์ ์
๋ ฅํด ์ฃผ์ธ์.")
|
61 |
|
62 |
+
monitor_memory() # ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ ํ์ธ
|
63 |
+
|
64 |
# ์ฌ์ฉ์ ์
๋ ฅ ๋ฐ๊ธฐ
|
65 |
user_input = st.text_input("์ง๋ฌธ")
|
66 |
if user_input:
|
67 |
response = generate_response(user_input)
|
68 |
st.write("์ฑ๋ด ์๋ต:", response)
|
69 |
+
monitor_memory() # ๋ฉ๋ชจ๋ฆฌ ์ํ ์
๋ฐ์ดํธ
|
70 |
+
|
71 |
+
# ๋ฉ๋ชจ๋ฆฌ ํด์
|
72 |
+
del response
|
73 |
+
gc.collect()
|
74 |
+
|