Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,12 +1,21 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
import tempfile
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
-
from
|
|
|
|
| 9 |
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
|
@@ -15,7 +24,7 @@ async def home():
|
|
| 15 |
return "API Server Running"
|
| 16 |
|
| 17 |
@app.post('/PromptBuddy')
|
| 18 |
-
async def PromptLLM(file: UploadFile = File(...)):
|
| 19 |
|
| 20 |
with tempfile.NamedTemporaryFile(delete=False) as temp_file: # Create temporary file
|
| 21 |
temp_file_path = temp_file.name
|
|
@@ -23,18 +32,25 @@ async def PromptLLM(file: UploadFile = File(...)):
|
|
| 23 |
shutil.copyfileobj(file.file, f)
|
| 24 |
|
| 25 |
|
| 26 |
-
loader =
|
| 27 |
data = loader.load()
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
template = """Summarise the report {pages}"""
|
| 33 |
-
prompt_template = PromptTemplate(input_variables=["pages"], template=template)
|
| 34 |
-
chain = LLMChain(llm=llm, prompt=prompt_template)
|
| 35 |
|
| 36 |
-
result = chain.run(pages=data[0].page_content)
|
| 37 |
-
|
| 38 |
-
return result
|
| 39 |
|
| 40 |
|
|
|
|
| 1 |
+
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
| 9 |
import os
|
| 10 |
import shutil
|
| 11 |
import tempfile
|
| 12 |
+
from langchain_community.document_loaders import PyMuPDFLoader
|
| 13 |
+
from LoadLLM import Loadllm
|
| 14 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 15 |
+
from langchain_community.vectorstores import FAISS
|
| 16 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 17 |
|
| 18 |
+
DB_FAISS_PATH = 'vectorstore/db_faiss'
|
| 19 |
|
| 20 |
app = FastAPI()
|
| 21 |
|
|
|
|
| 24 |
return "API Server Running"
|
| 25 |
|
| 26 |
@app.post('/PromptBuddy')
|
| 27 |
+
async def PromptLLM(file: UploadFile = File(...), query: str = Form(...)):
|
| 28 |
|
| 29 |
with tempfile.NamedTemporaryFile(delete=False) as temp_file: # Create temporary file
|
| 30 |
temp_file_path = temp_file.name
|
|
|
|
| 32 |
shutil.copyfileobj(file.file, f)
|
| 33 |
|
| 34 |
|
| 35 |
+
loader = PyMuPDFLoader(file_path= temp_file_path)
|
| 36 |
data = loader.load()
|
| 37 |
|
| 38 |
+
# Create embeddings using Sentence Transformers
|
| 39 |
+
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
| 40 |
+
|
| 41 |
+
# Create a FAISS vector store and save embeddings
|
| 42 |
+
db = FAISS.from_documents(data, embeddings)
|
| 43 |
+
db.save_local(DB_FAISS_PATH)
|
| 44 |
+
|
| 45 |
+
# Load the language model
|
| 46 |
+
llm = Loadllm.load_llm()
|
| 47 |
+
|
| 48 |
+
# Create a conversational chain
|
| 49 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=db.as_retriever())
|
| 50 |
+
|
| 51 |
+
result = chain({"question": query, "chat_history": ''})
|
| 52 |
+
return result['answer']
|
| 53 |
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
|