sameerbanchhor commited on
Commit
9b32714
Β·
verified Β·
1 Parent(s): 9596ac1

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. .gitignore +4 -1
  2. app/main.py +32 -10
  3. 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
- # 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"}
 
 
 
 
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
- # 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}")
 
 
 
 
 
 
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
+ )