Spaces:
Sleeping
Sleeping
Download app.py from memoona-nz/AI_pdf_reader: direct link, hf CLI and curl.
- Browser
- Download file 3.96 kB
-
https://huggingface.co/spaces/memoona-nz/AI_pdf_reader/resolve/main/app.py
- Command line
-
hf download hf://spaces/memoona-nz/AI_pdf_reader/app.py
-
curl -L -o app.py https://huggingface.co/spaces/memoona-nz/AI_pdf_reader/resolve/main/app.py
3.96 kB
| import gradio as gr | |
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| from langchain_community.document_loaders import PyPDFLoader | |
| from langchain_community.embeddings import HuggingFaceEmbeddings | |
| from langchain_community.vectorstores import FAISS | |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| import torch | |
| # MODEL | |
| model_name = "google/flan-t5-base" | |
| model = AutoModelForSeq2SeqLM.from_pretrained(model_name) | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| # 1. PROCESS PDF | |
| def process_pdfs(files): | |
| if not files: | |
| return None, "No files uploaded." | |
| all_docs = [] | |
| for file in files: | |
| loader = PyPDFLoader(file.name) | |
| docs = loader.load() | |
| all_docs.extend(docs) | |
| splitter = RecursiveCharacterTextSplitter( | |
| chunk_size=500, | |
| chunk_overlap=100 | |
| ) | |
| split_docs = splitter.split_documents(all_docs) | |
| embeddings = HuggingFaceEmbeddings( | |
| model_name="sentence-transformers/all-MiniLM-L6-v2" | |
| ) | |
| vectorstore = FAISS.from_documents(split_docs, embeddings) | |
| return vectorstore, f"β {len(split_docs)} chunks processed" | |
| # 2. ASK QUESTION | |
| def ask_question(question, vectorstore): | |
| if vectorstore is None: | |
| return "β Please upload PDF files first." | |
| docs = vectorstore.similarity_search(question, k=5) | |
| context = "\n".join([d.page_content for d in docs]) | |
| prompt = f""" | |
| You are a university professor teaching Machine Learning. | |
| You MUST give a detailed, long and structured answer. | |
| Format your answer EXACTLY like this: | |
| 1. Definition: | |
| 2. Explanation: | |
| 3. Intuition (why it is used): | |
| 4. Step-by-step working (if applicable): | |
| 5. Example: | |
| 6. Summary: | |
| Do NOT give short answers. | |
| Context: | |
| {context} | |
| Question: | |
| {question} | |
| Now write a complete educational answer: | |
| """ | |
| #3. generated output | |
| inputs = tokenizer(prompt, return_tensors="pt", truncation=True) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=300, | |
| temperature=0.8, | |
| top_p=0.9, | |
| do_sample=True, | |
| repetition_penalty=1.2 | |
| ) | |
| return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # GRADIO UI (FIX IS HERE) | |
| import gradio as gr | |
| with gr.Blocks(theme=gr.themes.Soft(), css=""" | |
| #title { | |
| text-align: center; | |
| font-size: 28px; | |
| font-weight: bold; | |
| color: #4A90E2; | |
| } | |
| .container { | |
| max-width: 900px; | |
| margin: auto; | |
| } | |
| .chatbox { | |
| height: 400px; | |
| overflow-y: auto; | |
| border-radius: 12px; | |
| } | |
| button { | |
| border-radius: 10px !important; | |
| } | |
| """) as demo: | |
| gr.Markdown("<h1 id='title'>π AI PDF Q&A Chatbot (RAG System)</h1>") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| gr.Markdown("### π Upload PDF") | |
| pdf_input = gr.File(file_count="multiple", file_types=[".pdf"]) | |
| upload_btn = gr.Button(" Process PDFs", variant="primary") | |
| upload_output = gr.Textbox(label="Status") | |
| with gr.Column(scale=2): | |
| gr.Markdown("### π¬ Ask Questions") | |
| question_input = gr.Textbox( | |
| placeholder="Ask anything from your PDF...", | |
| label="Your Question" | |
| ) | |
| ask_btn = gr.Button(" Ask AI", variant="primary") | |
| answer_output = gr.Textbox( | |
| label="AI Answer", | |
| lines=10 | |
| ) | |
| # ------------------------- | |
| # STATE | |
| # ------------------------- | |
| vector_state = gr.State(None) | |
| # ------------------------- | |
| # EVENTS | |
| # ------------------------- | |
| upload_btn.click( | |
| process_pdfs, | |
| inputs=pdf_input, | |
| outputs=[vector_state, upload_output] | |
| ) | |
| ask_btn.click( | |
| ask_question, | |
| inputs=[question_input, vector_state], | |
| outputs=answer_output | |
| ) | |
| demo.launch() |