|
from fastapi import FastAPI, HTTPException |
|
import base64 |
|
import os |
|
import sys |
|
import uvicorn |
|
from imagekitio import ImageKit |
|
from imagekitio.models.ListAndSearchFileRequestOptions import ListAndSearchFileRequestOptions |
|
from imagekitio.models.UploadFileRequestOptions import UploadFileRequestOptions |
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
|
import base64 |
|
from pydantic import BaseModel |
|
|
|
|
|
|
|
app = FastAPI() |
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["https://aiphotographer.github.io"], |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_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.post("/image_upload") |
|
async def upload_image(url): |
|
try: |
|
|
|
upload = imagekit.upload( |
|
file=url, |
|
file_name="image.jpg", |
|
|
|
) |
|
|
|
|
|
return {"message": "Uploaded successfully, check https://aiphotographer.github.io/photographer.html for your work!"} |
|
except Exception as e: |
|
return {"error": str(e)} |
|
|
|
|
|
@app.post("/palette_upload") |
|
async def upload_image(url): |
|
try: |
|
options = UploadFileRequestOptions( |
|
|
|
folder='/palette/', |
|
overwrite_tags=False, |
|
overwrite_custom_metadata=True, |
|
|
|
) |
|
upload = imagekit.upload( |
|
file=url, |
|
file_name="palette.jpg", |
|
options=options |
|
) |
|
|
|
|
|
return {"message": "Uploaded successfully, check https://aiphotographer.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) |
|
|