Spaces:
Paused
Paused
Daniel Marques
commited on
Commit
•
d5863e7
1
Parent(s):
19e75c0
feat: add save document route
Browse files- appv1.py +1 -3
- main.py +41 -23
- requirements.txt +1 -0
appv1.py
CHANGED
@@ -26,8 +26,6 @@ else:
|
|
26 |
DEVICE_TYPE = "cpu"
|
27 |
|
28 |
SHOW_SOURCES = True
|
29 |
-
logging.info(f"Running on: {DEVICE_TYPE}")
|
30 |
-
logging.info(f"Display Source Documents set to: {SHOW_SOURCES}")
|
31 |
|
32 |
EMBEDDINGS = HuggingFaceInstructEmbeddings(model_name=EMBEDDING_MODEL_NAME, model_kwargs={"device": DEVICE_TYPE})
|
33 |
|
@@ -182,4 +180,4 @@ def prompt_route():
|
|
182 |
|
183 |
|
184 |
if __name__ == "__main__":
|
185 |
-
app.run(host="0.0.0.0", port=5110)
|
|
|
26 |
DEVICE_TYPE = "cpu"
|
27 |
|
28 |
SHOW_SOURCES = True
|
|
|
|
|
29 |
|
30 |
EMBEDDINGS = HuggingFaceInstructEmbeddings(model_name=EMBEDDING_MODEL_NAME, model_kwargs={"device": DEVICE_TYPE})
|
31 |
|
|
|
180 |
|
181 |
|
182 |
if __name__ == "__main__":
|
183 |
+
app.run(host="0.0.0.0", port=5110)
|
main.py
CHANGED
@@ -1,7 +1,8 @@
|
|
1 |
from typing import Union
|
2 |
-
from fastapi import FastAPI, HTTPException, UploadFile
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
|
|
|
5 |
from pydantic import BaseModel
|
6 |
import pickle
|
7 |
import uvicorn
|
@@ -26,16 +27,16 @@ from werkzeug.utils import secure_filename
|
|
26 |
|
27 |
from constants import CHROMA_SETTINGS, EMBEDDING_MODEL_NAME, PERSIST_DIRECTORY, MODEL_ID, MODEL_BASENAME
|
28 |
|
29 |
-
if torch.backends.mps.is_available():
|
30 |
-
|
31 |
-
elif torch.cuda.is_available():
|
32 |
-
|
33 |
-
else:
|
34 |
-
|
|
|
|
|
35 |
|
36 |
SHOW_SOURCES = True
|
37 |
-
logging.info(f"Running on: {DEVICE_TYPE}")
|
38 |
-
logging.info(f"Display Source Documents set to: {SHOW_SOURCES}")
|
39 |
|
40 |
EMBEDDINGS = HuggingFaceInstructEmbeddings(model_name=EMBEDDING_MODEL_NAME, model_kwargs={"device": DEVICE_TYPE})
|
41 |
|
@@ -51,7 +52,14 @@ RETRIEVER = DB.as_retriever()
|
|
51 |
LLM = load_model(device_type=DEVICE_TYPE, model_id=MODEL_ID, model_basename=MODEL_BASENAME)
|
52 |
prompt, memory = get_prompt_template(promptTemplate_type="llama", history=False)
|
53 |
|
54 |
-
template = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
{context}
|
56 |
Question: {question}
|
57 |
Helpful Answer:"""
|
@@ -149,19 +157,29 @@ def run_ingest_route():
|
|
149 |
|
150 |
@api_app.post("/save_document/")
|
151 |
async def create_upload_file(file: Union[UploadFile, None] = None):
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
160 |
|
161 |
-
|
162 |
-
|
163 |
|
164 |
-
|
165 |
-
|
166 |
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from typing import Union
|
2 |
+
from fastapi import FastAPI, HTTPException, UploadFile, WebSocket
|
3 |
from fastapi.staticfiles import StaticFiles
|
4 |
|
5 |
+
|
6 |
from pydantic import BaseModel
|
7 |
import pickle
|
8 |
import uvicorn
|
|
|
27 |
|
28 |
from constants import CHROMA_SETTINGS, EMBEDDING_MODEL_NAME, PERSIST_DIRECTORY, MODEL_ID, MODEL_BASENAME
|
29 |
|
30 |
+
# if torch.backends.mps.is_available():
|
31 |
+
# DEVICE_TYPE = "mps"
|
32 |
+
# elif torch.cuda.is_available():
|
33 |
+
# DEVICE_TYPE = "cuda"
|
34 |
+
# else:
|
35 |
+
# DEVICE_TYPE = "cpu"
|
36 |
+
|
37 |
+
DEVICE_TYPE = "cuda"
|
38 |
|
39 |
SHOW_SOURCES = True
|
|
|
|
|
40 |
|
41 |
EMBEDDINGS = HuggingFaceInstructEmbeddings(model_name=EMBEDDING_MODEL_NAME, model_kwargs={"device": DEVICE_TYPE})
|
42 |
|
|
|
52 |
LLM = load_model(device_type=DEVICE_TYPE, model_id=MODEL_ID, model_basename=MODEL_BASENAME)
|
53 |
prompt, memory = get_prompt_template(promptTemplate_type="llama", history=False)
|
54 |
|
55 |
+
template = """you are a helpful, respectful and honest assistant.
|
56 |
+
Your name is Katara llma. You should only use the source documents provided to answer the questions.
|
57 |
+
You should only respond only topics that contains in documents use to training.
|
58 |
+
Use the following pieces of context to answer the question at the end.
|
59 |
+
Always answer in the most helpful and safe way possible.
|
60 |
+
If you don't know the answer to a question, just say that you don't know, don't try to make up an answer, don't share false information.
|
61 |
+
Use 15 sentences maximum. Keep the answer as concise as possible.
|
62 |
+
Always say "thanks for asking!" at the end of the answer.
|
63 |
{context}
|
64 |
Question: {question}
|
65 |
Helpful Answer:"""
|
|
|
157 |
|
158 |
@api_app.post("/save_document/")
|
159 |
async def create_upload_file(file: Union[UploadFile, None] = None):
|
160 |
+
try:
|
161 |
+
if not file:
|
162 |
+
raise HTTPException(status_code=400, detail="No upload file sent")
|
163 |
+
else:
|
164 |
+
if file.filename == "":
|
165 |
+
raise HTTPException(status_code=400, detail="No selected file")
|
166 |
+
if file:
|
167 |
+
filename = secure_filename(file.filename)
|
168 |
+
folder_path = "SOURCE_DOCUMENTS"
|
169 |
|
170 |
+
if not os.path.exists(folder_path):
|
171 |
+
os.makedirs(folder_path)
|
172 |
|
173 |
+
file_path = os.path.join(folder_path, filename)
|
174 |
+
file.save(file_path)
|
175 |
|
176 |
+
return {"response": "File saved successfully"}
|
177 |
+
except Exception as e:
|
178 |
+
raise HTTPException(status_code=400, detail=e)
|
179 |
+
|
180 |
+
@api_app.websocket("/ws")
|
181 |
+
async def websocket_endpoint(websocket: WebSocket):
|
182 |
+
await websocket.accept()
|
183 |
+
while True:
|
184 |
+
data = await websocket.receive_text()
|
185 |
+
await websocket.send_text(f"Message text was: {data}")
|
requirements.txt
CHANGED
@@ -28,6 +28,7 @@ flask
|
|
28 |
requests
|
29 |
uvicorn
|
30 |
fastapi
|
|
|
31 |
pydantic
|
32 |
|
33 |
# Streamlit related
|
|
|
28 |
requests
|
29 |
uvicorn
|
30 |
fastapi
|
31 |
+
websockets
|
32 |
pydantic
|
33 |
|
34 |
# Streamlit related
|