Spaces:
Runtime error
Runtime error
New app file
Browse files
app.py
CHANGED
|
@@ -1,9 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
return "Hello " + name + "!!"
|
| 6 |
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import torch
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
from fastai.vision.all import *
|
| 6 |
import gradio as gr
|
| 7 |
|
| 8 |
+
############### HF ###########################
|
| 9 |
|
| 10 |
+
HF_TOKEN = os.getenv('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')
|
|
|
|
| 11 |
|
| 12 |
+
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "savtadepth-flags")
|
| 13 |
|
| 14 |
+
############## DVC ################################
|
| 15 |
+
|
| 16 |
+
PROD_MODEL_PATH = "src/models"
|
| 17 |
+
TRAIN_PATH = "src/data/processed/train/bathroom"
|
| 18 |
+
TEST_PATH = "src/data/processed/test/bathroom"
|
| 19 |
+
|
| 20 |
+
if os.path.isdir(".dvc"):
|
| 21 |
+
print("Running DVC")
|
| 22 |
+
os.system("dvc config cache.type copy")
|
| 23 |
+
os.system("dvc config core.no_scm true")
|
| 24 |
+
if os.system(f"dvc pull {PROD_MODEL_PATH} {TRAIN_PATH } {TEST_PATH }") != 0:
|
| 25 |
+
exit("dvc pull failed")
|
| 26 |
+
os.system("rm -r .dvc")
|
| 27 |
+
# .apt/usr/lib/dvc
|
| 28 |
+
|
| 29 |
+
############## Inference ##############################
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class ImageImageDataLoaders(DataLoaders):
|
| 33 |
+
"""Basic wrapper around several `DataLoader`s with factory methods for Image to Image problems"""
|
| 34 |
+
@classmethod
|
| 35 |
+
@delegates(DataLoaders.from_dblock)
|
| 36 |
+
def from_label_func(cls, path, filenames, label_func, valid_pct=0.2, seed=None, item_transforms=None,
|
| 37 |
+
batch_transforms=None, **kwargs):
|
| 38 |
+
"""Create from list of `fnames` in `path`s with `label_func`."""
|
| 39 |
+
datablock = DataBlock(blocks=(ImageBlock(cls=PILImage), ImageBlock(cls=PILImageBW)),
|
| 40 |
+
get_y=label_func,
|
| 41 |
+
splitter=RandomSplitter(valid_pct, seed=seed),
|
| 42 |
+
item_tfms=item_transforms,
|
| 43 |
+
batch_tfms=batch_transforms)
|
| 44 |
+
res = cls.from_dblock(datablock, filenames, path=path, **kwargs)
|
| 45 |
+
return res
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def get_y_fn(x):
|
| 49 |
+
y = str(x.absolute()).replace('.jpg', '_depth.png')
|
| 50 |
+
y = Path(y)
|
| 51 |
+
|
| 52 |
+
return y
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def create_data(data_path):
|
| 56 |
+
fnames = get_files(data_path/'train', extensions='.jpg')
|
| 57 |
+
data = ImageImageDataLoaders.from_label_func(
|
| 58 |
+
data_path/'train', seed=42, bs=4, num_workers=0, filenames=fnames, label_func=get_y_fn)
|
| 59 |
+
return data
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
data = create_data(Path('src/data/processed'))
|
| 63 |
+
learner = unet_learner(data, resnet34, metrics=rmse,
|
| 64 |
+
wd=1e-2, n_out=3, loss_func=MSELossFlat(), path='src/')
|
| 65 |
+
learner.load('model')
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def gen(input_img):
|
| 69 |
+
return PILImageBW.create((learner.predict(input_img))[0]).convert('L')
|
| 70 |
+
|
| 71 |
+
################### Gradio Web APP ################################
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
title = "SavtaDepth WebApp"
|
| 75 |
+
|
| 76 |
+
description = """
|
| 77 |
+
<p>
|
| 78 |
+
<center>
|
| 79 |
+
Savta Depth is a collaborative Open Source Data Science project for monocular depth estimation - Turn 2d photos into 3d photos. To test the model and code please check out the link bellow.
|
| 80 |
+
<img src="https://huggingface.co/spaces/kingabzpro/savtadepth/resolve/main/examples/cover.png" alt="logo" width="250"/>
|
| 81 |
+
</center>
|
| 82 |
+
</p>
|
| 83 |
+
"""
|
| 84 |
+
article = "<p style='text-align: center'><a href='https://dagshub.com/OperationSavta/SavtaDepth' target='_blank'>SavtaDepth Project from OperationSavta</a></p><p style='text-align: center'><a href='https://colab.research.google.com/drive/1XU4DgQ217_hUMU1dllppeQNw3pTRlHy1?usp=sharing' target='_blank'>Google Colab Demo</a></p></center></p>"
|
| 85 |
+
|
| 86 |
+
examples = [
|
| 87 |
+
["examples/00008.jpg"],
|
| 88 |
+
["examples/00045.jpg"],
|
| 89 |
+
]
|
| 90 |
+
favicon = "examples/favicon.ico"
|
| 91 |
+
thumbnail = "examples/SavtaDepth.png"
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def main():
|
| 95 |
+
iface = gr.Interface(
|
| 96 |
+
gen,
|
| 97 |
+
gr.inputs.Image(shape=(640, 480), type='numpy'),
|
| 98 |
+
"image",
|
| 99 |
+
title=title,
|
| 100 |
+
flagging_options=["incorrect", "worst", "ambiguous"],
|
| 101 |
+
allow_flagging="manual",
|
| 102 |
+
flagging_callback=hf_writer,
|
| 103 |
+
description=description,
|
| 104 |
+
article=article,
|
| 105 |
+
examples=examples,
|
| 106 |
+
theme="peach",
|
| 107 |
+
allow_screenshot=True
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
iface.launch(enable_queue=True)
|
| 111 |
+
# enable_queue=True,auth=("admin", "pass1234")
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
if __name__ == '__main__':
|
| 115 |
+
main()
|