Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import gradio as gr
|
|
| 3 |
import fitz # PyMuPDF
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
-
from
|
| 7 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
from langchain_core.prompts import ChatPromptTemplate
|
| 9 |
from langchain_core.output_parsers import StrOutputParser
|
|
@@ -30,7 +30,7 @@ def create_vectorstore(text):
|
|
| 30 |
return vectorstore
|
| 31 |
|
| 32 |
# ----------------------------------------------------
|
| 33 |
-
# 3️⃣ Initialize Google Gemini Model
|
| 34 |
# ----------------------------------------------------
|
| 35 |
def get_model():
|
| 36 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY", "")
|
|
@@ -43,16 +43,16 @@ def chat_with_pdf(pdf_file, user_input, history):
|
|
| 43 |
if pdf_file is None:
|
| 44 |
return history + [["❌ Please upload a PDF file first.", ""]]
|
| 45 |
|
| 46 |
-
#
|
| 47 |
pdf_text = extract_text_from_pdf(pdf_file)
|
| 48 |
vectorstore = create_vectorstore(pdf_text)
|
| 49 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 50 |
|
| 51 |
-
#
|
| 52 |
docs = retriever.get_relevant_documents(user_input)
|
| 53 |
context = "\n\n".join([d.page_content for d in docs])
|
| 54 |
|
| 55 |
-
#
|
| 56 |
prompt = ChatPromptTemplate.from_template(
|
| 57 |
"""
|
| 58 |
You are a helpful Urdu assistant. Answer in Urdu (Roman Urdu is fine if needed).
|
|
@@ -70,7 +70,7 @@ def chat_with_pdf(pdf_file, user_input, history):
|
|
| 70 |
chain = prompt | llm | StrOutputParser()
|
| 71 |
answer = chain.invoke({"context": context, "question": user_input})
|
| 72 |
|
| 73 |
-
#
|
| 74 |
tts = gTTS(answer, lang="ur")
|
| 75 |
tts.save("response.mp3")
|
| 76 |
|
|
@@ -78,7 +78,7 @@ def chat_with_pdf(pdf_file, user_input, history):
|
|
| 78 |
return history, "response.mp3"
|
| 79 |
|
| 80 |
# ----------------------------------------------------
|
| 81 |
-
# 5️⃣ Gradio
|
| 82 |
# ----------------------------------------------------
|
| 83 |
with gr.Blocks(title="📘 Urdu RAG Chatbot") as demo:
|
| 84 |
gr.Markdown("## 🤖 Urdu RAG Chatbot — Ask questions from your PDF (Roman Urdu supported)")
|
|
|
|
| 3 |
import fitz # PyMuPDF
|
| 4 |
from langchain_community.vectorstores import FAISS
|
| 5 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 6 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 8 |
from langchain_core.prompts import ChatPromptTemplate
|
| 9 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
| 30 |
return vectorstore
|
| 31 |
|
| 32 |
# ----------------------------------------------------
|
| 33 |
+
# 3️⃣ Initialize Google Gemini Model
|
| 34 |
# ----------------------------------------------------
|
| 35 |
def get_model():
|
| 36 |
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY", "")
|
|
|
|
| 43 |
if pdf_file is None:
|
| 44 |
return history + [["❌ Please upload a PDF file first.", ""]]
|
| 45 |
|
| 46 |
+
# Extract and create FAISS
|
| 47 |
pdf_text = extract_text_from_pdf(pdf_file)
|
| 48 |
vectorstore = create_vectorstore(pdf_text)
|
| 49 |
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
|
| 50 |
|
| 51 |
+
# Retrieve relevant chunks
|
| 52 |
docs = retriever.get_relevant_documents(user_input)
|
| 53 |
context = "\n\n".join([d.page_content for d in docs])
|
| 54 |
|
| 55 |
+
# Generate Answer
|
| 56 |
prompt = ChatPromptTemplate.from_template(
|
| 57 |
"""
|
| 58 |
You are a helpful Urdu assistant. Answer in Urdu (Roman Urdu is fine if needed).
|
|
|
|
| 70 |
chain = prompt | llm | StrOutputParser()
|
| 71 |
answer = chain.invoke({"context": context, "question": user_input})
|
| 72 |
|
| 73 |
+
# Text-to-speech in Urdu
|
| 74 |
tts = gTTS(answer, lang="ur")
|
| 75 |
tts.save("response.mp3")
|
| 76 |
|
|
|
|
| 78 |
return history, "response.mp3"
|
| 79 |
|
| 80 |
# ----------------------------------------------------
|
| 81 |
+
# 5️⃣ Gradio Interface
|
| 82 |
# ----------------------------------------------------
|
| 83 |
with gr.Blocks(title="📘 Urdu RAG Chatbot") as demo:
|
| 84 |
gr.Markdown("## 🤖 Urdu RAG Chatbot — Ask questions from your PDF (Roman Urdu supported)")
|