import newspaper from newspaper import* import gradio as gr import openai openai.api_key = "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" import validators import ast import pyshine as ps import cv2 import numpy as np def predict(action,image, text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space): def titleonImage(action,image, text_in_image, words_line,font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space): font_dict = {'normal size sans-serif':cv2.FONT_HERSHEY_SIMPLEX, 'small size sans-serif':cv2.FONT_HERSHEY_PLAIN, 'normal size sans-serif (complexity +1)': cv2.FONT_HERSHEY_DUPLEX, 'normal size sans-serif (complexity +2)': cv2.FONT_HERSHEY_COMPLEX , 'normal size sans-serif (complexity +3)': cv2.FONT_HERSHEY_TRIPLEX, 'small size sans-serif (complexity +2)': cv2.FONT_HERSHEY_COMPLEX_SMALL, 'hand-writing style font': cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 'hand-writing style font (complexity +1)': cv2.FONT_HERSHEY_SCRIPT_COMPLEX } inp = text_in_image string = "" for word in range(len(inp.split())): if (word > 0) & (word % words_line == 0): string += "\n" string += " " + inp.split()[word] text_in_image = string if image is None: width = 1000 height = 1000 image = np.ones((height, width, 3), dtype=np.uint8) * 255 y_gap = 0 for i in text_in_image.split("\n"): image = ps.putBText(image, i, text_offset_x = int(x_cor), text_offset_y = int(y_cor)+y_gap, vspace = 5, hspace = 5, font_scale = float(font_size), font = font_dict[font], background_RGB = ast.literal_eval(backgound_RGB), text_RGB = ast.literal_eval(text_RGB), thickness = int(thickness), alpha = float(opacity)) y_gap+= int(line_space) return image def getText(url_or_text): text = url_or_text if validators.url(url_or_text): article = Article(url="%s" % (url_or_text), language='en') article.download() article.parse() text = article.text return text def getTitle(text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space): response = openai.Completion.create( model=model, prompt = url_or_text + "\n"+ prompt, temperature=int(temperature), max_tokens=int(max_tokens), top_p=1, frequency_penalty=0.0, presence_penalty=1 ).choices[0].text return response def getSummary(text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space): summary = openai.Completion.create( model=model, prompt = url_or_text + "\n"+ summary_prompt, temperature=int(temperature), max_tokens=int(max_tokens), top_p=1, frequency_penalty=0.0, presence_penalty=1 ).choices[0].text return summary image_final = cv2.imread("fun.png") # text_in_image = "title was not asked" --> only need in summary summary = "summary was not asked" if action == "title on Image": image_final = titleonImage(action,image, text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space) if action == "title on image from url/text": url_or_text = getText(url_or_text) text_in_image = getTitle(text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space) image_final = titleonImage(action,image, text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space) if action == "title on image and summary from url/text": url_or_text = getText(url_or_text) text_in_image = getTitle(text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space) summary = getSummary(text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space) image_final = titleonImage(action,image, text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space) if action == "title from url/text": url_or_text = getText(url_or_text) text_in_image = getTitle(text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space) if action == "summary from url/text": url_or_text = getText(url_or_text) text_in_image = "title was not asked" summary = getSummary(text_in_image, words_line, font, model, url_or_text, prompt, summary_prompt, temperature, max_tokens, x_cor, y_cor, font_size, backgound_RGB, text_RGB, thickness, opacity, line_space) return image_final, text_in_image, summary intr = gr.Interface(predict, [gr.Dropdown(["title on Image", "title on image from url/text", "title on image and summary from url/text", "title from url/text", "summary from url/text" ], value = "title on Image", label = "List of Actions"), "image", gr.Textbox(value="Text was not given", label = "Text you want to see in the image"), gr.Number(value=5, label = "Number of words in a line"), gr.Dropdown(['normal size sans-serif', 'small size sans-serif', 'normal size sans-serif (complexity +1)', 'normal size sans-serif (complexity +2)', 'normal size sans-serif (complexity +3)', 'small size sans-serif (complexity +2)', 'hand-writing style font', 'hand-writing style font (complexity +1)'], label = "Font for the text", value = 'normal size sans-serif'), gr.Dropdown(["text-curie-001", "text-davinci-003"], value = "text-curie-001", label = "GPT3- Model"), gr.Textbox(value="https://www.datasciencecentral.com/will-chatgpt-make-fraud-easier/", label = "Provide blog url or directly input the text for better results"), gr.Textbox(value="Title for instagram post based on above article", label= "Prompt for generating the title"), gr.Textbox(value="Captions for instagram post based on above article", label= "Prompt for summary generation"), gr.Number(value=0.7, label = "How random you want the results to be"), gr.Number(value=100, label= "Max number of words in output"), gr.Number(value=20, label = "X co-ordinate"), gr.Number(value=20, label = "Y co-ordiante"), gr.Number(value=1.0, label = "Font size"), gr.Textbox(value="(228, 225, 222, 211)", label = "Box Color (RGB)"), gr.Textbox(value= "(0, 255,1)", label = "Text Color (RGB)"), gr.Number(value=1, label = "text Thickness"), gr.Number(value=0.8, label = "Opacity of Box"), gr.Number(value=50, label = "Line sapcing")], ["image","text", gr.Textbox(label = "summarizer")], title = "Auto Insta Post", description = "Image and url to Instagram Post" ) intr.launch(inline = False)