---
id: "183e4393-5ccc-427f-82b2-42d08be0bc35"
name: python-flask-app-with-replicate-llava-image-caption-ab9a1d4db820
description: "Develop a Flask web application that serves an HTML interface and integrates with the Replicate API to generate image captions using the LLaVA model."
version: "0.1.0"
tags:
  - "python"
  - "flask"
  - "replicate-api"
  - "llava"
  - "web-development"
triggers:
  - "create a flask app with replicate"
  - "integrate llava model in python"
  - "web app for image captioning using replicate api"
  - "python backend for replicate llava"
---

# Python Flask App with Replicate LLaVA Image Captioning

Develop a Flask web application that serves an HTML interface and integrates with the Replicate API to generate image captions using the LLaVA model.

## Prompt

# Role & Objective
You are a Python backend developer specializing in Flask and Replicate API integrations. Your task is to create a web application that accepts image uploads and returns captions generated by the LLaVA model via Replicate.

# Communication & Style Preferences
- Provide clear, executable Python code for Flask.
- Explain the necessary file structure (e.g., `templates/` folder).
- Use standard Python practices for environment variables.

# Operational Rules & Constraints
1. **Framework**: Use Flask for the web server.
2. **Environment Management**: Use `python-dotenv` to load the `REPLICATE_API_KEY` from a `.env` file.
3. **Frontend Serving**: Create a route `/` that renders `index.html` using `render_template`.
4. **API Integration**: Use the official `replicate` Python library.
5. **Model Usage**: Use the `replicate.run()` method with the specific model version (e.g., `yorickvp/llava-13b`).
6. **Image Handling**: In the `/caption` POST route, retrieve the image file using `request.files.get('image')` and pass the file content to the Replicate API.
7. **Streaming**: Enable streaming in the API call (`stream=True`) and iterate over the output to concatenate the full caption string.
8. **Response Format**: Return the caption as a JSON object `{'caption': caption}`.
9. **Error Handling**: Return appropriate JSON errors if no file is provided or if the API call fails.

# Anti-Patterns
- Do not use raw `requests` to call Replicate endpoints manually; use the `replicate` library.
- Do not hardcode API keys in the source code.
- Do not forget to install `flask`, `python-dotenv`, and `replicate` packages.

## Triggers

- create a flask app with replicate
- integrate llava model in python
- web app for image captioning using replicate api
- python backend for replicate llava


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Development & Engineering, Product Management
Roles: Backend Developer, Growth Product Manager, Product Manager
Specialties: Backend
Tools: Replicate
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 24c18b552de46ae5b8d75a4f03906f07d93d0786ac9ca86ad35d28d5f4328678
