A web application that allows users to upload PDF files and ask questions about their content, with answers generated by the microsoft/Phi-3-mini-4k-instruct-fast model via the Hugging Face Inference API. Answers are strictly based on the PDF text, and unrelated questions return "This question is outside the PDF content." Ideal for technical use cases like analyzing API documentation or test plans.
- Upload PDFs and extract text using
pdf.js. - Ask questions about the PDF, receiving precise, detailed answers with quotes/references.
- Rejects unrelated questions to ensure accuracy.
- No input/output restrictions for handling large PDFs.
- Frontend hosted on GitHub Pages, backend on Render’s free plan.
- Frontend: React, pdf.js, Tailwind CSS, Babel, GitHub Pages
- Backend: FastAPI, Python 3.10, requests, Pydantic, Uvicorn, Render
- AI: Hugging Face Inference API,
microsoft/Phi-3-mini-4k-instruct-fast - DevOps: Docker, GitHub
- Security: Environment variables for API key, CORS for frontend access
- GitHub Account: For hosting frontend and backend repositories.
- Render Account: For deploying the backend (free plan).
- Hugging Face Account: For API access to
microsoft/Phi-3-mini-4k-instruct-fast.- Generate an API key (
HF_API_KEY) fromhttps://huggingface.co/settings/tokens. - Free tier (
10k tokens/day) may hit rate limits with large PDFs; consider a Pro plan ($9/month).
- Generate an API key (
- Docker: For local testing (optional).
- Git: For version control.
chat-with-pdfRepository: Frontend (React, hosted on GitHub Pages)index.html: Main file with UI and logic
chat-with-pdf-backendRepository: Backend (FastAPI, hosted on Render)main.py: FastAPI app with/api/chatendpointDockerfile: Containerizes the backendrequirements.txt: Python dependencies
- Create Repository:
- Create a public GitHub repository named
chat-with-pdf.
- Create a public GitHub repository named
- Add
index.html:- Copy the
index.htmlfile from this project to the repository root. - Update the
fetchURL inhandleSendMessage(line ~70) to your Render backend URL (e.g.,https://chat-with-pdf-backend.onrender.com/api/chat).
- Copy the
- Enable GitHub Pages:
- Go to repository Settings > Pages.
- Set Source to
Deploy from a branch, selectmainbranch, and/ (root)folder. - Save and note the URL (e.g.,
https://your-username.github.io/chat-with-pdf/).
- Push Changes:
git add index.html git commit -m "Add frontend" git push origin main- GitHub Pages will deploy the frontend (may take a few minutes).
- Create Repository:
- Create a public GitHub repository named
chat-with-pdf-backend.
- Create a public GitHub repository named
- Add Backend Files:
- Copy
main.py,Dockerfile, andrequirements.txtto the repository root. - In
main.py, ensureallow_originsincludes your GitHub Pages URL (e.g.,https://your-username.github.io).
- Copy
- Set Up Render:
- Sign in to Render (
https://render.com). - Create a new Web Service, selecting your
chat-with-pdf-backendrepository. - Configure:
- Runtime: Docker
- Region: Choose closest (e.g., Oregon)
- Plan: Free
- Environment Variables:
- Key:
HF_API_KEY - Value: Your Hugging Face API key (
hf_xxxxxxxxxxxxxxxxxxxxxxxx)
- Key:
- Deploy the service.
- Sign in to Render (
- Verify Deployment:
- Check Render logs to confirm FastAPI starts on port 8000.
- Note the Render URL (e.g.,
https://chat-with-pdf-backend.onrender.com).
- Access Frontend:
- Visit your GitHub Pages URL (e.g.,
https://your-username.github.io/chat-with-pdf/).
- Visit your GitHub Pages URL (e.g.,
- Upload PDF:
- Upload a PDF (e.g., API documentation).
- Verify the system message: “PDF uploaded successfully. You can now ask questions about the content.”
- Ask Questions:
- PDF-related (e.g., “What are the endpoint parameters?”): Expect a detailed response with PDF quotes.
- Unrelated (e.g., “What is Python?”): Expect “This question is outside the PDF content.”
- Debug with Curl:
curl -X POST https://your-render-service.onrender.com/api/chat \ -H "Content-Type: application/json" \ -d '{"pdfText":"The endpoint requires a JWT token.","userInput":"What does the endpoint need?"}'
- Expected:
{"reply": "The endpoint requires a JWT token."}
curl -X POST https://your-render-service.onrender.com/api/chat \ -H "Content-Type: application/json" \ -d '{"pdfText":"The endpoint requires a JWT token.","userInput":"What is Java?"}'
- Expected:
{"reply": "This question is outside the PDF content."}
- Expected:
- Hugging Face Free Tier: Limited to ~10k tokens/day. Large PDFs (e.g., 10,000 characters) + questions + 512-token responses can consume ~5,000-10,000 tokens per request, hitting limits quickly.
- Recommendation: Use a Hugging Face Pro plan (~$9/month) or compute credits for reliable access.
- Monitor: Check usage at
https://huggingface.co/settings/billing. - Mitigation: If rate limits occur, add temporary truncation in
main.py:pdf_text = request.pdfText[:5000] user_input = request.userInput[:1000]
- Rate Limit Exceeded:
- Check Hugging Face Console for usage.
- Reduce
max_tokensto 200 or add truncation. - Upgrade to Pro plan.
- Model Unavailable:
- Verify access to
microsoft/Phi-3-mini-4k-instruct-fastin Hugging Face Hub. - Test with
meta-llama/Llama-3.2-8B-Instruct.
- Verify access to
- Invalid API Key:
- Update
HF_API_KEYin Render dashboard.
- Update
- CORS Issues:
- Ensure
allow_originsinmain.pymatches your GitHub Pages URL.
- Ensure
- Vague Responses:
- Share example PDF and response for prompt refinement.
- Fork the repositories (
chat-with-pdf,chat-with-pdf-backend). - Submit pull requests with improvements (e.g., prompt tuning, UI enhancements).
MIT License. See LICENSE file (add to repositories if needed).
For issues or feature requests, open a GitHub issue in either repository or contact the project maintainer.