This is a Flask application that uses OpenAI's DALL-E to generate images based on user prompts. It features a simple web interface and tracks active users.
.
├── app.py # Main Flask application
├── generated_images/ # Directory where generated images are saved
├── static/ # Static files (CSS, JS)
│ ├── css/
│ │ └── style.css
│ └── js/
│ └── script.js
├── templates/
│ └── index.html # Main HTML page for the user interface
├── .env # Environment variables (OPENAI_API_KEY)
└── README.md # This file
-
Clone the repository (if applicable):
git clone <your-repository-url> cd <repository-directory>
-
Create a virtual environment and activate it:
python3 -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies: The application requires Python 3 and the following libraries. You can install them using pip:
pip install Flask openai python-dotenv flask-cors httpx
Alternatively, you can create a
requirements.txtfile with the following content:Flask openai python-dotenv Flask-CORS httpxAnd then install using:
pip install -r requirements.txt
-
Set up Environment Variables: Create a
.envfile in the root directory of the project and add your OpenAI API key:OPENAI_API_KEY='your_openai_api_key_here'
-
Proxy Configuration (Optional): The application is configured to use proxies defined in
app.py:proxies = { "http://": "http://127.0.0.1:6152", "https://": "http://127.0.0.1:6152", "all://": "socks5://127.0.0.1:6153" }
Adjust these settings in
app.pyif your proxy configuration is different or if you don't need proxies.
- Ensure your virtual environment is activated.
- Run the Flask application:
python app.py
- Open your web browser and go to
http://localhost:8002(orhttp://0.0.0.0:8002).
/: Serves the mainindex.htmlpage./static/css/<path:path>: Serves CSS files./static/js/<path:path>: Serves JavaScript files./active-users: (POST) Updates and returns the count of active users.- Request body:
{ "userId": "some_unique_user_id" }
- Request body:
/generate-image: (POST) Generates an image based on prompts.- Request body:
{ "system_prompt": "System instructions", "user_prompt": "User's image request", "userId": "some_unique_user_id" } - Response (success):
{ "status": "success", "image_url": "data:image/png;base64,...", "saved_path": "generated_images/...", "active_users": count } - Response (error):
{ "status": "error", "message": "Error details" }
- Request body:
- Backend: Flask handles HTTP requests.
- Image Generation: Uses the OpenAI Python library to interact with the DALL-E API (specifically "gpt-image-1" model in the code, which seems to be a placeholder or custom name for a DALL-E model).
- User Interface: A simple HTML page with JavaScript to send requests to the backend and display the generated image.
- User Activity: Tracks active users based on API calls, with an inactivity threshold of 60 seconds.
- Concurrency: Uses thread-local storage for OpenAI clients to ensure each thread has its own client instance, which is good practice for concurrent requests.
- Image Storage: Generated images are saved as PNG files in the
generated_imagesdirectory with a timestamped filename.
- Replace
"gpt-image-1"with the correct DALL-E model name if it's a placeholder (e.g.,dall-e-3,dall-e-2). - Add more robust error handling and logging.
- Implement user authentication if needed.
- Improve the user interface.
- Add unit and integration tests.
- Consider a more persistent way to track active users if needed beyond in-memory storage.