-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Add FastAPI app with S3 integration and browser auto-open feature
- 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
Showing
1 changed file
with
34 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |