This repository contains the code for an AI-powered Telegram bot that generates images using Stable Diffusion. The bot integrates Modal Labs for serverless GPU computing, Neon for PostgreSQL database management, and Stripe for handling payments.
- Features
- Technologies Used
- Prerequisites
- Setup
- Project Structure
- How It Works
- Future Enhancements
- Contributing
- License
- Generate AI images based on text prompts via Telegram
- Serverless GPU-powered image generation using Stable Diffusion
- User credit system for image generation
- Integration with Stripe for purchasing additional credits
- Scalable PostgreSQL database for user management
- Modal Labs - Serverless GPU computing
- Neon - Serverless PostgreSQL database
- Telegram Bot API - Bot interface
- Stripe - Payment processing
- Stable Diffusion - AI image generation model
- Python - Primary programming language
Before you begin, ensure you have the following:
- A Modal Labs account
- A Neon account and database
- A Telegram bot token
- A Stripe account (test mode is fine for development)
- Python 3.7 or later
-
Clone this repository:
git clone https://github.com/yirenlu92/modal-neon-image-generator cd modal-neon-image-generator
-
Set up a virtual environment:
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install the required packages:
pip install modal requests psycopg2-binary
-
Set up your environment variables in Modal as mentioned in the article:
TELEGRAM_BOT_API_TOKEN
: Your Telegram bot tokenDB_CONNECTION_STRING
: Your Neon database connection stringSTRIPE_PAYMENT_LINK
: Your Stripe payment link
-
Deploy the app to Modal:
modal deploy app.py
-
Set your Telegram bot's webhook to the Modal endpoint URL.
-
Set up a Stripe webhook to listen for the
checkout.session_completed
event, pointing to your Modal endpoint.
app.py
: Main application file containing the Modal app definition and webhook handlerhelpers.py
: Helper classes for Telegram bot interactions and database operations
- Users interact with the bot via Telegram, sending text prompts for image generation.
- The bot checks the user's available credits in the Neon database.
- If credits are available, it uses Modal's serverless GPU to generate an image with Stable Diffusion.
- The generated image is sent back to the user via Telegram, and credits are deducted.
- When credits run out, users are prompted to purchase more via a Stripe payment link.
- After successful payment, Stripe sends a webhook to the Modal endpoint, updating the user's credits in the database.