Spaces:
Runtime error
Runtime error
Commit
·
bbc856f
1
Parent(s):
dbf8103
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
theme = gr.themes.Glass(
|
| 4 |
primary_hue="cyan",
|
| 5 |
|
| 6 |
-
neutral_hue="gray",
|
| 7 |
)
|
| 8 |
|
| 9 |
interface = gr.Interface(
|
| 10 |
-
fn=
|
| 11 |
-
inputs = [gr.inputs.
|
| 12 |
-
|
| 13 |
-
theme=theme
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
+
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
| 6 |
+
import openai
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
openai.api_key = os.environ['OPENAI_KEY']
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
## Training models
|
| 14 |
+
device='cpu'
|
| 15 |
+
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
| 16 |
+
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
| 17 |
+
model_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
| 18 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
|
| 20 |
+
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint).to(device)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
## READING THE IMAGE
|
| 24 |
+
## Extracting features from image
|
| 25 |
+
## then create a context for the image like
|
| 26 |
+
## Then input the department and context extracted and send it to LLM to get captio meme
|
| 27 |
+
def predict(department,image,max_length=64, num_beams=4):
|
| 28 |
+
image = image.convert('RGB')
|
| 29 |
+
image = feature_extractor(image, return_tensors="pt").pixel_values.to(device)
|
| 30 |
+
print(image)
|
| 31 |
+
clean_text = lambda x: x.replace('<|endoftext|>','').split('\n')[0]
|
| 32 |
+
print(clean_text)
|
| 33 |
+
caption_ids = model.generate(image, max_length = max_length)[0]
|
| 34 |
+
print(caption_ids)
|
| 35 |
+
caption_text = clean_text(tokenizer.decode(caption_ids))
|
| 36 |
+
print(caption_text)
|
| 37 |
+
dept=department
|
| 38 |
+
context= caption_text
|
| 39 |
+
response = openai.Completion.create(
|
| 40 |
+
model="text-davinci-003",
|
| 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 |
+
reponse = response.choices[0].text
|
| 45 |
+
reponse = reponse.replace("department", "")
|
| 46 |
+
Feedback_SQL="DEPT"+dept+"CAPT"+caption_text+"MAMAY"+reponse
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
return reponse
|
| 50 |
+
|
| 51 |
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
output = gr.outputs.Textbox(type="text",label="Meme")
|
| 58 |
+
|
| 59 |
+
examples = [f"example{i}.png" for i in range(1,7)]
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
## GRADIO INTERFACE
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
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/)"
|
| 66 |
+
title = "Meme world 🖼️"
|
| 67 |
+
dropdown=["data science", "product management","marketing","startup" ,"agile","crypto" , "SEO" ]
|
| 68 |
+
|
| 69 |
+
article = "Created By : Xaheen "
|
| 70 |
theme = gr.themes.Glass(
|
| 71 |
primary_hue="cyan",
|
| 72 |
|
| 73 |
+
neutral_hue="gray",font_mono=['IBM Plex Mono', 'ui-monospace', 'Consolas', 'monospace'],
|
| 74 |
)
|
| 75 |
|
| 76 |
interface = gr.Interface(
|
| 77 |
+
fn=predict,
|
| 78 |
+
inputs = [gr.inputs.Dropdown(dropdown),gr.inputs.Image(label="Upload your Image", type = 'pil', optional=True)],
|
| 79 |
+
|
| 80 |
+
theme=theme,
|
| 81 |
+
outputs=output,
|
| 82 |
+
examples =[['data science', 'example5.png'],
|
| 83 |
+
['product management', 'example2.png'],
|
| 84 |
+
['startup', 'example3.png'],
|
| 85 |
+
['marketing', 'example4.png'],
|
| 86 |
+
['agile', 'example1.png'],
|
| 87 |
+
['crypto', 'example6.png']],
|
| 88 |
+
title=title,
|
| 89 |
+
description=description,
|
| 90 |
+
article = article
|
| 91 |
+
)
|
| 92 |
+
interface.launch(debug=True)
|