File size: 5,295 Bytes
99866eb
f4e1d16
8b11dca
f4e1d16
 
b69a7c1
 
1d6564a
b69a7c1
 
 
3be8482
4a102c0
f4e1d16
 
4c19bd6
f4e1d16
6abbc8e
b69a7c1
 
4c19bd6
b69a7c1
 
41eff51
b69a7c1
 
 
4a102c0
f4e1d16
 
bb2ec7a
1aa9064
f4e1d16
4c19bd6
6c594d0
4c19bd6
 
b69a7c1
 
 
4a102c0
324378a
b69a7c1
 
4a102c0
 
 
 
 
 
 
e5dd3a7
a4843e7
 
 
 
e5dd3a7
 
d5a2b83
61a5545
 
8e9d471
 
 
 
1ca8d00
 
 
 
4a102c0
 
e7fa3ac
4a102c0
 
 
b69a7c1
 
 
a4843e7
d29c399
 
57a93ba
d29c399
 
 
a4843e7
d29c399
 
 
 
 
 
b69a7c1
d29c399
 
 
 
61b5aed
 
06b62d1
d29c399
06b62d1
 
3f2b408
984ca34
324378a
984ca34
06b62d1
984ca34
06b62d1
 
324378a
57a93ba
324378a
 
d29c399
 
57a93ba
 
d29c399
 
b69a7c1
f4e1d16
bbf90e3
1aee2cc
ffce93e
1aee2cc
f4e1d16
20e0e22
f4e1d16
 
20e0e22
6abbc8e
f4e1d16
 
f2b27d5
bb2ec7a
f4e1d16
 
6abbc8e
6c594d0
6abbc8e
bbf90e3
 
 
9d2831a
5f927fd
91e5a69
bbf90e3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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("<h1><center>Meme your Image</center></h1>")
  gr.Markdown(
        """<div align="center">First, generate image description using <a href="https://huggingface.co/spaces/pharma/CLIP-Interrogator">pharma/CLIP-Interrogator</a>, then fetch a joke using my own <a href="https://huggingface.co/spaces/ysharma/text_to_joke">ysharma/text_to_joke</a>. If you see the message "Error in model inference - Run Again Please", just press the 'Get Meme' button again every time!<br><br><b>How to use:</b> Press <b>Get Prompts</b>, wait for generated prompt, and then <b>Get Meme</b> buttons to get the final Meme!</div>
        """)
  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:<br>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.<br>2. Sometimes the joke might not match your theme, please bear with the limited capabilities of free open-source ML prototypes.<br>3. Much like real life, sometimes the joke might just not land, haha!<br>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)