Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import hf_hub_url, hf_hub_download
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
import requests
|
6 |
+
|
7 |
+
import torch
|
8 |
+
from torchvision import transforms
|
9 |
+
from torch.autograd import Variable
|
10 |
+
|
11 |
+
from PIL import Image
|
12 |
+
|
13 |
+
import warnings
|
14 |
+
warnings.filterwarnings('ignore')
|
15 |
+
|
16 |
+
path_to_model = hf_hub_download(repo_id="opetrova/face-frontalization", filename="generator_v0.pt")
|
17 |
+
|
18 |
+
# Download network.py into the current directory
|
19 |
+
network_url = hf_hub_url(repo_id="opetrova/face-frontalization", filename="network.py")
|
20 |
+
r = requests.get(network_url, allow_redirects=True)
|
21 |
+
open('network.py', 'wb').write(r.content)
|
22 |
+
|
23 |
+
saved_model = torch.load(path_to_model, map_location=torch.device('cpu'))
|
24 |
+
|
25 |
+
def frontalize(image):
|
26 |
+
|
27 |
+
# Convert the test image to a [1, 3, 128, 128]-shaped torch tensor
|
28 |
+
# (as required by the frontalization model)
|
29 |
+
preprocess = transforms.Compose((transforms.ToPILImage(),
|
30 |
+
transforms.Resize(size = (128, 128)),
|
31 |
+
transforms.ToTensor()))
|
32 |
+
input_tensor = torch.unsqueeze(preprocess(image), 0)
|
33 |
+
|
34 |
+
# Use the saved model to generate an output (whose values go between -1 and 1,
|
35 |
+
# and this will need to get fixed before the output is displayed)
|
36 |
+
generated_image = saved_model(Variable(input_tensor.type('torch.FloatTensor')))
|
37 |
+
generated_image = generated_image.detach().squeeze().permute(1, 2, 0).numpy()
|
38 |
+
generated_image = (generated_image + 1.0) / 2.0
|
39 |
+
|
40 |
+
return generated_image
|
41 |
+
|
42 |
+
iface = gr.Interface(frontalize, gr.inputs.Image(type="numpy"), "image")
|
43 |
+
iface.launch()
|