Spaces:
Runtime error
Runtime error
Pranav Patel
commited on
Commit
•
182a984
1
Parent(s):
b78523c
Add application file
Browse files- app.py +46 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
|
4 |
+
inimg = gr.Image()
|
5 |
+
outimg = gr.AnnotatedImage()
|
6 |
+
custom_html = '''
|
7 |
+
<center>
|
8 |
+
<div style="overflow:hidden ; max-width: fit-content; margin-left: auto; margin-right: auto;">
|
9 |
+
<div style="float: left; font-family: Arial; font-size: 25px; color: orange; margin: 10px;">Please</div>
|
10 |
+
<div style="float: left">
|
11 |
+
<a href="https://www.buymeacoffee.com/alloc7260">
|
12 |
+
<img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=☕&slug=alloc7260&button_colour=FFDD00&font_colour=000000&font_family=Arial&outline_colour=000000&coffee_colour=ffffff" />
|
13 |
+
</a>
|
14 |
+
</div>
|
15 |
+
<div style="float: left; font-family: Arial; font-size: 25px; color: orange; margin: 10px;">for upgrade to gpu instance</div>
|
16 |
+
</div>
|
17 |
+
</center>
|
18 |
+
'''
|
19 |
+
|
20 |
+
def resize_image(image, max_width=1500):
|
21 |
+
original_width, original_height = image.size
|
22 |
+
aspect_ratio = original_width / original_height
|
23 |
+
new_width = min(original_width, max_width)
|
24 |
+
new_height = int(new_width / aspect_ratio)
|
25 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
26 |
+
return resized_image
|
27 |
+
|
28 |
+
def mask(img):
|
29 |
+
PIL_image = Image.fromarray(img.astype('uint8'), 'RGB')
|
30 |
+
resimg = resize_image(PIL_image)
|
31 |
+
import torch
|
32 |
+
from transformers import pipeline
|
33 |
+
with torch.inference_mode():
|
34 |
+
generator = pipeline("mask-generation", "facebook/sam-vit-base", points_per_batch = 64)
|
35 |
+
outputs = generator(resimg, points_per_batch = 64)
|
36 |
+
outputs_masks = outputs['masks']
|
37 |
+
torch.cuda.empty_cache()
|
38 |
+
return (resimg, [(outputs_masks[i], str(i + 1)) for i in range(len(outputs_masks))])
|
39 |
+
|
40 |
+
interface = gr.Blocks()
|
41 |
+
|
42 |
+
with interface:
|
43 |
+
gr.Interface(mask, inimg, outimg)
|
44 |
+
che = gr.HTML(custom_html)
|
45 |
+
|
46 |
+
interface.launch(show_api=False, debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch==2.2.1
|
2 |
+
transformers==4.38.2
|
3 |
+
Pillow==9.4.0
|
4 |
+
gradio==4.25.0
|