Spaces:
Sleeping
Sleeping
Download app.py from manugupranay/Cafe: direct link, hf CLI and curl.
- Browser
- Download file 2.51 kB
-
https://huggingface.co/spaces/manugupranay/Cafe/resolve/main/app.py
- Command line
-
hf download hf://spaces/manugupranay/Cafe/app.py
-
curl -L -o app.py https://huggingface.co/spaces/manugupranay/Cafe/resolve/main/app.py
2.51 kB
| import gradio as gr | |
| import fitz # PyMuPDF | |
| import faiss | |
| import numpy as np | |
| from sentence_transformers import SentenceTransformer | |
| from transformers import pipeline | |
| embed_model = SentenceTransformer("all-MiniLM-L6-v2") | |
| llm = pipeline("text2text-generation", model="google/flan-t5-small") | |
| texts = [] | |
| index = None | |
| def process_pdf(file): | |
| """ | |
| In HF Spaces, gr.File often returns a filepath (NamedString), | |
| not a file-like object. So we open using the path. | |
| """ | |
| global texts, index | |
| texts = [] | |
| index = None | |
| if file is None: | |
| return "❌ No file uploaded" | |
| # If file is a string/path | |
| pdf_path = file if isinstance(file, str) else getattr(file, "name", None) | |
| if not pdf_path: | |
| return f"❌ Unsupported file input type: {type(file)}" | |
| try: | |
| pdf = fitz.open(pdf_path) | |
| except Exception as e: | |
| return f"❌ Failed to read PDF: {e}" | |
| for i, page in enumerate(pdf): | |
| text = page.get_text().strip() | |
| if text: | |
| texts.append(f"[PAGE {i+1}]\n{text}") | |
| if not texts: | |
| return "⚠️ No readable text found in PDF (If this is scanned, OCR is needed)" | |
| embeddings = embed_model.encode(texts) | |
| index = faiss.IndexFlatL2(embeddings.shape[1]) | |
| index.add(np.array(embeddings, dtype=np.float32)) | |
| return f"✅ PDF processed successfully. Pages indexed: {len(texts)}" | |
| def ask_question(question): | |
| if index is None: | |
| return "❌ Upload and process a PDF first" | |
| q_embed = embed_model.encode([question]) | |
| _, I = index.search(np.array(q_embed, dtype=np.float32), 1) | |
| context = texts[I[0][0]] | |
| prompt = f""" | |
| You are a Restaurant Operations Assistant. | |
| Answer ONLY from the document context. | |
| If not found, say: "Not found in the document." | |
| Context: | |
| {context} | |
| Question: | |
| {question} | |
| Answer: | |
| """.strip() | |
| out = llm(prompt, max_new_tokens=150) | |
| return out[0]["generated_text"] | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🍽️ Restaurant Document AI Assistant (RAG)") | |
| gr.Markdown("Upload restaurant PDFs and ask questions from them.") | |
| pdf_file = gr.File(label="Upload Restaurant PDF", file_types=[".pdf"]) | |
| process_btn = gr.Button("Process PDF") | |
| status = gr.Textbox(label="Status", lines=2) | |
| process_btn.click(process_pdf, pdf_file, status) | |
| question = gr.Textbox(label="Ask a question", placeholder="Example: What work was performed?") | |
| answer = gr.Textbox(label="Answer", lines=6) | |
| question.submit(ask_question, question, answer) | |
| demo.launch() | |