hewoo commited on
Commit
5be7074
β€’
1 Parent(s): ee37e7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -3,17 +3,18 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
  from sentence_transformers import SentenceTransformer
4
  from langchain.vectorstores import Chroma
5
  import os
 
 
6
 
7
  # Hugging Face λͺ¨λΈ ID
8
  model_id = "hewoo/hehehehe"
9
- token = os.getenv("HF_API_TOKEN") # ν•„μš”ν•œ 경우 μ‚¬μš©μžμ—κ²Œ Hugging Face API 토큰 μž…λ ₯을 μš”μ²­ν•  수 있음
 
 
10
 
11
- # λͺ¨λΈκ³Ό ν† ν¬λ‚˜μ΄μ € λ‘œλ“œ
12
- tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=token)
13
- model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=token)
14
 
15
  # ν…μŠ€νŠΈ 생성 νŒŒμ΄ν”„λΌμΈ μ„€μ •
16
- pipe = 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)
17
 
18
  # μ‚¬μš©μž μ •μ˜ μž„λ² λ”© 클래슀 생성
19
  class CustomEmbedding:
@@ -27,29 +28,49 @@ class CustomEmbedding:
27
  return [self.model.encode(text, convert_to_tensor=True).tolist() for text in texts]
28
 
29
  # μž„λ² λ”© λͺ¨λΈ 및 벑터 μŠ€ν† μ–΄ μ„€μ •
30
- embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
31
  embedding_function = CustomEmbedding(embedding_model)
32
 
33
  # Chroma 벑터 μŠ€ν† μ–΄ μ„€μ •
34
- persist_directory = "./chroma_batch_vectors" # Spaces ν™˜κ²½μ— 맞게 μ‘°μ • ν•„μš”
35
  vectorstore = Chroma(persist_directory=persist_directory, embedding_function=embedding_function)
36
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
37
 
38
  # μ§ˆλ¬Έμ— λŒ€ν•œ 응닡 생성 ν•¨μˆ˜
39
  def generate_response(user_input):
 
 
 
40
  search_results = retriever.get_relevant_documents(user_input)
41
  context = "\n".join([result.page_content for result in search_results])
42
- input_text = f"λ§₯락: {context}\n질문: {user_input}"
 
 
 
 
 
 
 
43
  response = pipe(input_text)[0]["generated_text"]
44
- return response
 
 
 
 
 
 
 
 
45
 
46
  # Streamlit μ•± UI
47
- st.title("챗봇 test")
48
  st.write("Llama 3.2-3B λͺ¨λΈμ„ μ‚¬μš©ν•œ μ±—λ΄‡μž…λ‹ˆλ‹€. μ§ˆλ¬Έμ„ μž…λ ₯ν•΄ μ£Όμ„Έμš”.")
49
 
50
  # μ‚¬μš©μž μž…λ ₯ λ°›κΈ°
51
  user_input = st.text_input("질문")
52
  if user_input:
53
- response = generate_response(user_input)
54
  st.write("챗봇 응닡:", response)
 
 
55
 
 
3
  from sentence_transformers import SentenceTransformer
4
  from langchain.vectorstores import Chroma
5
  import os
6
+ import psutil
7
+ import time
8
 
9
  # Hugging Face λͺ¨λΈ ID
10
  model_id = "hewoo/hehehehe"
11
+ # λͺ¨λΈκ³Ό ν† ν¬λ‚˜μ΄μ € λ‘œλ“œ (토큰 없이 μ‚¬μš©)
12
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
13
+ model = AutoModelForCausalLM.from_pretrained(model_id)
14
 
 
 
 
15
 
16
  # ν…μŠ€νŠΈ 생성 νŒŒμ΄ν”„λΌμΈ μ„€μ •
17
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=150, temperature=0.3, top_p=0.85, top_k=40, repetition_penalty=1.2)
18
 
19
  # μ‚¬μš©μž μ •μ˜ μž„λ² λ”© 클래슀 생성
20
  class CustomEmbedding:
 
28
  return [self.model.encode(text, convert_to_tensor=True).tolist() for text in texts]
29
 
30
  # μž„λ² λ”© λͺ¨λΈ 및 벑터 μŠ€ν† μ–΄ μ„€μ •
31
+ embedding_model = SentenceTransformer("jhgan/ko-sroberta-multitask")
32
  embedding_function = CustomEmbedding(embedding_model)
33
 
34
  # Chroma 벑터 μŠ€ν† μ–΄ μ„€μ •
35
+ persist_directory = "./chroma_batch_vectors"
36
  vectorstore = Chroma(persist_directory=persist_directory, embedding_function=embedding_function)
37
  retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
38
 
39
  # μ§ˆλ¬Έμ— λŒ€ν•œ 응닡 생성 ν•¨μˆ˜
40
  def generate_response(user_input):
41
+ start_time = time.time() # μ‹œμž‘ μ‹œκ°„ 기둝
42
+
43
+ # λ¬Έμ„œ 검색 및 λ§₯락 생성
44
  search_results = retriever.get_relevant_documents(user_input)
45
  context = "\n".join([result.page_content for result in search_results])
46
+ input_text = f"""μ•„λž˜λŠ” ν•œκ΅­μ–΄λ‘œλ§Œ λ‹΅λ³€ν•˜λŠ” μ–΄μ‹œμŠ€ν„΄νŠΈμž…λ‹ˆλ‹€.
47
+ μ‚¬μš©μžμ˜ μ§ˆλ¬Έμ— λŒ€ν•΄ 제곡된 λ§₯락을 λ°”νƒ•μœΌλ‘œ μ •ν™•ν•˜κ³  μžμ„Έν•œ 닡변을 ν•œκ΅­μ–΄λ‘œ μž‘μ„±ν•˜μ„Έμš”.
48
+
49
+ λ§₯락: {context}
50
+ 질문: {user_input}
51
+ λ‹΅λ³€:"""
52
+
53
+ # 응닡 생성
54
  response = pipe(input_text)[0]["generated_text"]
55
+
56
+ end_time = time.time() # λλ‚œ μ‹œκ°„ 기둝
57
+ response_time = end_time - start_time # 응닡 μ‹œκ°„ 계산
58
+
59
+ # λ©”λͺ¨λ¦¬ μ‚¬μš©λŸ‰ λͺ¨λ‹ˆν„°λ§
60
+ memory_info = psutil.virtual_memory()
61
+ memory_usage = memory_info.percent # λ©”λͺ¨λ¦¬ μ‚¬μš©λŸ‰ λΉ„μœ¨(%)
62
+
63
+ return response, response_time, memory_usage
64
 
65
  # Streamlit μ•± UI
66
+ st.title("챗봇 데λͺ¨")
67
  st.write("Llama 3.2-3B λͺ¨λΈμ„ μ‚¬μš©ν•œ μ±—λ΄‡μž…λ‹ˆλ‹€. μ§ˆλ¬Έμ„ μž…λ ₯ν•΄ μ£Όμ„Έμš”.")
68
 
69
  # μ‚¬μš©μž μž…λ ₯ λ°›κΈ°
70
  user_input = st.text_input("질문")
71
  if user_input:
72
+ response, response_time, memory_usage = generate_response(user_input)
73
  st.write("챗봇 응닡:", response)
74
+ st.write(f"응닡 μ‹œκ°„: {response_time:.2f}초")
75
+ st.write(f"ν˜„μž¬ λ©”λͺ¨λ¦¬ μ‚¬μš©λŸ‰: {memory_usage}%")
76