mrme77's picture
Upload 2 files
6609187 verified

A newer version of the Gradio SDK is available: 5.35.0

Upgrade

πŸ“„ PDF to Social Media Post Generator

GitHub

πŸ“ 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

  1. Upload PDF: User selects a file
  2. Text Extraction: PDF parsed using PyPDF2
  3. Post Generation: Text sent to LLM via OpenRouter
  4. Cloud Logging: Logs are pushed to your GCS bucket (if credentials exist)
  5. 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.