File size: 8,946 Bytes
373df8e
 
 
 
 
 
 
 
 
 
 
 
0bef56f
373df8e
0bef56f
373df8e
0fed80e
0bef56f
 
 
373df8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68260b0
373df8e
2517351
373df8e
 
 
 
 
 
 
 
 
 
 
0bef56f
373df8e
 
 
 
 
 
 
 
 
 
 
 
0bef56f
373df8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bef56f
373df8e
 
 
 
0bef56f
373df8e
 
0bef56f
373df8e
 
 
 
0bef56f
373df8e
0bef56f
373df8e
0bef56f
373df8e
 
 
 
0bef56f
373df8e
 
 
 
 
0bef56f
373df8e
 
 
 
 
 
 
 
 
 
 
 
 
0bef56f
 
 
 
 
373df8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)