AshanGimhana commited on
Commit
19a2d28
1 Parent(s): 1e56be4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -34
app.py CHANGED
@@ -1,39 +1,19 @@
1
  import os
2
  import subprocess
3
 
4
- # Install Gradio
5
  os.system("pip install gradio==3.50")
6
 
7
- # Create and run setup script for CUDA environment variables
8
- with open('setup.sh', 'w') as f:
9
- f.write('''#!/bin/bash
10
- export CUDA_HOME=/usr/local/cuda
11
- export PATH=$CUDA_HOME/bin:$PATH
12
- export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH
13
- ''')
14
-
15
- # Make the script executable
16
- os.system('chmod +x setup.sh')
17
-
18
- # Run the setup script to set environment variables
19
- os.system('./setup.sh')
20
-
21
- import torch
22
-
23
- # Check CUDA availability
24
- is_cuda_available = torch.cuda.is_available()
25
- device = 'cuda' if is_cuda_available else 'cpu'
26
- print("CUDA available:", is_cuda_available)
27
 
28
  from argparse import Namespace
29
  import pprint
30
  import numpy as np
31
  from PIL import Image
 
32
  import torchvision.transforms as transforms
33
  import cv2
34
  import dlibs.dlib
35
  import matplotlib.pyplot as plt
36
- import gradio as gr
37
  from tensorflow.keras.preprocessing.image import img_to_array
38
  from huggingface_hub import hf_hub_download, login
39
  from datasets.augmentations import AgeTransformer
@@ -69,14 +49,14 @@ EXPERIMENT_DATA_ARGS = {
69
  }
70
  EXPERIMENT_ARGS = EXPERIMENT_DATA_ARGS[EXPERIMENT_TYPE]
71
  model_path = EXPERIMENT_ARGS['model_path']
72
- ckpt = torch.load(model_path, map_location=device)
73
  opts = ckpt['opts']
74
  pprint.pprint(opts)
75
  opts['checkpoint_path'] = model_path
76
  opts = Namespace(**opts)
77
  net = pSp(opts)
78
  net.eval()
79
- net.to(device) # Send model to the appropriate device (GPU or CPU)
80
 
81
  print('Model successfully loaded!')
82
 
@@ -151,9 +131,9 @@ def apply_aging(image, target_age):
151
  results = []
152
  for age_transformer in age_transformers:
153
  with torch.no_grad():
154
- input_image_age = [age_transformer(input_image.cpu()).to(device)] # Move to appropriate device
155
  input_image_age = torch.stack(input_image_age)
156
- result_tensor = net(input_image_age.to(device).float(), randomize_noise=False, resize=False)[0]
157
  result_image = tensor2im(result_tensor)
158
  results.append(np.array(result_image))
159
  final_result = results[0]
@@ -186,13 +166,10 @@ def process_image(uploaded_image):
186
 
187
  iface = gr.Interface(
188
  fn=process_image,
189
- inputs=gr.Image(type="pil", label="Upload an Image"),
190
- outputs=[
191
- gr.Image(label="Image with Good Teeth"),
192
- gr.Image(label="Image with Bad Teeth")
193
- ],
194
- title="Age Transformation and Teeth Replacement",
195
- description="Upload an image and apply aging effects while replacing teeth."
196
  )
197
 
198
- iface.launch(share=True)
 
1
  import os
2
  import subprocess
3
 
 
4
  os.system("pip install gradio==3.50")
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  from argparse import Namespace
8
  import pprint
9
  import numpy as np
10
  from PIL import Image
11
+ import torch
12
  import torchvision.transforms as transforms
13
  import cv2
14
  import dlibs.dlib
15
  import matplotlib.pyplot as plt
16
+ import gradio as gr # Importing Gradio as gr
17
  from tensorflow.keras.preprocessing.image import img_to_array
18
  from huggingface_hub import hf_hub_download, login
19
  from datasets.augmentations import AgeTransformer
 
49
  }
50
  EXPERIMENT_ARGS = EXPERIMENT_DATA_ARGS[EXPERIMENT_TYPE]
51
  model_path = EXPERIMENT_ARGS['model_path']
52
+ ckpt = torch.load(model_path, map_location='cpu')
53
  opts = ckpt['opts']
54
  pprint.pprint(opts)
55
  opts['checkpoint_path'] = model_path
56
  opts = Namespace(**opts)
57
  net = pSp(opts)
58
  net.eval()
59
+ net.cuda()
60
 
61
  print('Model successfully loaded!')
62
 
 
131
  results = []
132
  for age_transformer in age_transformers:
133
  with torch.no_grad():
134
+ input_image_age = [age_transformer(input_image.cpu()).to('cuda')]
135
  input_image_age = torch.stack(input_image_age)
136
+ result_tensor = net(input_image_age.to("cuda").float(), randomize_noise=False, resize=False)[0]
137
  result_image = tensor2im(result_tensor)
138
  results.append(np.array(result_image))
139
  final_result = results[0]
 
166
 
167
  iface = gr.Interface(
168
  fn=process_image,
169
+ inputs=gr.Image(type="pil"),
170
+ outputs=[gr.Image(type="pil"), gr.Image(type="pil")],
171
+ title="Aging Effect with Teeth Replacement",
172
+ description="Upload an image to apply an aging effect. The application will generate two results: one with good teeth and one with bad teeth."
 
 
 
173
  )
174
 
175
+ iface.launch(debug=True)