hewoo commited on
Commit
cc0604c
ยท
verified ยท
1 Parent(s): 02e4739

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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 os
6
+
7
+ # Hugging Face ๋ชจ๋ธ ID
8
+ model_id = "hewoo/meta-llama-3.2-3b-chatbot" # ์—…๋กœ๋“œํ•œ ๋ชจ๋ธ์˜ repo_id
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
+ embedding_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
20
+ persist_directory = "./chroma_batch_vectors"
21
+ vectorstore = Chroma(persist_directory=persist_directory, embedding_function=embedding_model.encode)
22
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
23
+
24
+ # ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์š”์•ฝ ํ•จ์ˆ˜
25
+ def summarize_results(search_results):
26
+ combined_text = "\n".join([result.page_content for result in search_results])
27
+ summary = summarizer(combined_text, max_length=100, min_length=30, do_sample=False)[0]["summary_text"]
28
+ return summary
29
+
30
+ # ๊ฒ€์ƒ‰ ๋ฐ ์‘๋‹ต ์ƒ์„ฑ ํ•จ์ˆ˜
31
+ def generate_response(user_input):
32
+ # ๊ฒ€์ƒ‰ ๋ฐ ๋งฅ๋ฝ ์ƒ์„ฑ
33
+ search_results = retriever.get_relevant_documents(user_input)
34
+ context = "\n".join([result.page_content for result in search_results])
35
+
36
+ # ๋ชจ๋ธ์— ๋งฅ๋ฝ๊ณผ ์งˆ๋ฌธ ์ „๋‹ฌ
37
+ input_text = f"๋งฅ๋ฝ: {context}\n์งˆ๋ฌธ: {user_input}"
38
+ response = pipe(input_text)[0]["generated_text"]
39
+
40
+ return response
41
+
42
+ # Streamlit ์•ฑ UI
43
+ st.title("์ฑ—๋ด‡ํ…Œ์ŠคํŠธ")
44
+ st.write("Llama 3.2-3B ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ์ฑ—๋ด‡์ž…๋‹ˆ๋‹ค. ์งˆ๋ฌธ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.")
45
+
46
+ # ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๋ฐ›๊ธฐ
47
+ user_input = st.text_input("์งˆ๋ฌธ")
48
+ if user_input:
49
+ response = generate_response(user_input)
50
+ st.write("์ฑ—๋ด‡ ์‘๋‹ต:", response)