Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,176 @@
|
|
1 |
-
# %%bash
|
2 |
|
3 |
-
# # git lfs install
|
4 |
-
# # git clone https://huggingface.co/spaces/Xhaheen/meme_world
|
5 |
|
6 |
|
7 |
-
# # pip install -r /content/meme_world/requirements.txt
|
8 |
-
# # pip install gradio
|
9 |
-
# cd /meme_world
|
10 |
|
11 |
|
12 |
-
import torch
|
13 |
-
import re
|
14 |
-
import gradio as gr
|
15 |
-
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
16 |
-
import cohere
|
17 |
-
import os
|
18 |
-
#
|
19 |
-
# os.environ['key_srkian'] = ''
|
20 |
-
key_srkian = os.environ["key_srkian"]
|
21 |
-
co = cohere.Client(key_srkian)#srkian
|
22 |
-
device='cpu'
|
23 |
-
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
24 |
-
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
25 |
-
model_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
26 |
-
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
|
27 |
-
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
|
28 |
-
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
|
29 |
-
|
30 |
-
|
31 |
-
def predict(department,image,max_length=64, num_beams=4):
|
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 |
-
# input = gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)
|
60 |
|
61 |
|
62 |
|
63 |
-
output = gr.outputs.Textbox(type="text",label="Meme")
|
64 |
-
#examples = [f"example{i}.jpg" for i in range(1,7)]
|
65 |
-
#examples = os.listdir()
|
66 |
-
examples = [f"example{i}.png" for i in range(1,7)]
|
67 |
|
68 |
-
#examples=os.listdir()
|
69 |
-
#for fichier in examples:
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
description= " Looking for a fun and easy way to generate memes? Look no further than Meme world! Leveraging large language models like GPT-3PT-3 / Ai21 / Cohere, you can create memes that are sure to be a hit with your friends or network. Created with ♥️ by Arsalan @[Xaheen](https://www.linkedin.com/in/sallu-mandya/). kindly share your thoughts in discussion session and use the app responsibly #NO_Offense \n \n built with ❤️ @[Xhaheen](https://www.linkedin.com/in/sallu-mandya/)"
|
74 |
-
title = "Meme world 🖼️"
|
75 |
-
dropdown=["data science", "product management","marketing","startup" ,"agile","crypto" , "SEO" ]
|
76 |
|
77 |
-
article = "Created By : Xaheen "
|
78 |
|
79 |
-
interface = gr.Interface(
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
interface.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# # %%bash
|
2 |
|
3 |
+
# # # git lfs install
|
4 |
+
# # # git clone https://huggingface.co/spaces/Xhaheen/meme_world
|
5 |
|
6 |
|
7 |
+
# # # pip install -r /content/meme_world/requirements.txt
|
8 |
+
# # # pip install gradio
|
9 |
+
# # cd /meme_world
|
10 |
|
11 |
|
12 |
+
# import torch
|
13 |
+
# import re
|
14 |
+
# import gradio as gr
|
15 |
+
# from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
16 |
+
# import cohere
|
17 |
+
# import os
|
18 |
+
# #
|
19 |
+
# # os.environ['key_srkian'] = ''
|
20 |
+
# key_srkian = os.environ["key_srkian"]
|
21 |
+
# co = cohere.Client(key_srkian)#srkian
|
22 |
+
# device='cpu'
|
23 |
+
# encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
24 |
+
# decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
25 |
+
# model_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
26 |
+
# feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
|
27 |
+
# tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
|
28 |
+
# model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
|
29 |
+
|
30 |
+
|
31 |
+
# def predict(department,image,max_length=64, num_beams=4):
|
32 |
+
# image = image.convert('RGB')
|
33 |
+
# image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
|
34 |
+
# clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
|
35 |
+
# caption_ids = model.generate(image, max_length = max_length)[0]
|
36 |
+
# caption_text = clean_text(tokenizer.decode(caption_ids))
|
37 |
+
# dept=department
|
38 |
+
# context= caption_text
|
39 |
+
# response = co.generate(
|
40 |
+
# model='large',
|
41 |
+
# prompt=f'create non offensive one line meme for given department and context\n\ndepartment- data science\ncontext-a man sitting on a bench with a laptop\nmeme- \"I\'m not a data scientist, but I play one on my laptop.\"\n\ndepartment-startup\ncontext-a young boy is smiling while using a laptop\nmeme-\"When your startup gets funded and you can finally afford a new laptop\"\n\ndepartment- {dept}\ncontext-{context}\nmeme-',
|
42 |
+
# max_tokens=20,
|
43 |
+
# temperature=0.8,
|
44 |
+
# k=0,
|
45 |
+
# p=0.75,
|
46 |
+
# frequency_penalty=0,
|
47 |
+
# presence_penalty=0,
|
48 |
+
# stop_sequences=["department"],
|
49 |
+
# return_likelihoods='NONE')
|
50 |
+
# reponse=response.generations[0].text
|
51 |
+
# reponse = reponse.replace("department", "")
|
52 |
+
# Feedback_SQL="DEPT"+dept+"CAPT"+caption_text+"MAMAY"+reponse
|
53 |
|
54 |
|
55 |
+
# return reponse
|
56 |
|
57 |
|
58 |
|
59 |
+
# # input = gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)
|
60 |
|
61 |
|
62 |
|
63 |
+
# output = gr.outputs.Textbox(type="text",label="Meme")
|
64 |
+
# #examples = [f"example{i}.jpg" for i in range(1,7)]
|
65 |
+
# #examples = os.listdir()
|
66 |
+
# examples = [f"example{i}.png" for i in range(1,7)]
|
67 |
|
68 |
+
# #examples=os.listdir()
|
69 |
+
# #for fichier in examples:
|
70 |
+
# # if not(fichier.endswith(".png")):
|
71 |
+
# # examples.remove(fichier)
|
72 |
|
73 |
+
# description= " Looking for a fun and easy way to generate memes? Look no further than Meme world! Leveraging large language models like GPT-3PT-3 / Ai21 / Cohere, you can create memes that are sure to be a hit with your friends or network. Created with ♥️ by Arsalan @[Xaheen](https://www.linkedin.com/in/sallu-mandya/). kindly share your thoughts in discussion session and use the app responsibly #NO_Offense \n \n built with ❤️ @[Xhaheen](https://www.linkedin.com/in/sallu-mandya/)"
|
74 |
+
# title = "Meme world 🖼️"
|
75 |
+
# dropdown=["data science", "product management","marketing","startup" ,"agile","crypto" , "SEO" ]
|
76 |
|
77 |
+
# article = "Created By : Xaheen "
|
78 |
|
79 |
+
# interface = gr.Interface(
|
80 |
+
# fn=predict,
|
81 |
+
# inputs = [gr.inputs.Dropdown(dropdown),gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)],
|
82 |
|
83 |
+
# theme="grass",
|
84 |
+
# outputs=output,
|
85 |
+
# examples =[['data science', 'example5.png'],
|
86 |
+
# ['product management', 'example2.png'],
|
87 |
+
# ['startup', 'example3.png'],
|
88 |
+
# ['marketing', 'example4.png'],
|
89 |
+
# ['agile', 'example1.png'],
|
90 |
+
# ['crypto', 'example6.png']],
|
91 |
+
# title=title,
|
92 |
+
# description=description,
|
93 |
+
# article = article,
|
94 |
+
# )
|
95 |
+
# interface.launch(debug=True)
|
96 |
+
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
|
105 |
+
|
106 |
+
|
107 |
+
# Step 2: Set up the Gradio interface and import necessary packages
|
108 |
+
import gradio as gr
|
109 |
+
import openai
|
110 |
+
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
111 |
+
import torch
|
112 |
+
from PIL import Image
|
113 |
+
|
114 |
+
# Step 3: Load the provided image captioning model
|
115 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
116 |
+
feature_extractor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
117 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
118 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
119 |
+
model.to(device)
|
120 |
+
|
121 |
+
# Step 4: Create a function to generate captions from images
|
122 |
+
max_length = 16
|
123 |
+
num_beams = 4
|
124 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
125 |
+
|
126 |
+
def generate_caption(image):
|
127 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
128 |
+
if image.mode != "RGB":
|
129 |
+
image = image.convert(mode="RGB")
|
130 |
+
pixel_values = feature_extractor(images=[image], return_tensors="pt").pixel_values
|
131 |
+
pixel_values = pixel_values.to(device)
|
132 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
133 |
+
caption = tokenizer.decode(output_ids[0], skip_special_tokens=True).strip()
|
134 |
+
return caption
|
135 |
+
|
136 |
+
|
137 |
+
# Step 5: Create a function to generate memes using the GPT-3 API
|
138 |
+
def generate_meme(caption, department):
|
139 |
+
openai.api_key = os.environ["key"]
|
140 |
+
prompt = f"Create a non-offensive meme caption for the following image description in the context of {department} department: {caption}"
|
141 |
+
response = openai.Completion.create(engine="text-davinci-002", prompt=prompt, max_tokens=50, n=1, stop=None, temperature=0.7)
|
142 |
+
meme_caption = response.choices[0].text.strip()
|
143 |
+
return meme_caption
|
144 |
+
|
145 |
+
# Step 6: Define the main meme generation function
|
146 |
+
def meme_generator(image, department):
|
147 |
+
caption = generate_caption(image)
|
148 |
+
meme_caption = generate_meme(caption, department)
|
149 |
+
return meme_caption
|
150 |
+
|
151 |
+
examples = [f"example{i}.png" for i in range(1,7)]
|
152 |
+
|
153 |
+
# Step 7: Launch the Gradio application
|
154 |
+
image_input = gr.inputs.Image()
|
155 |
+
department_input = gr.inputs.Dropdown(choices=["data science", "product management","marketing","startup" ,"agile","crypto" , "SEO" ])
|
156 |
+
output_text = gr.outputs.Textbox()
|
157 |
+
|
158 |
+
gr.Interface(fn=meme_generator, inputs=[image_input, department_input], outputs=output_text, title="Meme world!",description= " Looking for a fun and easy way to generate memes? Look no further than Meme world! Leveraging large language models like GPT-3PT-3 / Ai21 / Cohere, you can create memes that are sure to be a hit with your friends or network. Created with ♥️ by Arsalan @[Xaheen](https://www.linkedin.com/in/sallu-mandya/). kindly share your thoughts in discussion session and use the app responsibly #NO_Offense \n \n built with ❤️ @[Xhaheen](https://www.linkedin.com/in/sallu-mandya/)", theme="grass",
|
159 |
+
|
160 |
+
examples =[['example5.png','data science' ],
|
161 |
+
['example2.png','product management'],
|
162 |
+
['example3.png','startup'],
|
163 |
+
['example4.png','marketing'],
|
164 |
+
['example1.png','agile'],
|
165 |
+
['example6.png','crypto']]).launch(debug=True)
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
+
|
174 |
+
|
175 |
+
|
176 |
+
|