Spaces:
Sleeping
Sleeping
Jakaria commited on
Commit ·
d0896fa
1
Parent(s): 8d60d43
Add Bangla model API
Browse files
app.py
CHANGED
|
@@ -1,31 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from config import
|
| 4 |
from ingestion import ingest_pdf
|
| 5 |
from rag import answer, reset_memory
|
| 6 |
|
| 7 |
-
# Prepare runtime folders
|
| 8 |
os.makedirs(DATA_DIR, exist_ok=True)
|
| 9 |
os.makedirs(VECTOR_DIR, exist_ok=True)
|
| 10 |
|
| 11 |
def do_ingest(pdf_file):
|
| 12 |
if GROQ_API_KEY.strip() == "":
|
| 13 |
return "⚠️ GROQ_API_KEY is missing. Add it in your environment / HF Space secrets."
|
| 14 |
-
|
| 15 |
if pdf_file is None:
|
| 16 |
return "Please upload a PDF."
|
| 17 |
|
| 18 |
-
# Save uploaded file
|
| 19 |
with open(UPLOADED_PDF_PATH, "wb") as f:
|
| 20 |
f.write(pdf_file.read())
|
| 21 |
|
| 22 |
-
# Ingest and reset memory
|
| 23 |
msg = ingest_pdf(UPLOADED_PDF_PATH)
|
| 24 |
reset_memory()
|
| 25 |
return f"✅ {msg}"
|
| 26 |
|
| 27 |
def chat_fn(user_message, history):
|
| 28 |
-
# Ensure vector index exists
|
| 29 |
if not os.path.exists(VECTOR_DIR) or not os.listdir(VECTOR_DIR):
|
| 30 |
return "Please upload a PDF and click 'Ingest PDF' first.", history
|
| 31 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from config import DATA_DIR, UPLOADED_PDF_PATH, VECTOR_DIR, GROQ_API_KEY
|
| 4 |
from ingestion import ingest_pdf
|
| 5 |
from rag import answer, reset_memory
|
| 6 |
|
|
|
|
| 7 |
os.makedirs(DATA_DIR, exist_ok=True)
|
| 8 |
os.makedirs(VECTOR_DIR, exist_ok=True)
|
| 9 |
|
| 10 |
def do_ingest(pdf_file):
|
| 11 |
if GROQ_API_KEY.strip() == "":
|
| 12 |
return "⚠️ GROQ_API_KEY is missing. Add it in your environment / HF Space secrets."
|
|
|
|
| 13 |
if pdf_file is None:
|
| 14 |
return "Please upload a PDF."
|
| 15 |
|
|
|
|
| 16 |
with open(UPLOADED_PDF_PATH, "wb") as f:
|
| 17 |
f.write(pdf_file.read())
|
| 18 |
|
|
|
|
| 19 |
msg = ingest_pdf(UPLOADED_PDF_PATH)
|
| 20 |
reset_memory()
|
| 21 |
return f"✅ {msg}"
|
| 22 |
|
| 23 |
def chat_fn(user_message, history):
|
|
|
|
| 24 |
if not os.path.exists(VECTOR_DIR) or not os.listdir(VECTOR_DIR):
|
| 25 |
return "Please upload a PDF and click 'Ingest PDF' first.", history
|
| 26 |
|
config.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
# ---- Paths ----
|
| 4 |
-
DATA_DIR = "data"
|
| 5 |
-
VECTOR_DIR = "vectorstore/faiss"
|
| 6 |
-
UPLOADED_PDF_PATH = os.path.join(DATA_DIR, "uploaded.pdf")
|
| 7 |
|
| 8 |
# ---- Models ----
|
| 9 |
EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
# ---- Paths ----
|
| 4 |
+
DATA_DIR = "data"
|
| 5 |
+
VECTOR_DIR = "vectorstore/faiss"
|
| 6 |
+
UPLOADED_PDF_PATH = os.path.join(DATA_DIR, "uploaded.pdf")
|
| 7 |
|
| 8 |
# ---- Models ----
|
| 9 |
EMBED_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
ingestion.py
CHANGED
|
@@ -6,22 +6,15 @@ from langchain_huggingface import HuggingFaceEmbeddings
|
|
| 6 |
from config import VECTOR_DIR, EMBED_MODEL
|
| 7 |
|
| 8 |
def ingest_pdf(pdf_path: str) -> str:
|
| 9 |
-
"""
|
| 10 |
-
Load PDF, split into chunks, embed, and store in FAISS.
|
| 11 |
-
Overwrites previous FAISS index.
|
| 12 |
-
"""
|
| 13 |
if not os.path.exists(pdf_path):
|
| 14 |
return "PDF file not found."
|
| 15 |
|
| 16 |
-
# Load PDF
|
| 17 |
loader = PyPDFLoader(pdf_path)
|
| 18 |
docs = loader.load()
|
| 19 |
|
| 20 |
-
# Split text
|
| 21 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 22 |
chunks = splitter.split_documents(docs)
|
| 23 |
|
| 24 |
-
# Embeddings + FAISS
|
| 25 |
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 26 |
vectordb = FAISS.from_documents(chunks, embeddings)
|
| 27 |
vectordb.save_local(VECTOR_DIR)
|
|
|
|
| 6 |
from config import VECTOR_DIR, EMBED_MODEL
|
| 7 |
|
| 8 |
def ingest_pdf(pdf_path: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
if not os.path.exists(pdf_path):
|
| 10 |
return "PDF file not found."
|
| 11 |
|
|
|
|
| 12 |
loader = PyPDFLoader(pdf_path)
|
| 13 |
docs = loader.load()
|
| 14 |
|
|
|
|
| 15 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 16 |
chunks = splitter.split_documents(docs)
|
| 17 |
|
|
|
|
| 18 |
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 19 |
vectordb = FAISS.from_documents(chunks, embeddings)
|
| 20 |
vectordb.save_local(VECTOR_DIR)
|
rag.py
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_community.vectorstores import FAISS
|
| 2 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 3 |
+
from langchain.memory import ConversationBufferMemory
|
| 4 |
+
from langchain_groq import ChatGroq
|
| 5 |
+
from langchain.chains import conversational_retrieval
|
| 6 |
+
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 7 |
+
from config import VECTOR_DIR, EMBED_MODEL, GROQ_API_KEY, GROQ_MODEL
|
| 8 |
+
|
| 9 |
+
_memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 10 |
+
|
| 11 |
+
def reset_memory():
|
| 12 |
+
_memory.clear()
|
| 13 |
+
|
| 14 |
+
def build_chain():
|
| 15 |
+
embeddings = HuggingFaceEmbeddings(model_name=EMBED_MODEL)
|
| 16 |
+
vectordb = FAISS.load_local(VECTOR_DIR, embeddings, allow_dangerous_deserialization=True)
|
| 17 |
+
retriever = vectordb.as_retriever(search_kwargs={"k": 3})
|
| 18 |
+
|
| 19 |
+
llm = ChatGroq(model=GROQ_MODEL, api_key=GROQ_API_KEY, temperature=0.1)
|
| 20 |
+
|
| 21 |
+
chat_prompt = ChatPromptTemplate.from_messages([
|
| 22 |
+
SystemMessagePromptTemplate.from_template("You are a helpful assistant. Answer using the context."),
|
| 23 |
+
HumanMessagePromptTemplate.from_template("Context:\n{context}\n\nQuestion:\n{question}\nAnswer clearly and concisely:")
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
chain = conversational_retrieval.from_llm(
|
| 27 |
+
llm=llm,
|
| 28 |
+
retriever=retriever,
|
| 29 |
+
memory=_memory,
|
| 30 |
+
combine_docs_chain_kwargs={"prompt": chat_prompt}
|
| 31 |
+
)
|
| 32 |
+
return chain
|
| 33 |
+
|
| 34 |
+
def answer(question: str) -> str:
|
| 35 |
+
chain = build_chain()
|
| 36 |
+
result = chain.invoke({"question": question})
|
| 37 |
+
return result.get("answer") or result.get("result") or "No answer."
|