Upload 4 files
Browse files- Dockerfile +10 -0
- app.py +31 -0
- requirements.txt +8 -0
- upload_pdf.py +26 -0
Dockerfile
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.10
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
COPY . .
|
5 |
+
|
6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
7 |
+
|
8 |
+
ENV PORT=7860
|
9 |
+
|
10 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, Query
|
2 |
+
from langchain_community.vectorstores import Chroma
|
3 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
4 |
+
from together import Together
|
5 |
+
import os
|
6 |
+
|
7 |
+
embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
8 |
+
vectordb = Chroma(persist_directory="chroma_db", embedding_function=embedding)
|
9 |
+
|
10 |
+
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY", "")
|
11 |
+
client = Together(api_key=TOGETHER_API_KEY)
|
12 |
+
|
13 |
+
def call_llama(prompt: str):
|
14 |
+
response = client.chat.completions.create(
|
15 |
+
model="meta-llama/Llama-3-8b-chat-hf",
|
16 |
+
messages=[
|
17 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
18 |
+
{"role": "user", "content": prompt}
|
19 |
+
]
|
20 |
+
)
|
21 |
+
return response.choices[0].message.content
|
22 |
+
|
23 |
+
app = FastAPI()
|
24 |
+
|
25 |
+
@app.get("/ask")
|
26 |
+
async def ask(q: str = Query(..., description="Your question")):
|
27 |
+
docs = vectordb.similarity_search(q, k=3)
|
28 |
+
context = "\n".join([doc.page_content for doc in docs])
|
29 |
+
final_prompt = f"Use the context below to answer the question.\n\nContext:\n{context}\n\nQuestion: {q}"
|
30 |
+
answer = call_llama(final_prompt)
|
31 |
+
return {"answer": answer}
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
langchain
|
4 |
+
together
|
5 |
+
chromadb
|
6 |
+
sentence-transformers
|
7 |
+
langchain-community
|
8 |
+
pymupdf
|
upload_pdf.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from fastapi import FastAPI, File, UploadFile
|
3 |
+
from langchain_community.document_loaders import PyMuPDFLoader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
6 |
+
from langchain_community.vectorstores import Chroma
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
@app.post("/upload")
|
11 |
+
async def upload_pdf(file: UploadFile = File(...)):
|
12 |
+
file_location = f"uploads/{file.filename}"
|
13 |
+
with open(file_location, "wb") as f:
|
14 |
+
f.write(await file.read())
|
15 |
+
|
16 |
+
loader = PyMuPDFLoader(file_location)
|
17 |
+
docs = loader.load()
|
18 |
+
|
19 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
20 |
+
chunks = splitter.split_documents(docs)
|
21 |
+
|
22 |
+
embedding = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
23 |
+
vectordb = Chroma.from_documents(chunks, embedding, persist_directory="chroma_db")
|
24 |
+
vectordb.persist()
|
25 |
+
|
26 |
+
return {"message": f"✅ {file.filename} uploaded and processed into vector DB."}
|