Spaces:
Runtime error

Kaverin Maxim commited on
Commit
c7ec9aa
·
1 Parent(s): 6ac746d

Add text prompt

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -29,10 +29,20 @@ def run(text, input_image, n_pictures):
29
  inputs = processor(images=image, return_tensors="pt")
30
  with torch.no_grad():
31
  image_embedding = model.get_image_features(**inputs)
 
32
 
33
- # Конвертируем тензор в numpy для совместимости с Annoy
34
- image_embedding = image_embedding.cpu().numpy().flatten()
35
- indices = index.get_nns_by_vector(image_embedding, n_pictures)
 
 
 
 
 
 
 
 
 
36
 
37
  files = []
38
  for idx in indices:
@@ -51,7 +61,7 @@ with gr.Blocks(
51
  with gr.Column():
52
  with gr.Row(equal_height=True):
53
  # input images
54
- image_input_1 = gr.Image(label="Картинка", type="filepath", sources=['upload', 'clipboard'])
55
 
56
  # text prompt
57
  pictures_slider = gr.Slider(
@@ -59,7 +69,7 @@ with gr.Blocks(
59
  )
60
 
61
  prompt_input = gr.Textbox(
62
- label="Введите что-то", placeholder="Напишите сюда что-нибудь"
63
  )
64
 
65
  # generate
@@ -74,7 +84,7 @@ with gr.Blocks(
74
  run,
75
  inputs=[
76
  prompt_input,
77
- image_input_1,
78
  pictures_slider
79
  ],
80
  outputs=gallery
 
29
  inputs = processor(images=image, return_tensors="pt")
30
  with torch.no_grad():
31
  image_embedding = model.get_image_features(**inputs)
32
+ image_embedding = image_embedding.cpu().numpy().flatten()
33
 
34
+ final_embedding = image_embedding
35
+
36
+ text = text.strip()
37
+ if text:
38
+ inputs = processor(text=text, return_tensors="pt")
39
+ with torch.no_grad():
40
+ text_embedding = model.get_text_features(**inputs)
41
+ text_embedding = text_embedding.cpu().numpy().flatten()
42
+
43
+ final_embedding += text_embedding * 0.2
44
+
45
+ indices = index.get_nns_by_vector(final_embedding, n_pictures)
46
 
47
  files = []
48
  for idx in indices:
 
61
  with gr.Column():
62
  with gr.Row(equal_height=True):
63
  # input images
64
+ image_input = gr.Image(label="Картинка", type="filepath", sources=['upload', 'clipboard'])
65
 
66
  # text prompt
67
  pictures_slider = gr.Slider(
 
69
  )
70
 
71
  prompt_input = gr.Textbox(
72
+ label="Уточнение изображения на английском (опционально)", placeholder="A photo of a cute dog"
73
  )
74
 
75
  # generate
 
84
  run,
85
  inputs=[
86
  prompt_input,
87
+ image_input,
88
  pictures_slider
89
  ],
90
  outputs=gallery