I want you to help me create a new FastAPI and REACT project. Let's start with FastAPI,…
- User
I want you to help me create a new FastAPI and REACT project. Let's start with FastAPI, I need step by step for setting up the project, I am going to need Pydantic, SQLAlchemy using a DB that is flexible such that, using SQLAlchemy I can have dynamically created tables from the REACT frontend UI, and supports imagehash perceptual hashes with hammingway distance comparisons for finding similar images, set up CORSMiddleware, I am going to want to be able to add Image entries into the database that can be pulled to caption the images using ViT-L, scorers like CHAD scorer, as I want to use this to be able to build datasets for training image generative AI diffusion models. So the workflow will be:
- add images to the database
- caption and score images, adding that info into flexible metadata in the database
- Be able to browse, filter and sort images using various metadata
- Add a selection of images to a dataset(separate from the main list, but it just links to the main images)
- Choose and build captions using CLIP and other captioners or taggers like WD14, with the ability to manually add to all image captions in the data set, or to individual images
- I also want to be able to run a caption "upsample" on the captions, in which I ran a prompt with the caption through an LLM to get back a more detailed caption built from the other caption.
- In this way we will have multiple captions per image in a dataset, using categories to organise them, such as "ground truth" captions, simple main subject captions, and the upsampled detailed captions.
- Easily add/remove images to and from the dataset
- Then once the dataset is built, I can "package out" the dataset which generates file pairs of images and .txt caption files to the requested folder
- The options when packaging up a dataset can be to build folders with the same images but the different captions, so a folder with all the images plus the "ground truth" in caption .txt files, and a folder with the images plus "simple main subject" captions, etc
Well, that is a lot to go through already, but that is the overall workflow I want to end up with, but first we need to setup the project.
- Assistant
Certainly! Setting up a new FastAPI project with all the features you have described is quite an extensive task, but let's start by creating the FastAPI backend step by step. Please note that some steps might require additional considerations which I will try to highlight.
-
Project Setup:
- Make sure you have Python installed on your machine.
- Create a new directory for your project and navigate into it.
- Initialize a new Python virtual environment:
python -m venv venv - Activate the virtual environment:
- On Windows:
.\venv\Scripts\activate - On Linux/Mac:
source venv/bin/activate
- On Windows:
- Install FastAPI and Uvicorn (an ASGI server):
pip install fastapi uvicorn
-
Database Setup:
- Install SQLAlchemy and a database driver, for example, SQLite for development purposes:
pip install sqlalchemy aiosqlite - You can also consider other databases that SQLAlchemy supports, such as PostgreSQL, MySQL, etc.
- Create a new Python file for your database models, let's call it
models.py. - Define SQLAlchemy models:
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker DATABASE_URL = "sqlite:///./test.db" engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False}) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) Base = declarative_base() metadata = MetaData() # Define your models here Base.metadata.create_all(bind=engine)
- Install SQLAlchemy and a database driver, for example, SQLite for development purposes:
-
Dynamic Table Creation:
- Dynamic tables in SQLAlchemy can be tricky, and you might want to create a method that generates tables at runtime. This usually involves utilizing the
Tableclass directly fromsqlalchemyto define new tables with dynamically specified columns.
- Dynamic tables in SQLAlchemy can be tricky, and you might want to create a method that generates tables at runtime. This usually involves utilizing the
-
Images and ImageHash:
- Install additional libraries for image handling and perceptual hashing (You might need to implement the perceptual hash function using a library like
imagehash):pip install python-magic Pillow imagehash - Implement models and utility functions to store images, generate hashes, and perform comparisons with Hamming distance.
- Install additional libraries for image handling and perceptual hashing (You might need to implement the perceptual hash function using a library like
-
Pydantic Models:
- Create Pydantic models in a file like
schemas.pyfor request and response serialization.
- Create Pydantic models in a file like
-
FastAPI App Initialization:
- Create a new Python file called
main.pyfor your application code. - Set up FastAPI app with CORSMiddleware:
from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware app = FastAPI() # Configure CORS settings as needed origins = ["*"] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Define routes below
- Create a new Python file called
-
Routes:
- Define routes in
main.pyfor the various operations like adding images, captioning, scoring, browsing, etc., according to the workflow you have specified.
- Define routes in
-
Captioning and Scoring:
- Incorporate captioning and scoring mechanisms by integrating with suitable models or APIs for ViT-L, CLIP, CHAD scorer, etc.
- Implement API endpoints that trigger these processes.
-
Datasets and Export:
- Implement functionality to manage datasets, add/remove images, and handle captions.
- Develop the export feature allowing you to package the datasets as described.
-
Running the Server:
- Start the FastAPI server using Uvicorn:
This command will start the development server with auto-reload enabled.uvicorn main:app --reload
- Start the FastAPI server using Uvicorn:
-
Next Steps:
- You will need to implement each part of the backend step by step, verifying the functionality as you go.
- Extensive testing will be required to ensure each dynamic and complex feature works as expected.
- Once you have the backend working with a flexible schema and image handling, you can proceed with the React frontend setup and integration, which involves another series of steps.
Keep in mind that this outline provides a high-level overview, and implementing each step will require careful planning, coding, and testing. Each feature described, like dynamic table creation, image captioning/scoring, dataset packaging, etc., would typically need detailed examples and specific code snippets which go beyond the scope of this general setup guide.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.