mrbeliever commited on
Commit
09f16a9
·
verified ·
1 Parent(s): f684c32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
app.py CHANGED
@@ -1,26 +1,38 @@
1
- from fastapi import FastAPI, UploadFile, File, HTTPException
2
- import os
3
- import google.generativeai as genai
4
- from PIL import Image
5
- from io import BytesIO
6
 
7
  app = FastAPI()
8
 
9
- GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')
 
 
 
10
 
11
- if not GOOGLE_API_KEY:
12
- raise ValueError("GOOGLE_API_KEY environment variable not set")
13
 
14
- genai.configure(api_key=GOOGLE_API_KEY)
15
- model = genai.GenerativeModel('gemini-1.5-flash')
16
 
17
- @app.post("/generate-caption/")
18
- async def generate_caption(file: UploadFile = File(...)):
19
- try:
20
- image_data = await file.read()
21
- image = Image.open(BytesIO(image_data))
22
-
23
- response = model.generate_content(image)
24
- return {"caption": response.text}
25
- except Exception as e:
26
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import StreamingResponse
3
+ from PIL import Image, ImageOps, ImageFilter
4
+ import io
5
+ import numpy as np
6
 
7
  app = FastAPI()
8
 
9
+ @app.post("/")
10
+ async def convert_to_sketch(file: UploadFile = File(...)):
11
+ # Load the image
12
+ image = Image.open(io.BytesIO(await file.read()))
13
 
14
+ # Convert to grayscale
15
+ grayscale = ImageOps.grayscale(image)
16
 
17
+ # Invert the grayscale image
18
+ inverted = ImageOps.invert(grayscale)
19
 
20
+ # Apply Gaussian blur to the inverted image
21
+ blurred = inverted.filter(ImageFilter.GaussianBlur(15))
22
+
23
+ # Blend the grayscale image with the blurred inverted image using color dodge
24
+ blended = Image.blend(grayscale, blurred, 0.5)
25
+
26
+ # Enhance the contrast (optional)
27
+ enhanced = ImageOps.autocontrast(blended, cutoff=2)
28
+
29
+ # Save the resulting image to a bytes buffer
30
+ buf = io.BytesIO()
31
+ enhanced.save(buf, format='PNG')
32
+ buf.seek(0)
33
+
34
+ return StreamingResponse(buf, media_type="image/png")
35
+
36
+ if __name__ == "__main__":
37
+ import uvicorn
38
+ uvicorn.run(app, host="0.0.0.0", port=8000)