Pumponchik commited on
Commit
ecffe29
·
verified ·
1 Parent(s): 00ef496

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -4,20 +4,27 @@ import gradio as gr
4
  from PIL import Image
5
  import pickle
6
  import json
7
- from huggingface_hub import hf_hub_download
8
  from model import image_captioning_model
9
 
 
 
 
10
  # Загрузка файлов
11
- # https://huggingface.co/spaces/Pumponchik/ImageCaption/tree/main
12
  repo_id = "Pumponchik/ImageCaption"
13
- model_path = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin")
14
- config_path = hf_hub_download(repo_id=repo_id, filename="config.json")
15
- tokenizer_path = hf_hub_download(repo_id=repo_id, filename="tokenizer.pkl")
16
- transform_path = hf_hub_download(repo_id=repo_id, filename="image_transforms.pkl")
 
 
 
 
17
 
18
  # Загрузка конфигурации
19
  with open(config_path, "r") as f:
20
  config = json.load(f)
 
21
 
22
  # Загрузка модели
23
  model = image_captioning_model(**config)
@@ -54,9 +61,9 @@ def generate(model, image, tokenizer, max_seq_len=20, top_p=None, top_k=1):
54
  batch_size = image.size(0)
55
 
56
  with torch.no_grad():
57
- img_emb = model.img_features(image) # [batch_size, 512]
58
- h_0 = model.text_features.img_to_hidden(img_emb) # [batch_size, hidden_size]
59
- h_0 = h_0.unsqueeze(0).repeat(model.text_features.num_layers, 1, 1) # [num_layers, batch_size, hidden_size]
60
  c_0 = h_0.clone()
61
 
62
  input_token = torch.full((batch_size,), tokenizer.tok_to_ind['<BOS>'], dtype=torch.long, device=device)
@@ -64,11 +71,11 @@ def generate(model, image, tokenizer, max_seq_len=20, top_p=None, top_k=1):
64
  mask = torch.ones(batch_size, dtype=torch.bool, device=device)
65
 
66
  for _ in range(max_seq_len):
67
- input_emb = model.text_features.embed(input_token).unsqueeze(1) # [batch_size, 1, embed_dim]
68
  input_emb = model.text_features.text_to_hidden(input_emb)
69
- output, (h_0, c_0) = model.text_features.rnn(input_emb, (h_0, c_0)) # [batch_size, 1, hidden_size]
70
- logits = model.fc_out(output.squeeze(1)) # [batch_size, vocab_size]
71
- probs = F.softmax(logits, dim=-1) # [batch_size, vocab_size]
72
 
73
  next_tokens = torch.zeros(batch_size, dtype=torch.long, device=device)
74
  for i in range(batch_size):
@@ -103,7 +110,6 @@ def generate(model, image, tokenizer, max_seq_len=20, top_p=None, top_k=1):
103
  if not mask.any():
104
  break
105
 
106
- # Декодирование токенов в текст
107
  result_text = []
108
  for tokens in result_tokens:
109
  caption = " ".join([tokenizer.ind_to_tok.get(t, "<UNK>") for t in tokens
 
4
  from PIL import Image
5
  import pickle
6
  import json
7
+ from huggingface_hub import hf_hub_download, login
8
  from model import image_captioning_model
9
 
10
+ # Аутентификация (если репозиторий приватный)
11
+ # login(token="hf_YOUR_API_TOKEN") # Замените на ваш токен или уберите, если публичный
12
+
13
  # Загрузка файлов
 
14
  repo_id = "Pumponchik/ImageCaption"
15
+ repo_type = "space"
16
+ model_path = hf_hub_download(repo_id=repo_id, filename="pytorch_model.bin", repo_type=repo_type)
17
+ config_path = hf_hub_download(repo_id=repo_id, filename="config.json", repo_type=repo_type)
18
+ tokenizer_path = hf_hub_download(repo_id=repo_id, filename="tokenizer.pkl", repo_type=repo_type)
19
+ transform_path = hf_hub_download(repo_id=repo_id, filename="image_transforms.pkl", repo_type=repo_type)
20
+
21
+ print(f"Model path: {model_path}") # Отладка
22
+ print(f"Config path: {config_path}") # Отладка
23
 
24
  # Загрузка конфигурации
25
  with open(config_path, "r") as f:
26
  config = json.load(f)
27
+ print(f"Loaded config: {config}") # Отладка
28
 
29
  # Загрузка модели
30
  model = image_captioning_model(**config)
 
61
  batch_size = image.size(0)
62
 
63
  with torch.no_grad():
64
+ img_emb = model.img_features(image)
65
+ h_0 = model.text_features.img_to_hidden(img_emb)
66
+ h_0 = h_0.unsqueeze(0).repeat(model.text_features.num_layers, 1, 1)
67
  c_0 = h_0.clone()
68
 
69
  input_token = torch.full((batch_size,), tokenizer.tok_to_ind['<BOS>'], dtype=torch.long, device=device)
 
71
  mask = torch.ones(batch_size, dtype=torch.bool, device=device)
72
 
73
  for _ in range(max_seq_len):
74
+ input_emb = model.text_features.embed(input_token).unsqueeze(1)
75
  input_emb = model.text_features.text_to_hidden(input_emb)
76
+ output, (h_0, c_0) = model.text_features.rnn(input_emb, (h_0, c_0))
77
+ logits = model.fc_out(output.squeeze(1))
78
+ probs = F.softmax(logits, dim=-1)
79
 
80
  next_tokens = torch.zeros(batch_size, dtype=torch.long, device=device)
81
  for i in range(batch_size):
 
110
  if not mask.any():
111
  break
112
 
 
113
  result_text = []
114
  for tokens in result_tokens:
115
  caption = " ".join([tokenizer.ind_to_tok.get(t, "<UNK>") for t in tokens