Spaces:
Sleeping
Sleeping
| import fitz # PyMuPDF | |
| import numpy as np | |
| import faiss | |
| import requests | |
| import os | |
| from sentence_transformers import SentenceTransformer | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| import gradio as gr | |
| # 🔹 Step 1: PDF File Path from Hugging Face local space | |
| pdf_path = "our_philosophy-_falsafatuna (1).pdf" # Must be uploaded to "Files and versions" in your Space | |
| # 🔹 Step 2: Extract Text from PDF | |
| doc = fitz.open(pdf_path) | |
| text = "" | |
| for page in doc: | |
| text += page.get_text() | |
| # 🔹 Step 3: Split Text into Chunks | |
| splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50) | |
| chunks = splitter.split_text(text) | |
| # 🔹 Step 4: Create Embeddings and FAISS Index | |
| model = SentenceTransformer("all-MiniLM-L6-v2") | |
| embeddings = model.encode(chunks) | |
| dimension = embeddings.shape[1] | |
| index = faiss.IndexFlatL2(dimension) | |
| index.add(np.array(embeddings)) | |
| chunk_list = chunks # Used for retrieval | |
| # 🔹 Step 5: RAG Query | |
| def query_rag(question, k=3): | |
| question_embedding = model.encode([question]) | |
| D, I = index.search(np.array(question_embedding), k) | |
| retrieved_chunks = [chunk_list[i] for i in I[0]] | |
| context = "\n".join(retrieved_chunks) | |
| prompt = f"Answer the question based on the following context:\n{context}\n\nQuestion: {question}\nAnswer:" | |
| return prompt | |
| # 🔹 Step 6: Generate answer from Groq API using environment variable for key | |
| def generate_answer(prompt): | |
| GROQ_API_KEY = os.environ["GROQ_API_KEY"] # Secure way to get API key | |
| url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| data = { | |
| "model": "llama3-8b-8192", | |
| "messages": [{"role": "user", "content": prompt}], | |
| "temperature": 0.3 | |
| } | |
| response = requests.post(url, headers=headers, json=data) | |
| return response.json()['choices'][0]['message']['content'] | |
| # 🔹 Step 7: Full RAG Pipeline | |
| def rag_pipeline(question): | |
| prompt = query_rag(question) | |
| answer = generate_answer(prompt) | |
| return answer | |
| # 🔹 Step 8: Gradio Interface | |
| interface = gr.Interface( | |
| fn=rag_pipeline, | |
| inputs=gr.Textbox(lines=2, placeholder="Ask any question from Falsafatuna..."), | |
| outputs="text", | |
| title="📘 Read ❤️Falsafatuna❤️ (Our Philosophy) by Allama Muhammad Baqir as-Sadr", | |
| description="Developed by Najaf Ali Sharqi — Educator, researcher and advocate of AI for Education. This app allows you to ask any question from the book *Falsafatuna* and receive intelligent responses using Groq + LLaMA3." | |
| ) | |
| interface.launch() | |