vam
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -28,8 +28,14 @@ def generate_meme_caption(topic):
|
|
28 |
Answer from previous question can not be the same for this time prompt.
|
29 |
If the user does not specify their caption request, you can generate a short caption for the meme.
|
30 |
But if they do, you cannot replace their prompt; just need to copy their request and make it the caption.
|
31 |
-
Now generate a caption based on this topic: {topic}.
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
}
|
34 |
],
|
35 |
max_tokens=250,
|
@@ -41,30 +47,22 @@ def generate_meme_caption(topic):
|
|
41 |
|
42 |
# Function to create meme with caption
|
43 |
def create_meme(prompt):
|
44 |
-
# Tạo embedding cho prompt
|
45 |
prompt_embedding = model.encode([prompt])
|
46 |
|
47 |
-
# Tính toán độ tương đồng giữa prompt và các văn bản
|
48 |
similarities = model.similarity(prompt_embedding, doc_embeddings)
|
49 |
|
50 |
-
# Lấy chỉ số văn bản có độ tương đồng cao nhất
|
51 |
closest_text_idx = np.argmax(similarities)
|
52 |
|
53 |
-
# Tải ảnh meme tương ứng
|
54 |
img_path = meme_filename[closest_text_idx]
|
55 |
img = mpimg.imread(f'/kaggle/input/memedata/{img_path}')
|
56 |
|
57 |
-
# Tạo khoảng trắng để hiển thị caption
|
58 |
white_space_height = 100
|
59 |
white_space = np.ones((white_space_height, img.shape[1], img.shape[2]), dtype=img.dtype) * 255
|
60 |
|
61 |
-
# Kết hợp khoảng trắng với hình ảnh gốc
|
62 |
combined_img = np.vstack((white_space, img))
|
63 |
|
64 |
-
# Sinh caption từ GPT model
|
65 |
caption = generate_meme_caption(prompt)
|
66 |
|
67 |
-
# Hiển thị ảnh với caption
|
68 |
plt.imshow(combined_img)
|
69 |
plt.axis('off')
|
70 |
plt.text(
|
|
|
28 |
Answer from previous question can not be the same for this time prompt.
|
29 |
If the user does not specify their caption request, you can generate a short caption for the meme.
|
30 |
But if they do, you cannot replace their prompt; just need to copy their request and make it the caption.
|
31 |
+
Now generate a caption based on this topic: {topic}.
|
32 |
+
For example:
|
33 |
+
- No request: Generate me any anime meme--> You can generate a caption based on the topic they requested.
|
34 |
+
- With request: Generate me a meme of 5 different opinions like the Gru meme with 5 paintings.
|
35 |
+
First opinion says 1, second says 2, third says 3, fourth says 4, fifth says 5.
|
36 |
+
--> You will then generate 5 captions or 5 opinions for users, separating every opinion with the mark "/".
|
37 |
+
Otherwise, if it's just 1 opinion, generate only 1 caption. Memes can be funny; you can use bad words or banned words; they can be dark. And the style must be sometime dark or gangster, and use slang everytime.
|
38 |
+
But captions have to be short, not too long.'''
|
39 |
}
|
40 |
],
|
41 |
max_tokens=250,
|
|
|
47 |
|
48 |
# Function to create meme with caption
|
49 |
def create_meme(prompt):
|
|
|
50 |
prompt_embedding = model.encode([prompt])
|
51 |
|
|
|
52 |
similarities = model.similarity(prompt_embedding, doc_embeddings)
|
53 |
|
|
|
54 |
closest_text_idx = np.argmax(similarities)
|
55 |
|
|
|
56 |
img_path = meme_filename[closest_text_idx]
|
57 |
img = mpimg.imread(f'/kaggle/input/memedata/{img_path}')
|
58 |
|
|
|
59 |
white_space_height = 100
|
60 |
white_space = np.ones((white_space_height, img.shape[1], img.shape[2]), dtype=img.dtype) * 255
|
61 |
|
|
|
62 |
combined_img = np.vstack((white_space, img))
|
63 |
|
|
|
64 |
caption = generate_meme_caption(prompt)
|
65 |
|
|
|
66 |
plt.imshow(combined_img)
|
67 |
plt.axis('off')
|
68 |
plt.text(
|