meme_world / app.py
Xhaheen's picture
Update app.py
bf967d0
raw history blame
No virus
3.15 kB
import torch
import re
import gradio as gr
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
import cohere
import os
key_srkian = os.environ["key_srkian"]
co = cohere.Client(key_srkian)#srkian
device='cpu'
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
model_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
def predict(department,image,max_length=64, num_beams=4):
image = image.convert('RGB')
image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
caption_ids = model.generate(image, max_length = max_length)[0]
caption_text = clean_text(tokenizer.decode(caption_ids))
dept=department
context= caption_text
response = co.generate(
model='large',
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-',
max_tokens=20,
temperature=0.8,
k=0,
p=0.75,
frequency_penalty=0,
presence_penalty=0,
stop_sequences=["department"],
return_likelihoods='NONE')
reponse=response.generations[0].text
reponse = reponse.replace("department", "")
Feedback_SQL="DEPT"+dept+"CAPT"+caption_text+"MAMAY"+reponse
return reponse
# input = gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)
output = gr.outputs.Textbox(type="auto",label="Meme")
#examples = [f"example{i}.jpg" for i in range(1,7)]
#examples = os.listdir()
#examples=os.listdir()
#for fichier in examples:
# if not(fichier.endswith(".png")):
# examples.remove(fichier)
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 ♥️[Xaheen](https://www.linkedin.com/in/sallu-mandya/). kindly share your thoughts in discussion session and use the app responsibly "
title = "Meme world 🖼️"
dropdown=["data science ", "product management","marketing","startup" ,"agile","crypto" , "SEO" ]
article = "Created By : Xaheen "
interface = gr.Interface(
fn=predict,
inputs = [gr.inputs.Dropdown(dropdown),gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)],
theme="grass",
outputs=output,
# examples = examples,#
title=title,
description=description,
article = article,
)
interface.launch(debug=True)
# c0here2022