File tree Expand file tree Collapse file tree 7 files changed +83
-1
lines changed
Expand file tree Collapse file tree 7 files changed +83
-1
lines changed Original file line number Diff line number Diff line change 1- # doc-ai-bot
1+ """
2+ ## AI-Powered Document Q&A Bot
3+ ## Upload a PDF and ask questions. Uses LangChain, FAISS, and OpenAI.
4+
5+ ## Run Instructions:
6+ 1. Install: `pip install -r requirements.txt`
7+ 2. Run API: `uvicorn app.main:app --reload`
8+ 3. Run Streamlit: `streamlit run streamlit_ui/app.py`
9+ """
Original file line number Diff line number Diff line change 1+ from langchain .chains .question_answering import load_qa_chain
2+ from langchain .llms import OpenAI
3+ from vector_store import search_similar
4+
5+ llm = OpenAI (temperature = 0 )
6+ chain = load_qa_chain (llm , chain_type = "stuff" )
7+
8+
9+ def query_answer (query : str ):
10+ docs = search_similar (query )
11+ return chain .run (input_documents = docs , question = query )
Original file line number Diff line number Diff line change 1+ from fastapi import FastAPI , UploadFile
2+ from llm_pipeline import query_answer
3+ from vector_store import ingest_document
4+
5+ app = FastAPI ()
6+
7+
8+ @app .post ("/upload" )
9+ async def upload_pdf (file : UploadFile ):
10+ content = await file .read ()
11+ ingest_document (content )
12+ return {"status" : "Document ingested." }
13+
14+
15+ @app .get ("/query" )
16+ def query (q : str ):
17+ return {"answer" : query_answer (q )}
Original file line number Diff line number Diff line change 1+ from langchain .vectorstores import FAISS
2+ from langchain .embeddings .openai import OpenAIEmbeddings
3+ from langchain .text_splitter import CharacterTextSplitter
4+ from langchain .document_loaders import PyPDFLoader
5+ import tempfile
6+
7+ embedding = OpenAIEmbeddings ()
8+ vector_db = None
9+
10+
11+ def ingest_document (content : bytes ):
12+ with tempfile .NamedTemporaryFile (delete = False , suffix = ".pdf" ) as tmp :
13+ tmp .write (content )
14+ loader = PyPDFLoader (tmp .name )
15+ docs = loader .load ()
16+ splitter = CharacterTextSplitter (chunk_size = 500 , chunk_overlap = 50 )
17+ chunks = splitter .split_documents (docs )
18+ global vector_db
19+ vector_db = FAISS .from_documents (chunks , embedding )
20+
21+
22+ def search_similar (query : str ):
23+ return vector_db .similarity_search (query , k = 3 ) if vector_db else []
Original file line number Diff line number Diff line change 1+ langchain
2+ openai
3+ faiss-cpu
4+ streamlit
5+ PyMuPDF
6+ uvicorn
7+ fastapi
8+ python-multipart
Original file line number Diff line number Diff line change 1+ import streamlit as st
2+ import requests
3+
4+ st .title ("📄 Ask your Document" )
5+ file = st .file_uploader ("Upload your PDF" )
6+ if file :
7+ response = requests .post (
8+ "http://localhost:8000/upload" , files = {"file" : file })
9+ st .success ("File uploaded" )
10+
11+ query = st .text_input ("Ask a question" )
12+ if query :
13+ answer = requests .get ("http://localhost:8000/query" ,
14+ params = {"q" : query }).json ()
15+ st .write ("Answer:" , answer ["answer" ])
You can’t perform that action at this time.
0 commit comments