-
Notifications
You must be signed in to change notification settings - Fork 6
/
views.py
75 lines (63 loc) · 2.62 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
import copy
from .forms import FileForm
from .models import File
from .prediction import *
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class CustomUnpickler(pickle.Unpickler):
def find_class(self, module, name):
if name == 'Vocabulary':
return Vocabulary
return super().find_class(module, name)
vocab = CustomUnpickler(open('/home/harshit/Downloads/vocab.pkl', 'rb')).load()
def handle_file(image):
"""process uploaded file"""
# img = PIL.Image.open(image)
# print(img.size)
pass
def index(request):
"""homepage"""
if request.method == 'POST':
form = FileForm(request.POST, request.FILES)
if form.is_valid():
myfile = File(file=request.FILES['file'])
form.save()
file_name = myfile.file # file attribute of File model object
print(file_name)
file_name_original = copy.copy(file_name) # prevent name modification after saving file
handle_file(myfile)
# process image
output = process(file_name,
'/home/harshit/Downloads/pretrained_model/encoder-5-3000.pkl',
'/home/harshit/Downloads/pretrained_model/decoder-5-3000.pkl',
vocab,
256, 512, 1)
print(output)
myfile.save()
print(file_name)
return render(request, 'captionApp/output.html', {'file_name': file_name,
'file_name_original': file_name_original,
'output': output})
# return HttpResponse('File successfully uploaded!')
# else:
# return HttpResponse('Something Went Wrong!')
else:
form = FileForm()
return render(request, 'captionApp/index.html', {'form': form})
### For heroku ###
# def index(request):
# """homepage"""
# if request.method == 'POST':
# form = FileForm(request.POST, request.FILES)
# if form.is_valid():
# myfile = request.FILES['file']
# print(myfile)
# handle_file(myfile)
# return render(request, 'captionApp/output.html', {'file': myfile})
# # return HttpResponse('File successfully uploaded!')
# # else:
# # return HttpResponse('Something Went Wrong!')
# else:
# form = FileForm()
# return render(request, 'captionApp/index.html', {'form': form})