Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from groq import Groq | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain_community.vectorstores import FAISS | |
| from langchain_community.embeddings import SentenceTransformerEmbeddings | |
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| # ---------------- CONFIG ---------------- | |
| GROQ_API_KEY = os.getenv("GROQ_API_KEY") | |
| client = Groq(api_key=GROQ_API_KEY) | |
| embeddings = SentenceTransformerEmbeddings( | |
| model_name="all-MiniLM-L6-v2" | |
| ) | |
| vector_store = None | |
| chat_history = [] | |
| # ---------------- FUNCTIONS ---------------- | |
| def process_pdfs(files): | |
| global vector_store | |
| docs = [] | |
| for file in files: | |
| loader = PyPDFLoader(file.name) | |
| docs.extend(loader.load()) | |
| splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=800, | |
| chunk_overlap=150 | |
| ) | |
| chunks = splitter.split_documents(docs) | |
| vector_store = FAISS.from_documents(chunks, embeddings) | |
| return "✅ PDFs processed successfully!" | |
| def ask_question(question): | |
| global chat_history, vector_store | |
| try: | |
| # ---------- Check 1: PDFs processed ---------- | |
| if vector_store is None: | |
| return "❌ Please upload and process PDF files first." | |
| if question.strip() == "": | |
| return "❌ Please enter a valid question." | |
| # ---------- Retrieve documents ---------- | |
| docs = vector_store.similarity_search(question, k=4) | |
| if not docs: | |
| return "⚠️ No relevant information found in the documents." | |
| context = "" | |
| sources = [] | |
| for d in docs: | |
| context += d.page_content + "\n" | |
| page = d.metadata.get("page", "N/A") | |
| sources.append(f"Page {page + 1}" if page != "N/A" else "Unknown page") | |
| # ---------- Prompt ---------- | |
| prompt = f""" | |
| You are a helpful assistant. | |
| Answer ONLY using the context below. | |
| Context: | |
| {context} | |
| Question: | |
| {question} | |
| """ | |
| # ---------- Groq API ---------- | |
| response = client.chat.completions.create( | |
| model="llama-3.1-8b-instant", | |
| messages=[ | |
| {"role": "system", "content": "Answer strictly from the documents."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| # ---------- Safe response extraction ---------- | |
| answer = response.choices[0].message.content.strip() | |
| final_answer = f"{answer}\n\n📄 Sources: {', '.join(set(sources))}" | |
| chat_history.append((question, final_answer)) | |
| return final_answer | |
| except Exception as e: | |
| return f"❌ Error occurred: {str(e)}" | |
| # ---------------- UI ---------------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 📘Kashif's Enhanced RAG-Based Chatbot") | |
| with gr.Row(): | |
| pdfs = gr.File(file_types=[".pdf"], file_count="multiple") | |
| upload_btn = gr.Button("Process PDFs") | |
| status = gr.Textbox() | |
| upload_btn.click(process_pdfs, pdfs, status) | |
| question = gr.Textbox(label="Ask a Question") | |
| answer = gr.Textbox(label="Answer") | |
| ask_btn = gr.Button("Ask") | |
| ask_btn.click(ask_question, question, answer) | |
| demo.launch() | |