store the name of the picture too before sending back the array,…
- User
store the name of the picture too before sending back the array,
@app.post("/api/get_prepareItem_image") async def get_prepareItem_images(): images = [] for image in os.listdir("downloaded_images"): image_path = os.path.join("downloaded_images", image) with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) # Assuming the images are in JPEG format images.append("data:image/jpg;base64," + encoded_string.decode("utf-8")) return JSONResponse({"images": images})
- Assistant
To store the name of the image in the response array, you can modify the for loop to keep track of the image filenames and include them in the results. Each entry in the
imagesarray will be a dictionary containing thenameof the image and itsdata. Here is the updated code:from fastapi import FastAPI from fastapi.responses import JSONResponse import os import base64 app = FastAPI() @app.post("/api/get_prepareItem_image") async def get_prepareItem_images(): images = [] for image_name in os.listdir("downloaded_images"): image_path = os.path.join("downloaded_images", image_name) with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) # Storing both the image name and its base64 representation in the array image_data = { "name": image_name, "data": "data:image/jpg;base64," + encoded_string.decode("utf-8") } images.append(image_data) return JSONResponse({"images": images}) - User
make so it runs the getimages in the background and return the other stuff to the client:
@app.post("/perpareItem") async def perpare_item_endpoint(request: Request): request_data = await request.json() message = request_data.get("message") print(message) urls = request_data.get("urls") print(urls) result = await llm_bot.lookup_ai(message) print(result) avg_price = stats.adding_item(result) result["avg_price"] = avg_price get_pictures.get_images(urls)
return result - Assistant
- User
Downloaded s-l500.jpg to downloaded_images Downloaded s-l500.jpg to downloaded_images 2024-01-09 12:21:06,290:WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 2024-01-09 12:21:06,290 - WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 Downloaded s-l500.jpg to downloaded_images 2024-01-09 12:21:06,352:WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 2024-01-09 12:21:06,352 - WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 Downloaded s-l1600.jpg to downloaded_images 2024-01-09 12:21:06,434:WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 2024-01-09 12:21:06,434 - WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 2024-01-09 12:21:06,550:WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 2024-01-09 12:21:06,550 - WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 Downloaded s-l500.jpg to downloaded_images 2024-01-09 12:21:07,052:WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 2024-01-09 12:21:07,052 - WARNING - Connection pool is full, discarding connection: i.ebayimg.com. Connection pool size: 10 Downloaded s-l1600.jpg to downloaded_images
- Assistant
- User
- Assistant
- User
return a gif: else: images = [] for image in os.listdir("downloaded_images"): image_path = os.path.join("downloaded_images", image) with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) image_data = { "image": "data:image/gif;base64," + encoded_string.decode("utf-8"), "name": image, } images.append(image_data) return JSONResponse({"images": images})
- Assistant
Your current code snippet is assuming all images in the "downloaded_images" directory are GIFs. However, we need to account for different image types and assign the correct MIME type for each image. The MIME type for JPEG images is
image/jpeg, for PNGs it'simage/png, for GIFs it'simage/gif, and so forth.Here is the code snippet modified to detect the image type using the file extension and then encode it accordingly:
import os import base64 from fastapi import FastAPI from fastapi.responses import JSONResponse from mimetypes import guess_type app = FastAPI() @app.post("/api/get_images") async def get_images(): images = [] for image in os.listdir("downloaded_images"): image_path = os.path.join("downloaded_images", image) with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) mime_type, _ = guess_type(image_path) # guess the MIME type of the image if not mime_type: # if mime_type is None, default to 'application/octet-stream' mime_type = 'application/octet-stream' image_data = { "image": f"data:{mime_type};base64," + encoded_string.decode("utf-8"), "name": image, } images.append(image_data) return JSONResponse({"images": images})
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.