Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app/main.py +4 -6
- app/routers/image/jpgcompressor.py +60 -0
- requirements.txt +2 -1
app/main.py
CHANGED
|
@@ -1,21 +1,19 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
|
| 3 |
-
|
|
|
|
| 4 |
from app.routers.testers import random_number_generator
|
| 5 |
from app.routers.testers import help
|
| 6 |
from app.routers.testers import calculator
|
| 7 |
|
| 8 |
-
# add your more module here
|
| 9 |
-
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# Include all routers
|
|
|
|
| 13 |
app.include_router(random_number_generator.router)
|
| 14 |
app.include_router(help.router)
|
| 15 |
app.include_router(calculator.router)
|
| 16 |
|
| 17 |
-
# add more router here
|
| 18 |
-
|
| 19 |
@app.get("/")
|
| 20 |
def greet_json():
|
| 21 |
return {"message": "Go to /docs to see all available endpoints!", "link" : "https://sameerbanchhor-multi-use-api.hf.space/docs"}
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
# Import the new router
|
| 3 |
+
from app.routers.image import jpgcompressor
|
| 4 |
+
# Import the existing 'testers' routers
|
| 5 |
from app.routers.testers import random_number_generator
|
| 6 |
from app.routers.testers import help
|
| 7 |
from app.routers.testers import calculator
|
| 8 |
|
|
|
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
# Include all routers
|
| 12 |
+
app.include_router(jpgcompressor.router) # <-- Include the new image router
|
| 13 |
app.include_router(random_number_generator.router)
|
| 14 |
app.include_router(help.router)
|
| 15 |
app.include_router(calculator.router)
|
| 16 |
|
|
|
|
|
|
|
| 17 |
@app.get("/")
|
| 18 |
def greet_json():
|
| 19 |
return {"message": "Go to /docs to see all available endpoints!", "link" : "https://sameerbanchhor-multi-use-api.hf.space/docs"}
|
app/routers/image/jpgcompressor.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, UploadFile, File, Form, HTTPException
|
| 2 |
+
from fastapi.responses import StreamingResponse
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
|
| 6 |
+
# Create the router for image processing endpoints
|
| 7 |
+
router = APIRouter(
|
| 8 |
+
prefix="/image",
|
| 9 |
+
tags=["Image Processing"]
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
@router.post("/compress_jpg")
|
| 13 |
+
async def compress_jpg(
|
| 14 |
+
file: UploadFile = File(..., description="The JPG image file to compress."),
|
| 15 |
+
quality: int = Form(75, description="JPEG compression quality (1 to 100). Default is 75.")
|
| 16 |
+
):
|
| 17 |
+
"""
|
| 18 |
+
Compresses a JPEG image based on the specified quality setting.
|
| 19 |
+
The compressed image is returned as a file.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
if file.content_type != "image/jpeg":
|
| 23 |
+
raise HTTPException(
|
| 24 |
+
status_code=400,
|
| 25 |
+
detail="Invalid file type. Only JPEG files are supported for this endpoint."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
if not 1 <= quality <= 100:
|
| 29 |
+
raise HTTPException(
|
| 30 |
+
status_code=400,
|
| 31 |
+
detail="Quality must be an integer between 1 and 100."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Read the image file content
|
| 35 |
+
content = await file.read()
|
| 36 |
+
|
| 37 |
+
# Use BytesIO to handle the image in memory
|
| 38 |
+
input_buffer = BytesIO(content)
|
| 39 |
+
output_buffer = BytesIO()
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
# Open the image using Pillow
|
| 43 |
+
img = Image.open(input_buffer)
|
| 44 |
+
|
| 45 |
+
# Save the image to the output buffer with specified quality
|
| 46 |
+
img.save(output_buffer, "JPEG", quality=quality)
|
| 47 |
+
|
| 48 |
+
# Move the buffer cursor to the start so it can be read
|
| 49 |
+
output_buffer.seek(0)
|
| 50 |
+
|
| 51 |
+
# Return the compressed image as a file stream
|
| 52 |
+
return StreamingResponse(
|
| 53 |
+
output_buffer,
|
| 54 |
+
media_type="image/jpeg",
|
| 55 |
+
headers={"Content-Disposition": f"attachment; filename=compressed_{file.filename}"}
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
# Handle potential errors during image processing
|
| 60 |
+
raise HTTPException(status_code=500, detail=f"Image processing error: {e}")
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
-
requests
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn
|
| 3 |
+
requests
|
| 4 |
+
pillow
|