A newer version of the Gradio SDK is available:
5.35.0
π PDF to Social Media Post Generator
π Overview
This tool allows users to upload a PDF (i.e. research paper, article, or report) and automatically generates a Social Media post that summarizes the content. The app uses meta-llama/llama-3.3-8b-instruct:free
via OpenRouter to produce compelling, professional summaries, while securely logging usage events to a Google Cloud bucket. It works seamlessly both locally and on Hugging Face Spaces.
β¨ Features
- π PDF Text Extraction: Extracts text from uploaded PDFs using
PyPDF2
- π¨ PII Checker: Ensures not PII is sent to the LLM
- π€ AI-Powered Summarization: Uses LLM models for post generation
- β Relevance Assurance: Keeps key concepts from the original document
- π― Format Optimization: Produces clean, ready-to-publish SocialMedia text
- π Cloud-Based Logging: Stores usage logs in a GCS bucket (Vertex AI compatible)
- π§βπ» Works Locally and in Hugging Face Spaces
π Getting Started
β Prerequisites
- Python 3.8 or higher
- OpenRouter API key β https://openrouter.ai
- (Optional for local use) GCP Service Account JSON key for logging
π§ Installation
# 1. Clone the repository
git clone https://huggingface.co/spaces/your-username/post_generator
cd post_generator
# 2. Create a virtual environment with uv package
- Install UV if not already installed
curl -LsSf https://astral.sh/uv/install.sh | sh
- Create a virtual environment with UV
uv venv .venv
- Activate the virtual environment
source .venv/bin/activate
- Install required dependencies
uv pip install openai gradio python-dotenv PyPDF2 presidio-analyzer spacy
python -m spacy download en_core_web_lg
π API and Credential Setup
π 1. Create a .env
file:
OPENROUTER_API_KEY=your-openrouter-key
GOOGLE_APPLICATION_CREDENTIALS=/absolute/path/to/your/gcp_key.json
On Hugging Face Spaces, you donβt need the
.env
file β add secrets via Settings > Secrets.
π 2. Hugging Face Secret Configuration:
Key | Value |
---|---|
OPENROUTER_API_KEY |
OpenRouter API key |
GOOGLE_APPLICATION_CREDENTIALS |
Paste raw JSON from GCP key |
βΆοΈ Running the App
Locally:
python app/main.py
Your app will be available at: http://127.0.0.1:7860
Hugging Face Spaces:
Just push the code β it auto-deploys!
π‘ How It Works
- Upload PDF: User selects a file
- Text Extraction: PDF parsed using
PyPDF2
- Post Generation: Text sent to LLM via OpenRouter
- Cloud Logging: Logs are pushed to your GCS bucket (if credentials exist)
- Display Result: The Social Media post appears, ready to copy
π Project Structure
pdf-to-socialmedia-app/
βββ app/
β βββ main.py # Gradio UI + routing
β βββ llm_integration.py # LLM logic via OpenRouter
β βββ pdf_processing.py # PDF extraction
β βββ export_handler.py # Text export logic
β βββ analytics.py # Cloud/local logging abstraction
βββ explore.ipynb # Jupyter Notebook for quick exploration
βββ .env # (local only) API keys
βββ requirements.txt # Python dependencies
βββ README.md
βββ LICENSE # It contains the terms of the MIT License
βββ analytics_logs/ # Local logs (if GCS is not used)
βοΈ Configuration Options
Change the LLM Model
Edit llm_integration.py
:
model = "meta-llama/llama-3.3-8b-instruct:free"
Explore more models at: https://openrouter.ai/models
Customize Generation Style
Edit the instruction
prompt in llm_integration.py
to change tone, length, or format of the post.
π Security Notes
- The app never stores the PDFs uploaded beyond session memory.
- OpenRouter keys and GCP credentials are stored in
.env
(local) or Hugging Face Secrets (hosted). - GCS logs are append-only and used solely for usage analytics.
π οΈ Future Features
- π Authentication (e.g., for private use or teams)
- π Direct Social Media Posting (API integration)
- π Support for DOCX, PPTX files
- πΎ Enhanced cloud storage of generated posts
π License
MIT License β see LICENSE
file for details.
π€ Contributing
All PRs, suggestions, and feedback are welcome!
Open an issue or fork and submit a pull request.