hewoo commited on
Commit
6ab87db
ยท
verified ยท
1 Parent(s): b9a5673

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -22
app.py CHANGED
@@ -2,23 +2,19 @@ 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 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:
@@ -31,13 +27,13 @@ class CustomEmbedding:
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): # _embedding_model๋กœ ์ด๋ฆ„ ๋ณ€๊ฒฝ
41
  embedding_function = CustomEmbedding(_embedding_model)
42
  return Chroma(persist_directory="./chroma_batch_vectors", embedding_function=embedding_function)
43
 
@@ -46,9 +42,22 @@ 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()
@@ -56,20 +65,21 @@ embedding_model = load_embedding_model()
56
  vectorstore = load_vectorstore(embedding_model)
57
 
58
  # Streamlit ์•ฑ UI
59
- st.title("์ฑ—๋ด‡ tessst")
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
 
75
-
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  from sentence_transformers import SentenceTransformer
4
  from langchain.vectorstores import Chroma
5
+ import os
6
  import psutil
7
+ import gc
8
 
9
+ # Hugging Face ๋ชจ๋ธ ID
10
  model_id = "hewoo/hehehehe"
11
 
12
+ # ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋ธ๊ณผ ํŒŒ์ดํ”„๋ผ์ธ ๋กœ๋“œ
 
 
 
 
 
13
  @st.cache_resource
14
  def load_model():
15
  tokenizer = AutoTokenizer.from_pretrained(model_id)
16
  model = AutoModelForCausalLM.from_pretrained(model_id)
17
+ return pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=150)
18
 
19
  # ์‚ฌ์šฉ์ž ์ •์˜ ์ž„๋ฒ ๋”ฉ ํด๋ž˜์Šค
20
  class CustomEmbedding:
 
27
  def embed_documents(self, texts):
28
  return [self.model.encode(text, convert_to_tensor=True).tolist() for text in texts]
29
 
30
+ # ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ ๋ฐ ๋ฒกํ„ฐ ์Šคํ† ์–ด ์„ค์ • (์บ์‹œ ์ ์šฉ)
31
  @st.cache_resource
32
  def load_embedding_model():
33
+ return SentenceTransformer("jhgan/ko-sroberta-multitask")
34
 
35
  @st.cache_resource
36
+ def load_vectorstore(_embedding_model):
37
  embedding_function = CustomEmbedding(_embedding_model)
38
  return Chroma(persist_directory="./chroma_batch_vectors", embedding_function=embedding_function)
39
 
 
42
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
43
  search_results = retriever.get_relevant_documents(user_input)
44
  context = "\n".join([result.page_content for result in search_results])
45
+
46
+ prompt = f"""๋‹ค์Œ์€ ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•˜๋Š” ํ•œ๊ตญ์–ด ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค.
47
+ ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ๋งฅ๋ฝ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ์ •ํ™•ํ•˜๊ณ  ์ž์„ธํ•œ ๋‹ต๋ณ€์„ ํ•œ๊ตญ์–ด๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.
48
+ ๋งŒ์•ฝ ๋งฅ๋ฝ์— ๊ด€๋ จ ์ •๋ณด๊ฐ€ ์—†์œผ๋ฉด, "์ฃ„์†กํ•˜์ง€๋งŒ ํ•ด๋‹น ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."๋ผ๊ณ  ๋‹ต๋ณ€ํ•˜์„ธ์š”.
49
+
50
+ ๋งฅ๋ฝ:
51
+ {context}
52
+
53
+ ์งˆ๋ฌธ:
54
+ {user_input}
55
+
56
+ ๋‹ต๋ณ€:"""
57
+
58
+ response = pipe(prompt)[0]["generated_text"]
59
+ response = response.split("๋‹ต๋ณ€:")[-1].strip()
60
+ return response, context
61
 
62
  # ๋ชจ๋ธ ๋ฐ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ ๋กœ๋“œ
63
  pipe = load_model()
 
65
  vectorstore = load_vectorstore(embedding_model)
66
 
67
  # Streamlit ์•ฑ UI
68
+ st.title("์ฑ—๋ด‡ ๋ฐ๋ชจ")
69
+ st.write("ํ•œ๊ตญ์–ด ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ์ฑ—๋ด‡์ž…๋‹ˆ๋‹ค. ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.")
 
 
70
 
71
  # ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๋ฐ›๊ธฐ
72
  user_input = st.text_input("์งˆ๋ฌธ")
73
  if user_input:
74
+ response, context = generate_response(user_input)
75
  st.write("์ฑ—๋ด‡ ์‘๋‹ต:", response)
76
+ st.write("์‚ฌ์šฉ๋œ ๋งฅ๋ฝ:", context)
77
+
78
+ # ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ ๋ชจ๋‹ˆํ„ฐ๋ง
79
+ memory_usage = psutil.virtual_memory().used / (1024 ** 3)
80
+ st.write(f"ํ˜„์žฌ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰: {memory_usage:.2f} GB")
81
 
82
  # ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ
83
  del response
84
  gc.collect()
85