Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import re
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
5 |
+
import cohere
|
6 |
+
import os
|
7 |
+
|
8 |
+
|
9 |
+
key_srkian = os.environ["key_srkian"]
|
10 |
+
co = cohere.Client(key_srkian)#srkian
|
11 |
+
device='cpu'
|
12 |
+
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
13 |
+
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
14 |
+
model_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
15 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
|
17 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
|
18 |
+
|
19 |
+
|
20 |
+
def predict(department,image,max_length=64, num_beams=4):
|
21 |
+
image = image.convert('RGB')
|
22 |
+
image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
|
23 |
+
clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
|
24 |
+
caption_ids = model.generate(image, max_length = max_length)[0]
|
25 |
+
caption_text = clean_text(tokenizer.decode(caption_ids))
|
26 |
+
dept=department
|
27 |
+
context= caption_text
|
28 |
+
response = co.generate(
|
29 |
+
model='large',
|
30 |
+
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-',
|
31 |
+
max_tokens=20,
|
32 |
+
temperature=0.8,
|
33 |
+
k=0,
|
34 |
+
p=0.75,
|
35 |
+
frequency_penalty=0,
|
36 |
+
presence_penalty=0,
|
37 |
+
stop_sequences=["department"],
|
38 |
+
return_likelihoods='NONE')
|
39 |
+
reponse=response.generations[0].text
|
40 |
+
reponse = reponse.replace("department", "")
|
41 |
+
Feedback_SQL="DEPT"+dept+"CAPT"+caption_text+"MAMAY"+reponse
|
42 |
+
|
43 |
+
|
44 |
+
return reponse
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
# input = gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
output = gr.outputs.Textbox(type="auto",label="Meme")
|
53 |
+
#examples = [f"example{i}.jpg" for i in range(1,7)]
|
54 |
+
#examples = os.listdir()
|
55 |
+
examples = [f"example{i}.png" for i in range(1,7)]
|
56 |
+
|
57 |
+
#examples=os.listdir()
|
58 |
+
#for fichier in examples:
|
59 |
+
# if not(fichier.endswith(".png")):
|
60 |
+
# examples.remove(fichier)
|
61 |
+
|
62 |
+
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 ♥️ dicuss @[Xaheen](https://chat.whatsapp.com/BA2s37KvPrG4ach28iISBv). kindly share your thoughts in discussion session and use the app responsibly "
|
63 |
+
title = "Meme world 🖼️"
|
64 |
+
dropdown=["data science ", "product management","marketing","startup" ,"agile","crypto" , "SEO" ]
|
65 |
+
|
66 |
+
article = "Created By : Xaheen "
|
67 |
+
|
68 |
+
interface = gr.Interface(
|
69 |
+
fn=predict,
|
70 |
+
inputs = [gr.inputs.Dropdown(dropdown),gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)],
|
71 |
+
|
72 |
+
theme="grass",
|
73 |
+
outputs=output,
|
74 |
+
# examples = examples,#
|
75 |
+
title=title,
|
76 |
+
description=description,
|
77 |
+
article = article,
|
78 |
+
)
|
79 |
+
interface.launch(debug=True)
|
80 |
+
|
81 |
+
|
82 |
+
# c0here2022
|