Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
import gradio as grfrom langchain_community.document_loaders import PyPDFLoaderfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom langchain_huggingface import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import FAISSfrom huggingface_hub import InferenceClientimport tempfileimport os
# Initialize embedding model (runs on CPU, small enough for free tier)embedding_model = HuggingFaceEmbeddings( model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': 'cpu'})
# Initialize Inference Client (uses free API)client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta")
# Global variable to store vectorstorevectorstore = None
def process_pdf(pdf_file): """Process uploaded PDF and create vector store.""" global vectorstore if pdf_file is None: return "Please upload a PDF file." try: # Load PDF loader = PyPDFLoader(pdf_file.name) documents = loader.load() # Split into chunks text_splitter = RecursiveCharacterTextSplitter( chunk_size=1000, chunk_overlap=200, ) chunks = text_splitter.split_documents(documents) # Create vector store vectorstore = FAISS.from_documents( documents=chunks, embedding=embedding_model ) return f"β
Successfully processed {len(documents)} pages into {len(chunks)} chunks. You can now ask questions!" except Exception as e: return f"β Error processing PDF: {str(e)}"
def answer_question(question): """Answer question using RAG.""" global vectorstore if vectorstore is None: return "Please upload and process a PDF first.", "" if not question.strip(): return "Please enter a question.", "" try: # Retrieve relevant chunks docs = vectorstore.similarity_search(question, k=3) # Format context context = "\n\n".join([doc.page_content for doc in docs]) # Create prompt prompt = f"""<|system|>You are a helpful assistant that answers questions based on the provided context. Only use information from the context to answer. If the answer is not in the context, say "I cannot find this information in the document."</s><|user|>Context:{context}
Question: {question}</s><|assistant|>""" # Call Inference API response = client.text_generation( prompt, max_new_tokens=512, temperature=0.7, ) # Format sources sources = [] for i, doc in enumerate(docs, 1): page = doc.metadata.get('page', 'N/A') if isinstance(page, int): page += 1 preview = doc.page_content[:150].replace('\n', ' ') sources.append(f"{i}. Page {page}: {preview}...") sources_text = "\n".join(sources) return response, sources_text except Exception as e: return f"β Error: {str(e)}", ""
# Create Gradio interfacewith gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown(""" # π RAG Document Q&A System Upload a PDF document and ask questions about its content. **How it works:** 1. Upload a PDF file 2. Click "Process PDF" to analyze the document 3. Ask questions about the document content """) with gr.Row(): with gr.Column(scale=1): pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"]) process_btn = gr.Button("π Process PDF", variant="primary") status_output = gr.Textbox(label="Status", interactive=False) with gr.Column(scale=2): question_input = gr.Textbox( label="Your Question", placeholder="What is this document about?", lines=2 ) ask_btn = gr.Button("π Ask Question", variant="primary") answer_output = gr.Textbox(label="Answer", lines=6, interactive=False) sources_output = gr.Textbox(label="Sources", lines=4, interactive=False) gr.Markdown(""" --- ### π οΈ Technical Details | Component | Technology | |-----------|------------| | Embeddings | sentence-transformers/all-MiniLM-L6-v2 | | Vector Store | FAISS | | LLM | Zephyr-7B via Inference API | | Chunking | 1000 chars, 200 overlap | ### β οΈ Development Challenges Documented This project encountered several technical challenges: - **LangChain API changes**: Package restructuring required updated imports - **PDF parsing issues**: Required proper HTTP headers for downloads - **LLM response quality**: FLAN-T5 produced short responses; switched to Zephyr-7B - **Memory management**: Required explicit GPU cleanup between model loads Built by [Nav772](https://huggingface.co/Nav772) as part of AI Engineering portfolio. """) # Connect buttons to functions process_btn.click( fn=process_pdf, inputs=[pdf_input], outputs=[status_output] ) ask_btn.click( fn=answer_question, inputs=[question_input], outputs=[answer_output, sources_output] )
demo.launch()app.pyAdded+169