from fastapi import FastAPI import base64 import os import sys import uvicorn from imagekitio import ImageKit from imagekitio.models.ListAndSearchFileRequestOptions import ListAndSearchFileRequestOptions from fastapi.middleware.cors import CORSMiddleware import base64 app = FastAPI() # Configure CORS app.add_middleware( CORSMiddleware, allow_origins=["https://chatzl.github.io"], # Allows only this origin allow_credentials=True, allow_methods=["*"], # Allows all methods allow_headers=["*"], # Allows all headers ) imagekit = ImageKit( public_key=os.environ.get("public_key"), private_key=os.environ.get("private_key"), url_endpoint=os.environ.get("url_endpoint") ) @app.get("/") def read_root(): return { "introduction": "Welcome to the AI Photo Creator App! Here's how to use it: " "Step 1: 📸 Tell me what picture you want to take, and I will create one for you. " "Step 2: 🌍 If you feel satisfied with the photo created by me, you can copy and paste " "the photo address and share your photo on our crowd-sourced AI photography website." } @app.get("/image_upload") def upload_image(url): try: # Upload the image to ImageKit upload = imagekit.upload( file=url, file_name="image.jpg", ) # Return the upload response return {"message": "uploaded successfuly, check https://chatzl.github.io/photographer.html for your work!"} except Exception as e: return {"error": str(e)} @app.get("/images_list") async def get_images(): list_files = imagekit.list_files(options=ListAndSearchFileRequestOptions(search_query='createdAt >= "7d"')) list_files_sorted=sorted(list_files.response_metadata.raw, key=lambda x: x['createdAt'],reverse=True) return list_files_sorted if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=7860)