import gradio as gr import os import requests import urllib from os import path import PIL from PIL import Image from PIL import ImageDraw from PIL import ImageFont import re img_to_text = gr.Blocks.load(name="spaces/pharma/CLIP-Interrogator") prompt_to_joke = gr.Blocks.load(name="spaces/ysharma/text_to_joke") def driver(uploaded_image, prompt): #get a joke based on the prompt generated_joke = texttojoke_inference(prompt) #write on the image image0 = write_on_image(uploaded_image, generated_joke) return image0 #get clip interrogator's prompt response def clip_intero_inference(uploaded_image): prompt = img_to_text(uploaded_image, fn_index=1)[0] print(f"CLip interrogator prompt is : {prompt}") return prompt def texttojoke_inference(prompt): generated_joke = prompt_to_joke(prompt) print(f"Returned values from text_to_joke : {generated_joke}") return generated_joke def write_on_image(uploaded_image, joke): image0 = Image.open(uploaded_image).convert("RGB").resize((512, 512)) I1 = ImageDraw.Draw(image0) #cleaning ellipsis from joke joke = re.sub(r'\.+', ".", joke) lenjoke = len(joke) #Cleaning and Splitting text for top and bottom if joke[-1] == '.': joke = joke[:-1] if len(joke) < 45: topline = joke bottomline = '' elif ('Q.' in joke) and ('A.' in joke): topline = joke.split('?')[0] + '?' bottomline = joke.split('?')[1] elif '?' in joke: topline = joke.split('?')[0] + '?' bottomline = joke[len(joke.split('?')[0] + '?'):] #joke.split('?')[1] elif '!' in joke: topline = joke.split('!')[0] #bottomline = joke.split('.')[1] bottomline = joke[len(joke.split('!')[0])+1:] elif '.' in joke: topline = joke.split('.')[0] #bottomline = joke.split('.')[1] bottomline = joke[len(joke.split('.')[0])+1:] else: numwords = len(joke.split()) numwordstop = int(numwords/2) topline = ' '.join(joke.split()[:numwordstop]) bottomline = ' '.join(joke.split()[numwordstop:]) print(f"Joke is : {joke}") print(f"top line is : {topline }") print(f"bottom line is : {bottomline }") if len(topline.split()) <= 3: I1.text((114, 14), topline, font=set_font(topline), fill =(255, 255, 255)) elif len(topline.split()) <= 6: I1.text((74, 14), topline, font=set_font(topline), fill =(255, 255, 255)) else: I1.text((14, 14), topline, font=set_font(topline), fill =(255, 255, 255)) if len(bottomline.split()) <= 3: I1.text((114, 454), bottomline, font=set_font(bottomline), fill =(255, 255, 255)) elif len(bottomline.split()) <= 6: I1.text((74, 454), bottomline, font=set_font(bottomline), fill =(255, 255, 255)) else: I1.text((14, 454), bottomline, font=set_font(bottomline), fill =(255, 255, 255)) return image0 #, new_prompt def set_font(line): #SETTING UP FONT SIZE length_line = len(line) if length_line < 35 : fontsize = 30 elif length_line < 45 : fontsize = 25 elif length_line < 55 : fontsize = 22 elif length_line < 65 : fontsize = 18 elif length_line < 75 : fontsize = 16 elif length_line < 80 : fontsize = 14 elif length_line < 85 : fontsize = 13 elif length_line < 95: fontsize = 12 elif length_line < 110: fontsize = 11 elif length_line < 130: fontsize = 10 else: fontsize = 8 myfont = ImageFont.truetype('./font1.ttf', fontsize) return myfont with gr.Blocks() as demo: gr.Markdown("

Meme your Image

") gr.Markdown( """
First, generate image description using pharma/CLIP-Interrogator, then fetch a joke using my own ysharma/text_to_joke. If you see the message "Error in model inference - Run Again Please", just press the 'Get Meme' button again every time!

How to use: Press Get Prompts, wait for generated prompt, and then Get Meme buttons to get the final Meme!
""") with gr.Row(): in_image = gr.Image(type="filepath") with gr.Row(): b1 = gr.Button("Get Prompts") b2 = gr.Button("Get Meme") with gr.Row(): out_prompt = gr.Textbox() out_image = gr.Image() #out_dataframe = gr.Dataframe(wrap=True, datatype = ["str", "str", "markdown", "markdown", "str"]) #b1.click(fn=driver, inputs=in_image, outputs=out_image) b1.click(fn=clip_intero_inference, inputs=in_image, outputs=out_prompt) b2.click(fn=driver, inputs=[in_image, out_prompt], outputs=out_image) with gr.Row(): gr.Markdown( """Please keep in mind a few caveats while using this Space:
1. Kindly note that sometimes the joke might be NSFW. Although, I have tried putting in filters to avoid that experience, however, the filters seem non-exhaustive.
2. Sometimes the joke might not match your theme, please bear with the limited capabilities of free open-source ML prototypes.
3. Much like real life, sometimes the joke might just not land, haha!
4. Run time depends on the load on the underlying spaces at the time. """) demo.queue(concurrency_count=3) demo.launch(debug=True, show_error=True, enable_queue=True)