Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- .gitignore +4 -1
- app/main.py +32 -10
- app/routers/image/jpgcompressor.py +38 -22
.gitignore
CHANGED
|
@@ -1,2 +1,5 @@
|
|
| 1 |
# prompts file
|
| 2 |
-
prompt.txt
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# prompts file
|
| 2 |
+
prompt.txt
|
| 3 |
+
|
| 4 |
+
#cache
|
| 5 |
+
__pycache__
|
app/main.py
CHANGED
|
@@ -1,19 +1,41 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
def greet_json():
|
| 19 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
|
| 4 |
+
# ==========================
|
| 5 |
+
# π Image Module
|
| 6 |
+
# ==========================
|
| 7 |
+
from app.routers.image import jpgcompressor
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# ==========================
|
| 11 |
+
# π§ͺ Test Utilities
|
| 12 |
+
# ==========================
|
| 13 |
from app.routers.testers import random_number_generator
|
| 14 |
from app.routers.testers import help
|
| 15 |
from app.routers.testers import calculator
|
| 16 |
|
| 17 |
+
|
| 18 |
+
# ==========================
|
| 19 |
+
# π App Initialization
|
| 20 |
+
# ==========================
|
| 21 |
app = FastAPI()
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# ==========================
|
| 25 |
+
# π Router Registration
|
| 26 |
+
# ==========================
|
| 27 |
+
app.include_router(jpgcompressor.router) # Image compression handler
|
| 28 |
+
app.include_router(random_number_generator.router) # Random number tester
|
| 29 |
+
app.include_router(help.router) # Helper/test info
|
| 30 |
+
app.include_router(calculator.router) # Mini calculator service
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# ==========================
|
| 34 |
+
# π Root Endpoint
|
| 35 |
+
# ==========================
|
| 36 |
+
@app.get("/", response_class=HTMLResponse)
|
| 37 |
def greet_json():
|
| 38 |
+
return """
|
| 39 |
+
<h2>Go to the Swagger docs π</h2>
|
| 40 |
+
<a href="/docs">Click here for API Docs</a>
|
| 41 |
+
"""
|
app/routers/image/jpgcompressor.py
CHANGED
|
@@ -3,58 +3,74 @@ from fastapi.responses import StreamingResponse
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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 |
-
#
|
|
|
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
| 52 |
return StreamingResponse(
|
| 53 |
-
output_buffer,
|
| 54 |
-
media_type="image/jpeg",
|
| 55 |
-
headers={
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
-
#
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from io import BytesIO
|
| 5 |
|
| 6 |
+
|
| 7 |
+
# ==========================
|
| 8 |
+
# πΌοΈ Image Processing Router
|
| 9 |
+
# ==========================
|
| 10 |
router = APIRouter(
|
| 11 |
prefix="/image",
|
| 12 |
tags=["Image Processing"]
|
| 13 |
)
|
| 14 |
|
| 15 |
+
|
| 16 |
+
# ==========================
|
| 17 |
+
# π¦ JPG Compression Endpoint
|
| 18 |
+
# ==========================
|
| 19 |
@router.post("/compress_jpg")
|
| 20 |
async def compress_jpg(
|
| 21 |
file: UploadFile = File(..., description="The JPG image file to compress."),
|
| 22 |
quality: int = Form(75, description="JPEG compression quality (1 to 100). Default is 75.")
|
| 23 |
):
|
| 24 |
"""
|
| 25 |
+
π§ Compresses a JPEG image at a specified quality and returns the optimized file.
|
|
|
|
| 26 |
"""
|
| 27 |
+
|
| 28 |
+
# --------------------------
|
| 29 |
+
# π Validation Checks
|
| 30 |
+
# --------------------------
|
| 31 |
if file.content_type != "image/jpeg":
|
| 32 |
raise HTTPException(
|
| 33 |
+
status_code=400,
|
| 34 |
detail="Invalid file type. Only JPEG files are supported for this endpoint."
|
| 35 |
)
|
| 36 |
|
| 37 |
if not 1 <= quality <= 100:
|
| 38 |
raise HTTPException(
|
| 39 |
+
status_code=400,
|
| 40 |
detail="Quality must be an integer between 1 and 100."
|
| 41 |
)
|
| 42 |
|
| 43 |
+
# --------------------------
|
| 44 |
+
# π₯ Read Image Bytes
|
| 45 |
+
# --------------------------
|
| 46 |
content = await file.read()
|
|
|
|
|
|
|
| 47 |
input_buffer = BytesIO(content)
|
| 48 |
output_buffer = BytesIO()
|
| 49 |
+
|
| 50 |
try:
|
| 51 |
+
# --------------------------
|
| 52 |
+
# π§ͺ Pillow Compression
|
| 53 |
+
# --------------------------
|
| 54 |
img = Image.open(input_buffer)
|
|
|
|
|
|
|
| 55 |
img.save(output_buffer, "JPEG", quality=quality)
|
|
|
|
|
|
|
| 56 |
output_buffer.seek(0)
|
| 57 |
+
|
| 58 |
+
# --------------------------
|
| 59 |
+
# π€ Return as Stream
|
| 60 |
+
# --------------------------
|
| 61 |
return StreamingResponse(
|
| 62 |
+
output_buffer,
|
| 63 |
+
media_type="image/jpeg",
|
| 64 |
+
headers={
|
| 65 |
+
"Content-Disposition": f"attachment; filename=compressed_{file.filename}"
|
| 66 |
+
}
|
| 67 |
)
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
+
# --------------------------
|
| 71 |
+
# π₯ Error Handling
|
| 72 |
+
# --------------------------
|
| 73 |
+
raise HTTPException(
|
| 74 |
+
status_code=500,
|
| 75 |
+
detail=f"Image processing error: {e}"
|
| 76 |
+
)
|