Spaces:
Runtime error
Runtime error
reduced to 3b
Browse files- app.py +16 -23
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,20 +1,18 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
from langchain_text_splitters import Language, RecursiveCharacterTextSplitter
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
from langchain_community.vectorstores import FAISS
|
6 |
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
|
7 |
from langchain.memory import ConversationBufferMemory
|
8 |
from langchain.chains import ConversationalRetrievalChain
|
9 |
-
from transformers import pipeline
|
10 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
11 |
-
import torch
|
12 |
|
13 |
|
14 |
|
15 |
-
gpt_model = 'gpt-4-1106-preview'
|
16 |
-
embedding_model = 'text-embedding-3-small'
|
17 |
-
default_model_id = "bigcode/starcoder2-
|
18 |
|
19 |
def init():
|
20 |
if "conversation" not in st.session_state:
|
@@ -23,12 +21,13 @@ def init():
|
|
23 |
st.session_state.chat_history = None
|
24 |
|
25 |
def init_llm_pipeline(model_id):
|
26 |
-
if "llm" not in st.session_state:
|
27 |
-
|
28 |
model = AutoModelForCausalLM.from_pretrained(
|
29 |
model_id,
|
30 |
-
device_map="auto"
|
31 |
-
)
|
|
|
32 |
tokenizer.add_eos_token = True
|
33 |
tokenizer.pad_token_id = 0
|
34 |
tokenizer.padding_side = "left"
|
@@ -37,21 +36,17 @@ def init_llm_pipeline(model_id):
|
|
37 |
model=model,
|
38 |
tokenizer=tokenizer,
|
39 |
task="text-generation",
|
40 |
-
|
41 |
-
repetition_penalty=1.1,
|
42 |
-
return_full_text=True,
|
43 |
-
max_new_tokens=300,
|
44 |
)
|
45 |
st.session_state.llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
|
46 |
|
47 |
-
def
|
48 |
-
|
49 |
-
|
50 |
-
def get_vectorstore(documents):
|
51 |
python_splitter = RecursiveCharacterTextSplitter.from_language(
|
52 |
language=Language.PYTHON, chunk_size=2000, chunk_overlap=200
|
53 |
)
|
54 |
-
|
|
|
55 |
|
56 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
57 |
|
@@ -83,7 +78,6 @@ def handle_user_input(question):
|
|
83 |
st.write(message.content)
|
84 |
|
85 |
def main():
|
86 |
-
#load_dotenv()
|
87 |
init()
|
88 |
|
89 |
st.set_page_config(page_title="Coding-Assistent", page_icon=":books:")
|
@@ -103,9 +97,8 @@ def main():
|
|
103 |
if st.button("Hochladen"):
|
104 |
with st.spinner("Analysiere Dokumente ..."):
|
105 |
init_llm_pipeline(model_id)
|
106 |
-
|
107 |
-
|
108 |
-
st.session_state.conversation = get_conversation(vectorstore)
|
109 |
|
110 |
|
111 |
if __name__ == "__main__":
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
from langchain_text_splitters import Language, RecursiveCharacterTextSplitter
|
4 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
5 |
from langchain_community.vectorstores import FAISS
|
6 |
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
|
7 |
from langchain.memory import ConversationBufferMemory
|
8 |
from langchain.chains import ConversationalRetrievalChain
|
|
|
9 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
10 |
|
11 |
|
12 |
|
13 |
+
# gpt_model = 'gpt-4-1106-preview'
|
14 |
+
# embedding_model = 'text-embedding-3-small'
|
15 |
+
default_model_id = "bigcode/starcoder2-3b"
|
16 |
|
17 |
def init():
|
18 |
if "conversation" not in st.session_state:
|
|
|
21 |
st.session_state.chat_history = None
|
22 |
|
23 |
def init_llm_pipeline(model_id):
|
24 |
+
if "llm" not in st.session_state:
|
25 |
+
|
26 |
model = AutoModelForCausalLM.from_pretrained(
|
27 |
model_id,
|
28 |
+
device_map="auto"
|
29 |
+
)
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
31 |
tokenizer.add_eos_token = True
|
32 |
tokenizer.pad_token_id = 0
|
33 |
tokenizer.padding_side = "left"
|
|
|
36 |
model=model,
|
37 |
tokenizer=tokenizer,
|
38 |
task="text-generation",
|
39 |
+
max_new_tokens=1024
|
|
|
|
|
|
|
40 |
)
|
41 |
st.session_state.llm = HuggingFacePipeline(pipeline=text_generation_pipeline)
|
42 |
|
43 |
+
def get_retriever(files):
|
44 |
+
documents = [doc.getvalue().decode("utf-8") for doc in files]
|
|
|
|
|
45 |
python_splitter = RecursiveCharacterTextSplitter.from_language(
|
46 |
language=Language.PYTHON, chunk_size=2000, chunk_overlap=200
|
47 |
)
|
48 |
+
|
49 |
+
texts = python_splitter.create_documents(documents)
|
50 |
|
51 |
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
52 |
|
|
|
78 |
st.write(message.content)
|
79 |
|
80 |
def main():
|
|
|
81 |
init()
|
82 |
|
83 |
st.set_page_config(page_title="Coding-Assistent", page_icon=":books:")
|
|
|
97 |
if st.button("Hochladen"):
|
98 |
with st.spinner("Analysiere Dokumente ..."):
|
99 |
init_llm_pipeline(model_id)
|
100 |
+
retriever = get_retriever(upload_docs)
|
101 |
+
st.session_state.conversation = get_conversation(retriever)
|
|
|
102 |
|
103 |
|
104 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
@@ -7,5 +7,5 @@ huggingface-hub
|
|
7 |
torch
|
8 |
accelerate
|
9 |
langchain-text-splitters
|
10 |
-
sentence_transformers
|
11 |
git+https://github.com/huggingface/transformers.git
|
|
|
7 |
torch
|
8 |
accelerate
|
9 |
langchain-text-splitters
|
10 |
+
sentence_transformers
|
11 |
git+https://github.com/huggingface/transformers.git
|