LapStore commited on
Commit
b1f46e5
·
1 Parent(s): 7126c79
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +79 -0
  3. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.9
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI ,Request ,Form, UploadFile, File
2
+ from fastapi.responses import JSONResponse
3
+ from fastapi.responses import HTMLResponse, FileResponse
4
+ import os
5
+ import io
6
+ from PIL import ImageOps,Image ,ImageFilter
7
+ #from transformers import pipeline
8
+ import matplotlib.pyplot as plt
9
+ import numpy as np
10
+ import ast
11
+ #http://localhost:8000
12
+ app = FastAPI()
13
+
14
+ # Root route
15
+ @app.get('/')
16
+ def hello_world():
17
+ return "Hello World taha"
18
+
19
+ def get_segment_image(raw_image):
20
+ #pipe = pipeline("image-segmentation", model="Intel/dpt-large-ade")
21
+ #output = pipe(raw_image, points_per_batch=32)
22
+ #return output
23
+ pass
24
+
25
+ def get_supported_segmentation(output,supported_types):
26
+ return [obj for obj in output if (obj['label'] in supported_types)]
27
+
28
+
29
+ @app.post('/predict')
30
+ async def predict(supported_types_str: str = Form(),age: str = Form() , file: UploadFile = File(...)):
31
+ # Form(...) to accept input as web form ,may change when android /upload
32
+
33
+ supported_types=ast.literal_eval(supported_types_str)
34
+
35
+ contents = await file.read()
36
+ image = Image.open(io.BytesIO(contents))
37
+
38
+ # Process the image (example: convert to grayscale)
39
+ processed_image = image.convert("L")
40
+
41
+ # Save the processed image to a temporary file
42
+ output_file_path = "tmp_processed_image.png"
43
+ processed_image.save(output_file_path)
44
+
45
+ # Return the processed image for download
46
+ return FileResponse(output_file_path, media_type='image/png', filename="tmp_processed_image.png")
47
+
48
+
49
+
50
+
51
+
52
+ @app.post('/predict2')
53
+ async def predict2(supported_types_str: str = Form(...), age: str = Form(...), file: UploadFile = File(...)):
54
+
55
+ contents = await file.read()
56
+ image = Image.open(io.BytesIO(contents))
57
+
58
+ # Process the image (example: convert to grayscale)
59
+ processed_image = image.convert("L")
60
+
61
+ # Save the processed image to a BytesIO object
62
+ img_byte_arr = io.BytesIO()
63
+ processed_image.save(img_byte_arr, format='PNG')
64
+ img_byte_arr.seek(0) # Move to the beginning of the BytesIO buffer
65
+
66
+ # Create an HTML response with the processed image
67
+ html_content = f"""
68
+ <html>
69
+ <body>
70
+ <h3>Processed Image:</h3>
71
+ <img src="data:image/png;base64,{img_byte_arr.getvalue().decode('latin1')}" alt="Processed Image" style="max-width: 500px;"/>
72
+ <br><br>
73
+ <p>Your name: {supported_types_str}</p>
74
+ <p>Your age: {age}</p>
75
+ <p><a href="/download">Download Processed Image</a></p>
76
+ </body>
77
+ </html>
78
+ """
79
+ return HTMLResponse(content=html_content)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ Pillow
4
+ transformers
5
+ matplotlib
6
+ numpy
7
+ python-multipart