Skip to content

Commit

Permalink
# Add FastAPI app with S3 integration and browser auto-open feature
Browse files Browse the repository at this point in the history
- Set up FastAPI app with routes for file upload and retrieval.
- Integrated S3 client for uploading files to an S3 bucket.
- Added functionality to automatically open the app in the browser.
- Imported necessary modules including threading and webbrowser.
  • Loading branch information
Solrikk authored Jul 19, 2024
1 parent 41dc356 commit 9c1c8a1
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,57 @@
import webbrowser
import threading
import hashlib
import os

from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse, FileResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
import hashlib
import os
from image_utils import find_similar_images, s3_client, S3_BUCKET_NAME

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/", response_class=HTMLResponse)
async def upload_form():
with open('templates/upload_form.html', 'r') as file:
html_content = file.read()
return HTMLResponse(content=html_content)
with open('templates/upload_form.html', 'r') as file:
html_content = file.read()
return HTMLResponse(content=html_content)


@app.post("/upload/", response_class=JSONResponse)
async def create_upload_file(file: UploadFile = File(...)):
contents = await file.read()
image_hash = hashlib.sha256(contents).hexdigest()
file_path = f'uploads/{image_hash}.jpg'
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'wb') as f:
f.write(contents)
contents = await file.read()
image_hash = hashlib.sha256(contents).hexdigest()
file_path = f'uploads/{image_hash}.jpg'
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, 'wb') as f:
f.write(contents)

s3_client.upload_file(file_path, S3_BUCKET_NAME, f'{image_hash}.jpg')
s3_client.upload_file(file_path, S3_BUCKET_NAME, f'{image_hash}.jpg')

similar_images = await find_similar_images(file_path)
similar_images = await find_similar_images(file_path)

response_data = {
"message": "File uploaded successfully",
"filename": image_hash,
"similar_images": similar_images
}
response_data = {
"message": "File uploaded successfully",
"filename": image_hash,
"similar_images": similar_images
}

return JSONResponse(content=response_data)
return JSONResponse(content=response_data)


@app.get("/uploads/{image_hash}.jpg", response_class=FileResponse)
async def serve_uploaded_image(image_hash: str):
file_path = f'uploads/{image_hash}.jpg'
return FileResponse(file_path)
file_path = f'uploads/{image_hash}.jpg'
return FileResponse(file_path)


def start_server():
import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)


if __name__ == "__main__":
threading.Timer(1.25, lambda: webbrowser.open("http://127.0.0.1:8000")).start()
start_server()

0 comments on commit 9c1c8a1

Please sign in to comment.