In my python FastAPI backend code, I have a crud function for adding images: [code] It…
- User
In my python FastAPI backend code, I have a crud function for adding images:
def add_image_no_commit(db: Session, image_file_path: str) -> bool: with PILImage.open(image_file_path) as image: ahash_value = str(imagehash.phash(image, 16)) # Check to see if the image path has already been added db_image = db.query(models.Image.image_file).filter(models.Image.image_file == image_file_path).first() # File already in database if db_image: # Same ahash if db_image.ahash == ahash_value: print(f"Image {image_file_path!r} already in database and ahash({ahash_value!r}) matches db ahash({db_image.ahash!r})") return False else: # Difference ahash, new image, should we overwrite it? For now, yes. print(f"Image {image_file_path!r} found in database but ahash({ahash_value!r}) does not match db ahash({db_image.ahash!r})") db_image.ahash = ahash_value else: db_image = models.Image(image_file=image_file_path, ahash=ahash_value, width=image.width, height=image.height) db.add(db_image) return TrueIt seems if the image exists in the database and it tries to update the ahash, it errors with the error of simply "ahash", the reason why it errors is my assumption, however the error it prints out is simply "ahash". Here is the background task function for more context:
def add_folder_images_task(path: str): # Create a new, indepdent session for background task db = SessionLocal() try: # TODO: It would be better to build a bulk insert statement maybe, for now we iterate here in python entries = os.listdir(path) image_paths = [os.path.join(path, f) for f in entries if os.path.isfile(os.path.join(path, f)) and f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp', '.gif'))] images_added = 0 for image in image_paths: if crud.add_image_no_commit(db, image): images_added = images_added + 1 db.commit() # Make sure to commit the changes made by the task print(f"Background task added {images_added} images to the database.") except Exception as e: print(f"Error running background task: {str(e)}") finally: db.close() # Ensure the session is closed after taskMy assumption is I am not using the correct method for updating existing rows, however, do a deep dive analysis of the functions to first figure out what I am doing wrong to cause this particular error message, and how to properly fix it, and then if there is any other glaring mistakes please let me know along with a solution, and do it in a step-by-step way to ensure the code is solid.
- Assistant
- User
Perfect solution, thank you!
I have this element:
<Flex> <Select mode="multiple" allowClear style={{ width: "100%", }} placeholder="Select Taggers" defaultValue={JSON.stringify(taggersData["wd14_moat_v2"])} onChange={handleTaggerChange} options={taggers} /> <div> Max Tags: {/* integer input text box */} </div> </Flex>I am working on the Max Tags input, I need a text box that only allows integers, and of course, the value needs to get stored in a state variable that I can access, and updated whenever it is changed, maybe if antd has a box that allow allows up and down buttons or other controls to change the number without typing? The sane limits, if it is possible to set them, should be minimum 5, maximum 100. However, I will settle for a simple text input that supports numbers if antd doesn't have support for something fancier.
The value will be passed in to performTagging here:
<Button onClick={() => { performTagging(selectedImages, source); }} > - Assistant
In your scenario, you can use the
InputNumbercomponent from Ant Design (antd), which is designed for numeric input and includes controls for incrementing and decrementing the value. It also allows you to set minimum and maximum limits. - User
- Assistant
- User
- Assistant