Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from pydantic import BaseModel
|
| 4 |
-
import shutil
|
| 5 |
import os
|
| 6 |
import requests
|
| 7 |
from langchain_community.document_loaders import PyPDFLoader
|
|
@@ -10,33 +7,34 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
|
|
| 10 |
from langchain_community.vectorstores import Chroma
|
| 11 |
from langchain.chains import RetrievalQA
|
| 12 |
|
| 13 |
-
#
|
| 14 |
HF_API_URL = "https://api-inference.huggingface.co/models/your-username/your-model"
|
| 15 |
HF_API_TOKEN = "hf_xxxYOUR_TOKENxxx" # Replace with your HF access token
|
| 16 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
app.add_middleware(
|
| 21 |
-
CORSMiddleware,
|
| 22 |
-
allow_origins=["*"],
|
| 23 |
-
allow_methods=["*"],
|
| 24 |
-
allow_headers=["*"],
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
UPLOAD_DIR = "./uploads"
|
| 28 |
-
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 29 |
-
|
| 30 |
qa = None
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
global qa
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
loader = PyPDFLoader(
|
| 40 |
documents = loader.load()
|
| 41 |
|
| 42 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
@@ -46,41 +44,41 @@ def process_pdf(pdf_file_path):
|
|
| 46 |
docs = text_splitter.split_documents(documents)
|
| 47 |
|
| 48 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 49 |
-
vectorstore = Chroma.from_documents(docs, embeddings
|
| 50 |
|
| 51 |
-
# RetrievalQA
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
response = requests.post(HF_API_URL, headers=headers, json=payload)
|
| 55 |
-
if response.status_code == 200:
|
| 56 |
-
output = response.json()
|
| 57 |
-
# HF API response format varies; usually output[0]["generated_text"]
|
| 58 |
-
return output[0]["generated_text"]
|
| 59 |
-
else:
|
| 60 |
-
return f"Error: {response.status_code} - {response.text}"
|
| 61 |
|
| 62 |
qa = RetrievalQA.from_chain_type(
|
| 63 |
-
llm=
|
| 64 |
retriever=vectorstore.as_retriever()
|
| 65 |
)
|
| 66 |
|
| 67 |
-
return "
|
| 68 |
-
|
| 69 |
-
# ---------------- Endpoints ----------------
|
| 70 |
-
@app.post("/upload_pdf")
|
| 71 |
-
async def upload_pdf(file: UploadFile = File(...)):
|
| 72 |
-
file_path = os.path.join(UPLOAD_DIR, file.filename)
|
| 73 |
-
with open(file_path, "wb") as f:
|
| 74 |
-
shutil.copyfileobj(file.file, f)
|
| 75 |
-
|
| 76 |
-
msg = process_pdf(file_path)
|
| 77 |
-
return {"message": msg}
|
| 78 |
|
| 79 |
-
|
| 80 |
-
def ask_question(request: QueryRequest):
|
| 81 |
global qa
|
| 82 |
if qa is None:
|
| 83 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader
|
|
|
|
| 7 |
from langchain_community.vectorstores import Chroma
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
|
| 10 |
+
# ---------------- HF Model API Setup ----------------
|
| 11 |
HF_API_URL = "https://api-inference.huggingface.co/models/your-username/your-model"
|
| 12 |
HF_API_TOKEN = "hf_xxxYOUR_TOKENxxx" # Replace with your HF access token
|
| 13 |
headers = {"Authorization": f"Bearer {HF_API_TOKEN}"}
|
| 14 |
|
| 15 |
+
# Global QA object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
qa = None
|
| 17 |
+
UPLOAD_FOLDER = "uploads"
|
| 18 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 19 |
|
| 20 |
+
# ---------------- Functions ----------------
|
| 21 |
+
def hf_llm_call(prompt: str):
|
| 22 |
+
"""Call HF hosted model API"""
|
| 23 |
+
payload = {"inputs": prompt}
|
| 24 |
+
response = requests.post(HF_API_URL, headers=headers, json=payload)
|
| 25 |
+
if response.status_code == 200:
|
| 26 |
+
output = response.json()
|
| 27 |
+
return output[0]["generated_text"]
|
| 28 |
+
else:
|
| 29 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 30 |
|
| 31 |
+
def process_pdf(pdf_file):
|
| 32 |
+
"""Load PDF, create embeddings, setup RetrievalQA with HF API"""
|
| 33 |
global qa
|
| 34 |
+
file_path = os.path.join(UPLOAD_FOLDER, pdf_file.name)
|
| 35 |
+
pdf_file.save(file_path)
|
| 36 |
|
| 37 |
+
loader = PyPDFLoader(file_path)
|
| 38 |
documents = loader.load()
|
| 39 |
|
| 40 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
|
|
| 44 |
docs = text_splitter.split_documents(documents)
|
| 45 |
|
| 46 |
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
| 47 |
+
vectorstore = Chroma.from_documents(docs, embeddings)
|
| 48 |
|
| 49 |
+
# Setup RetrievalQA but use HF API as LLM
|
| 50 |
+
def llm_wrapper(prompt):
|
| 51 |
+
return hf_llm_call(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
qa = RetrievalQA.from_chain_type(
|
| 54 |
+
llm=llm_wrapper,
|
| 55 |
retriever=vectorstore.as_retriever()
|
| 56 |
)
|
| 57 |
|
| 58 |
+
return f"{pdf_file.name} uploaded and processed successfully!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
def ask_question(query):
|
|
|
|
| 61 |
global qa
|
| 62 |
if qa is None:
|
| 63 |
+
return "No PDF uploaded yet!"
|
| 64 |
+
return qa.run(query)
|
| 65 |
+
|
| 66 |
+
# ---------------- Gradio UI ----------------
|
| 67 |
+
with gr.Blocks() as demo:
|
| 68 |
+
gr.Markdown("## PDF QA System using HF Hosted Model")
|
| 69 |
+
|
| 70 |
+
with gr.Row():
|
| 71 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 72 |
+
upload_btn = gr.Button("Upload & Process PDF")
|
| 73 |
+
output_msg = gr.Textbox(label="Status", interactive=False)
|
| 74 |
|
| 75 |
+
upload_btn.click(fn=process_pdf, inputs=pdf_input, outputs=output_msg)
|
| 76 |
+
|
| 77 |
+
query_input = gr.Textbox(label="Ask a question")
|
| 78 |
+
query_btn = gr.Button("Get Answer")
|
| 79 |
+
answer_output = gr.Textbox(label="Answer", interactive=False)
|
| 80 |
+
|
| 81 |
+
query_btn.click(fn=ask_question, inputs=query_input, outputs=answer_output)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|