Spaces:
Runtime error
Runtime error
allow more lines to be written
Browse files355 unmodified lines3563573583593603616 unmodified lines36836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144228 unmodified lines47147247347447547647716 unmodified lines49449549649749849950082 unmodified lines583584585586587588589590591592593594595596597598297 unmodified lines89689789889990090190212 unmodified lines91591691791891992092192216 unmodified lines939940941942943944945355 unmodified linesreturn resized_imgdef test_font_size(draw,image,6 unmodified lines):text_width = draw.textlength(text, font)text_is_too_long = Trueif num_lines == 1:while font.size > min_font and text_is_too_long:font = ImageFont.truetype(f"fonts/{font_meme_text}.ttf", size=font.size - font_size_reduction)text_width = draw.textlength(text, font)text_is_too_long = text_width > image.widthelif num_lines == 2:while font.size > min_font and text_is_too_long:font = ImageFont.truetype(f"fonts/{font_meme_text}.ttf", size=font.size - font_size_reduction)max_len_increment = 0while (text_is_too_longand max_len_increment < 10and max_len_increment < (len(text)) // 2):temp_text = insert_backslash(text, max_length=(len(text) + max_len_increment) // 2)first_line, second_line = (temp_text.split("\n")[0],temp_text.split("\n")[1],)text_width = max(draw.textlength(first_line, font),draw.textlength(second_line, font),)text_is_too_long = text_width > image.widthmax_len_increment += 1elif num_lines == 3:while font.size > min_font and text_is_too_long:font = ImageFont.truetype(f"fonts/{font_meme_text}.ttf", size=font.size - font_size_reduction)max_len_incr_1_split = 0while text_is_too_long and max_len_incr_1_split < 10:first_temp_text = insert_backslash(text, max_length=(len(text) + max_len_incr_1_split) // 3)first_line, second_line = (first_temp_text.split("\n")[0],first_temp_text.split("\n")[1],)max_len_incr_2_split = 0while text_is_too_long and max_len_incr_2_split < 10:temp_text_second_line = insert_backslash(second_line,max_length=(len(second_line) + max_len_incr_2_split) // 2,)second_line_1, second_line_2 = (temp_text_second_line.split("\n")[0],temp_text_second_line.split("\n")[1],)temp_text = first_line + "\n" + second_line_1 + "\n" + second_line_2text_width = max(draw.textlength(first_line, font),draw.textlength(second_line_1, font),draw.textlength(second_line_2, font),)text_is_too_long = text_width > image.widthmax_len_incr_2_split += 1max_len_incr_1_split += 1else:raise (ValueError("num_lines can only be 1, 2 or 3"))if not text_is_too_long and num_lines > 1:text = temp_text28 unmodified linesinitial_font_size = 80text_is_too_long = Truenum_lines = 0while text_is_too_long and num_lines < 3:num_lines += 1font = ImageFont.truetype(f"fonts/{font_meme_text}.ttf", size=initial_font_size)text, font, text_width, text_is_too_long = test_font_size(16 unmodified linesoutline_width = 2text_x = (image_width - text_width) / 2text_y = image_height - num_lines * font.size - 10 - num_linesif text_at_the_top:text_y = 082 unmodified linesvisible=False,avatar_images=[None, BOT_AVATAR],)css='''.gradio-container{max-width: 970px!important}h1{display: flex;align-items: center;justify-content: center;gap: .25em}'''with gr.Blocks(title="AI Meme Generator", theme=gr.themes.Base(), css=css) as demo:with gr.Row(scale=0.5):gr.HTML("""<h1 align="center">AI Meme Generator <span style="font-size: 13px;">powered by <a href="https://huggingface.co/blog/idefics">IDEFICS</a></span><img width=40 height=40 src="https://cdn-uploads.huggingface.co/production/uploads/624bebf604abc7ebb01789af/v770xGti5vH1SYLBgyOO_.png" /></h1>""")with gr.Row(elem_id="model_selector_row"):model_selector = gr.Dropdown(choices=MODELS,297 unmodified linesfull_text += acc_textacc_text = ""textbox.submit(fn=lambda: "", inputs=[], outputs=[generated_memes_gallery], queue=False).then(fn=model_inference,inputs=[model_selector,12 unmodified lines],outputs=[textbox, generated_memes_gallery, chatbot],)imagebox.upload(fn=lambda: "", inputs=[], outputs=[generated_memes_gallery], queue=False).then(fn=lambda: "Write a meme about this image.", inputs=[], outputs=[textbox], queue=False).then(fn=model_inference,inputs=[model_selector,16 unmodified lineschatbot,],)submit_btn.click(fn=lambda: "", inputs=[], outputs=[generated_memes_gallery], queue=False).then(fn=model_inference,inputs=[model_selector,355 unmodified lines3563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953966 unmodified lines40340440540640740840941041141241341441541641741841942042142242342442542642728 unmodified lines45645745845946046146216 unmodified lines47948048148248348448582 unmodified lines568569570571572573574575576577578579580581582583297 unmodified lines88188288388488588688788888912 unmodified lines90290390490590690790890991091191291391491516 unmodified lines932933934935936937938939940355 unmodified linesreturn resized_imgdef make_new_lines(draw, image, font, text_is_too_long, lines, num_lines, num_loops):max_len_increment = 0while text_is_too_long and max_len_increment < 10:new_lines = lines.copy()last_line_with_backslash = insert_backslash(new_lines[-1],max_length=(len(new_lines[-1]) + max_len_increment)// (num_lines - num_loops),)penultimate_line, last_line = (last_line_with_backslash.split("\n")[0],last_line_with_backslash.split("\n")[1],)new_lines.pop(-1)new_lines.append(penultimate_line)new_lines.append(last_line)# If the we haven't reached the last line, we split it againif len(new_lines) < num_lines:new_lines, text_width, text_is_too_long = make_new_lines(draw=draw,image=image,font=font,text_is_too_long=text_is_too_long,lines=new_lines,num_lines=num_lines,num_loops=num_loops + 1,)text_width = max([draw.textlength(line, font) for line in new_lines])text_is_too_long = text_width > image.widthmax_len_increment += 1if not text_is_too_long:lines = new_linesreturn lines, text_width, text_is_too_longdef test_font_size(draw,image,6 unmodified lines):text_width = draw.textlength(text, font)text_is_too_long = Truelines = [text]while font.size > min_font and text_is_too_long:font = ImageFont.truetype(f"fonts/{font_meme_text}.ttf", size=font.size - font_size_reduction)if num_lines == 1:text_width = draw.textlength(text, font)text_is_too_long = text_width > image.widthelse:lines, text_width, text_is_too_long = make_new_lines(draw=draw,image=image,font=font,text_is_too_long=text_is_too_long,lines=lines,num_lines=num_lines,num_loops=0,)temp_text = "\n".join(lines)if not text_is_too_long and num_lines > 1:text = temp_text28 unmodified linesinitial_font_size = 80text_is_too_long = Truenum_lines = 0while text_is_too_long and num_lines < 8:num_lines += 1font = ImageFont.truetype(f"fonts/{font_meme_text}.ttf", size=initial_font_size)text, font, text_width, text_is_too_long = test_font_size(16 unmodified linesoutline_width = 2text_x = (image_width - text_width) / 2text_y = image_height - num_lines * font.size - 10 - 2 * num_linesif text_at_the_top:text_y = 082 unmodified linesvisible=False,avatar_images=[None, BOT_AVATAR],)css = """.gradio-container{max-width: 970px!important}h1{display: flex;align-items: center;justify-content: center;gap: .25em}"""with gr.Blocks(title="AI Meme Generator", theme=gr.themes.Base(), css=css) as demo:with gr.Row(scale=0.5):gr.HTML("""<h1 align="center">AI Meme Generator <span style="font-size: 13px;">powered by <a href="https://huggingface.co/blog/idefics">IDEFICS</a></span><img width=40 height=40 src="https://cdn-uploads.huggingface.co/production/uploads/624bebf604abc7ebb01789af/v770xGti5vH1SYLBgyOO_.png" /></h1>""")with gr.Row(elem_id="model_selector_row"):model_selector = gr.Dropdown(choices=MODELS,297 unmodified linesfull_text += acc_textacc_text = ""textbox.submit(fn=lambda: "", inputs=[], outputs=[generated_memes_gallery], queue=False).then(fn=model_inference,inputs=[model_selector,12 unmodified lines],outputs=[textbox, generated_memes_gallery, chatbot],)imagebox.upload(fn=lambda: "", inputs=[], outputs=[generated_memes_gallery], queue=False).then(fn=lambda: "Write a meme about this image.",inputs=[],outputs=[textbox],queue=False,).then(fn=model_inference,inputs=[model_selector,16 unmodified lineschatbot,],)submit_btn.click(fn=lambda: "", inputs=[], outputs=[generated_memes_gallery], queue=False).then(fn=model_inference,inputs=[model_selector,
app_dialogue.py+70-75