├── .env.example ├── .gitignore ├── README.md ├── api └── index.py ├── ogimage.png ├── requirements.txt └── vercel.json /.env.example: -------------------------------------------------------------------------------- 1 | REPLICATE_API_TOKEN= 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vercel 2 | *.log 3 | *.pyc 4 | __pycache__ 5 | 6 | # Environments 7 | .env 8 | .venv 9 | env/ 10 | venv/ 11 | ENV/ 12 | env.bak/ 13 | venv.bak/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alt Image Generator 2 | 3 | [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/nutlope/alt-text-generator&env=REPLICATE_API_KEY&project-name=alt-tag-generator&repo-name=alt-tag-generator) 4 | 5 | This Flask API will generate a description for any image using AI. If you're looking for the TypeScript version, [click here](https://github.com/vercel/examples/tree/main/solutions/alt-tag-generator). 6 | 7 | ![Alt Image Generator](ogimage.png) 8 | 9 | ## How it works 10 | 11 | This project uses an ML modal from Salesforce called [BLIP](https://github.com/salesforce/BLIP) on [Replicate](https://replicate.com/) to generate relevant alt text for images. You can feed the Flask API endpoint an image as a query param and it will return a one sentence description of that image. 12 | 13 | ## Running Locally 14 | 15 | After cloning the repo, go to [Replicate](https://replicate.com/) to make an account and put your API key in `.env`. 16 | 17 | Then, run the following in the command line and your application will be available at `http://localhost:3000` 18 | 19 | ```bash 20 | npm i -g vercel 21 | vercel dev 22 | ``` 23 | 24 | To use the API route, go to the link below in your browser or run a curl command in your terminal to get a sample result. Feel free to replace the dub.sh link with a link to any image. 25 | 26 | ```bash 27 | curl http://localhost:3000/generate?imageUrl=https://dub.sh/confpic 28 | ``` 29 | -------------------------------------------------------------------------------- /api/index.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import replicate 4 | from flask import Flask, request 5 | 6 | app = Flask(__name__) 7 | os.environ.get("REPLICATE_API_TOKEN") 8 | 9 | @app.route("/") 10 | def index(): 11 | return "This is an alt tag generator!" 12 | 13 | @app.route('/generate') 14 | def home(): 15 | # Get imageUrl query param 16 | args = request.args 17 | imageUrl = args.to_dict().get('imageUrl') 18 | 19 | # Run ML Model with imageUrl 20 | model = replicate.models.get("salesforce/blip") 21 | version = model.versions.get("2e1dddc8621f72155f24cf2e0adbde548458d3cab9f00c0139eea840d0ac4746") 22 | 23 | # Get the alt text result and return it 24 | return version.predict(image=imageUrl) 25 | -------------------------------------------------------------------------------- /ogimage.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Nutlope/alt-text-generator/e5f3c8661a3a542e84787b83a62a9ab35e1d908a/ogimage.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==3.0.0 2 | replicate==0.14.0 3 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "builds": [ 3 | { 4 | "src": "api/index.py", 5 | "use": "@vercel/python" 6 | } 7 | ], 8 | "routes": [ 9 | { 10 | "src": "/(.*)", 11 | "dest": "api/index.py" 12 | } 13 | ] 14 | } 15 | --------------------------------------------------------------------------------