Spaces:
Runtime error
Runtime error
Commit
·
0022a58
1
Parent(s):
b122a6c
Adding dockerfile
Browse files- Dockerfile +32 -1
- app.py +6 -24
- pyproject.toml +1 -1
Dockerfile
CHANGED
@@ -1,3 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Get a distribution that has uv already installed
|
2 |
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
|
3 |
|
@@ -26,4 +57,4 @@ RUN uv sync
|
|
26 |
EXPOSE 7860
|
27 |
|
28 |
# Run the app
|
29 |
-
CMD ["uv", "run", "chainlit", "run", "app.py", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
1 |
+
Hugging Face's logo
|
2 |
+
Hugging Face
|
3 |
+
|
4 |
+
Spaces:
|
5 |
+
|
6 |
+
lsy9874205
|
7 |
+
/
|
8 |
+
assignment15
|
9 |
+
|
10 |
+
like
|
11 |
+
0
|
12 |
+
App
|
13 |
+
Files
|
14 |
+
Community
|
15 |
+
assignment15
|
16 |
+
/
|
17 |
+
Dockerfile
|
18 |
+
|
19 |
+
lsy9874205's picture
|
20 |
+
lsy9874205
|
21 |
+
initial files
|
22 |
+
d58f81a
|
23 |
+
raw
|
24 |
+
|
25 |
+
Copy download link
|
26 |
+
history
|
27 |
+
blame
|
28 |
+
contribute
|
29 |
+
delete
|
30 |
+
|
31 |
+
700 Bytes
|
32 |
# Get a distribution that has uv already installed
|
33 |
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim
|
34 |
|
|
|
57 |
EXPOSE 7860
|
58 |
|
59 |
# Run the app
|
60 |
+
CMD ["uv", "run", "chainlit", "run", "app.py", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
@@ -19,7 +19,6 @@ from tqdm.asyncio import tqdm
|
|
19 |
# ---- ENV VARIABLES ---- #
|
20 |
"""
|
21 |
This function will load our environment file (.env) if it is present.
|
22 |
-
|
23 |
NOTE: Make sure that .env is in your .gitignore file - it is by default, but please ensure it remains there.
|
24 |
"""
|
25 |
load_dotenv()
|
@@ -27,14 +26,13 @@ load_dotenv()
|
|
27 |
"""
|
28 |
We will load our environment variables here.
|
29 |
"""
|
30 |
-
HF_LLM_ENDPOINT = os.
|
31 |
-
HF_EMBED_ENDPOINT = os.
|
32 |
-
HF_TOKEN = os.
|
33 |
|
34 |
if not all([HF_LLM_ENDPOINT, HF_EMBED_ENDPOINT, HF_TOKEN]):
|
35 |
raise ValueError("Missing required environment variables. Please check your .env file.")
|
36 |
|
37 |
-
|
38 |
# ---- GLOBAL DECLARATIONS ---- #
|
39 |
|
40 |
# -- RETRIEVAL -- #
|
@@ -44,16 +42,12 @@ if not all([HF_LLM_ENDPOINT, HF_EMBED_ENDPOINT, HF_TOKEN]):
|
|
44 |
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
45 |
4. Index Files if they do not exist, otherwise load the vectorstore
|
46 |
"""
|
47 |
-
|
48 |
-
|
49 |
-
text_loader = TextLoader("./data/paul_graham_essays.txt")
|
50 |
-
documents = text_loader.load()
|
51 |
|
52 |
-
### 2. CREATE TEXT SPLITTER AND SPLIT DOCUMENTS
|
53 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
54 |
split_documents = text_splitter.split_documents(documents)
|
55 |
|
56 |
-
### 3. LOAD HUGGINGFACE EMBEDDINGS
|
57 |
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
58 |
model=HF_EMBED_ENDPOINT,
|
59 |
task="feature-extraction",
|
@@ -117,29 +111,23 @@ hf_retriever = asyncio.run(run())
|
|
117 |
1. Define a String Template
|
118 |
2. Create a Prompt Template from the String Template
|
119 |
"""
|
120 |
-
### 1. DEFINE STRING TEMPLATE
|
121 |
RAG_PROMPT_TEMPLATE = """\
|
122 |
<|start_header_id|>system<|end_header_id|>
|
123 |
You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|>
|
124 |
-
|
125 |
<|start_header_id|>user<|end_header_id|>
|
126 |
User Query:
|
127 |
{query}
|
128 |
-
|
129 |
Context:
|
130 |
{context}<|eot_id|>
|
131 |
-
|
132 |
<|start_header_id|>assistant<|end_header_id|>
|
133 |
"""
|
134 |
|
135 |
-
### 2. CREATE PROMPT TEMPLATE
|
136 |
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
137 |
|
138 |
# -- GENERATION -- #
|
139 |
"""
|
140 |
1. Create a HuggingFaceEndpoint for the LLM
|
141 |
"""
|
142 |
-
### 1. CREATE HUGGINGFACE ENDPOINT FOR LLM
|
143 |
hf_llm = HuggingFaceEndpoint(
|
144 |
endpoint_url=HF_LLM_ENDPOINT,
|
145 |
max_new_tokens=512,
|
@@ -154,7 +142,6 @@ hf_llm = HuggingFaceEndpoint(
|
|
154 |
def rename(original_author: str):
|
155 |
"""
|
156 |
This function can be used to rename the 'author' of a message.
|
157 |
-
|
158 |
In this case, we're overriding the 'Assistant' author to be 'Paul Graham Essay Bot'.
|
159 |
"""
|
160 |
rename_dict = {
|
@@ -166,13 +153,10 @@ def rename(original_author: str):
|
|
166 |
async def start_chat():
|
167 |
"""
|
168 |
This function will be called at the start of every user session.
|
169 |
-
|
170 |
We will build our LCEL RAG chain here, and store it in the user session.
|
171 |
-
|
172 |
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
173 |
"""
|
174 |
|
175 |
-
### BUILD LCEL RAG CHAIN THAT ONLY RETURNS TEXT
|
176 |
lcel_rag_chain = (
|
177 |
{"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
178 |
| rag_prompt | hf_llm
|
@@ -184,16 +168,14 @@ async def start_chat():
|
|
184 |
async def main(message: cl.Message):
|
185 |
"""
|
186 |
This function will be called every time a message is recieved from a session.
|
187 |
-
|
188 |
We will use the LCEL RAG chain to generate a response to the user query.
|
189 |
-
|
190 |
The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
|
191 |
"""
|
192 |
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
193 |
|
194 |
msg = cl.Message(content="")
|
195 |
|
196 |
-
|
197 |
{"query": message.content},
|
198 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
199 |
):
|
|
|
19 |
# ---- ENV VARIABLES ---- #
|
20 |
"""
|
21 |
This function will load our environment file (.env) if it is present.
|
|
|
22 |
NOTE: Make sure that .env is in your .gitignore file - it is by default, but please ensure it remains there.
|
23 |
"""
|
24 |
load_dotenv()
|
|
|
26 |
"""
|
27 |
We will load our environment variables here.
|
28 |
"""
|
29 |
+
HF_LLM_ENDPOINT = os.getenv("HF_LLM_ENDPOINT")
|
30 |
+
HF_EMBED_ENDPOINT = os.getenv("HF_EMBED_ENDPOINT")
|
31 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
32 |
|
33 |
if not all([HF_LLM_ENDPOINT, HF_EMBED_ENDPOINT, HF_TOKEN]):
|
34 |
raise ValueError("Missing required environment variables. Please check your .env file.")
|
35 |
|
|
|
36 |
# ---- GLOBAL DECLARATIONS ---- #
|
37 |
|
38 |
# -- RETRIEVAL -- #
|
|
|
42 |
3. Load HuggingFace Embeddings (remember to use the URL we set above)
|
43 |
4. Index Files if they do not exist, otherwise load the vectorstore
|
44 |
"""
|
45 |
+
document_loader = TextLoader("./data/paul_graham_essays.txt")
|
46 |
+
documents = document_loader.load()
|
|
|
|
|
47 |
|
|
|
48 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=30)
|
49 |
split_documents = text_splitter.split_documents(documents)
|
50 |
|
|
|
51 |
hf_embeddings = HuggingFaceEndpointEmbeddings(
|
52 |
model=HF_EMBED_ENDPOINT,
|
53 |
task="feature-extraction",
|
|
|
111 |
1. Define a String Template
|
112 |
2. Create a Prompt Template from the String Template
|
113 |
"""
|
|
|
114 |
RAG_PROMPT_TEMPLATE = """\
|
115 |
<|start_header_id|>system<|end_header_id|>
|
116 |
You are a helpful assistant. You answer user questions based on provided context. If you can't answer the question with the provided context, say you don't know.<|eot_id|>
|
|
|
117 |
<|start_header_id|>user<|end_header_id|>
|
118 |
User Query:
|
119 |
{query}
|
|
|
120 |
Context:
|
121 |
{context}<|eot_id|>
|
|
|
122 |
<|start_header_id|>assistant<|end_header_id|>
|
123 |
"""
|
124 |
|
|
|
125 |
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
126 |
|
127 |
# -- GENERATION -- #
|
128 |
"""
|
129 |
1. Create a HuggingFaceEndpoint for the LLM
|
130 |
"""
|
|
|
131 |
hf_llm = HuggingFaceEndpoint(
|
132 |
endpoint_url=HF_LLM_ENDPOINT,
|
133 |
max_new_tokens=512,
|
|
|
142 |
def rename(original_author: str):
|
143 |
"""
|
144 |
This function can be used to rename the 'author' of a message.
|
|
|
145 |
In this case, we're overriding the 'Assistant' author to be 'Paul Graham Essay Bot'.
|
146 |
"""
|
147 |
rename_dict = {
|
|
|
153 |
async def start_chat():
|
154 |
"""
|
155 |
This function will be called at the start of every user session.
|
|
|
156 |
We will build our LCEL RAG chain here, and store it in the user session.
|
|
|
157 |
The user session is a dictionary that is unique to each user session, and is stored in the memory of the server.
|
158 |
"""
|
159 |
|
|
|
160 |
lcel_rag_chain = (
|
161 |
{"context": itemgetter("query") | hf_retriever, "query": itemgetter("query")}
|
162 |
| rag_prompt | hf_llm
|
|
|
168 |
async def main(message: cl.Message):
|
169 |
"""
|
170 |
This function will be called every time a message is recieved from a session.
|
|
|
171 |
We will use the LCEL RAG chain to generate a response to the user query.
|
|
|
172 |
The LCEL RAG chain is stored in the user session, and is unique to each user session - this is why we can access it here.
|
173 |
"""
|
174 |
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
175 |
|
176 |
msg = cl.Message(content="")
|
177 |
|
178 |
+
for chunk in await cl.make_async(lcel_rag_chain.stream)(
|
179 |
{"query": message.content},
|
180 |
config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
181 |
):
|
pyproject.toml
CHANGED
@@ -19,4 +19,4 @@ dependencies = [
|
|
19 |
"jupyter>=1.1.1",
|
20 |
"faiss-cpu>=1.10.0",
|
21 |
"websockets>=15.0",
|
22 |
-
]
|
|
|
19 |
"jupyter>=1.1.1",
|
20 |
"faiss-cpu>=1.10.0",
|
21 |
"websockets>=15.0",
|
22 |
+
]
|