File size: 5,552 Bytes
0646af4
788a6f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0646af4
 
28e7d81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55d5517
 
 
 
 
 
0646af4
788a6f6
 
55d5517
d2d1cfd
 
 
 
3adb6b2
55d5517
788a6f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28e7d81
788a6f6
 
 
 
 
 
 
 
 
 
 
 
 
28e7d81
788a6f6
 
 
55d5517
788a6f6
 
 
 
55d5517
788a6f6
 
 
55d5517
788a6f6
28e7d81
 
788a6f6
 
 
 
 
 
 
28e7d81
 
 
788a6f6
28e7d81
 
788a6f6
55d5517
 
 
 
788a6f6
0646af4
 
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import gradio as gr

import ai_tasks
import code_tasks
import custom_code


def open__get_text_from_url() -> str:
    with open("code_tasks/text_in_url.py") as f:
        return f.read()


def open__get_images_from_url() -> str:
    with open("code_tasks/images_in_url.py") as f:
        return f.read()


def open__get_image_infos() -> str:
    with open("custom_code/image_analysis.py") as f:
        return f.read()


def get_text_and_images_from_url(url):
    return (
        code_tasks.text_in_url.get_text_from_url(url),
        code_tasks.images_in_url.get_images_from_url(url),
    )


def get_images_analysis(images):
    return custom_code.image_analysis.analyze_images(eval(images))


def summarize_text(prompt, url, dimensions, text, images, image_infos, summary):
    return ai_tasks.text_summary._summarize_text(
        prompt,
        url=url,
        dimensions=dimensions,
        text=text,
        images=images,
        image_infos=image_infos,
        summary=summary,
    )


def get_headline_for_image(prompt, url, dimensions, text, images, image_infos, summary):
    return ai_tasks.headlines_for_images._get_headline_for_image(
        prompt,
        url=url,
        dimensions=dimensions,
        text=text,
        images=images,
        image_infos=image_infos,
        summary=summary,
    )


def set_image(headline):
    import json

    return json.loads(headline)["url"]


with gr.Blocks() as demo:
    gr.Markdown(
        """
        ## Scrape a website and get an ad
        Enter an url and the dimensions for an image (eg, 300x600).
        <br> A sequence of code and AI tasks will scrape the website and find an image that best fits those dimensions.
        <br> It's your job to edit the image.
        <br> A headline for your ad will also be generated, along with an explanation of why the image was chosen.
        <br> Play around with the AI tasks to get different results.
        """
    )

    url = gr.Textbox(label="Input: {url}")
    dimensions = gr.Textbox(label="Input: {dimensions}")
    execute = gr.Button("Run")

    with gr.Box():
        gr.Markdown("Code task")
        with gr.Row():
            with gr.Column():
                gr.Textbox(
                    "write a python function that given an url returns all text in the website",
                    label="ChatGPT-4 prompt",
                )
                with gr.Accordion("Input: {url}", open=False):
                    gr.Code(open__get_text_from_url(), "python")
            with gr.Column():
                text = gr.Textbox(
                    label="Output: {text}", lines=10, max_lines=10, interactive=False
                )

    with gr.Box():
        gr.Markdown("Code task")
        with gr.Row():
            with gr.Column():
                gr.Textbox(
                    "write a python function that given an url returns all images in the website",
                    label="ChatGPT-4 prompt",
                )
                with gr.Accordion("Input: {url}", open=False):
                    gr.Code(open__get_images_from_url(), "python")
            with gr.Column():
                images = gr.Textbox(
                    label="Output: {images}", lines=10, max_lines=10, interactive=False
                )

    with gr.Box():
        gr.Markdown("Custom code: analyze images with Google Vision")
        with gr.Row():
            with gr.Column():
                with gr.Accordion("Input: {images}", open=False):
                    gr.Code(open__get_image_infos(), "python")
            with gr.Column():
                image_infos = gr.Textbox(
                    label="Output: {image_infos}",
                    lines=10,
                    max_lines=10,
                    interactive=False,
                )

    with gr.Box():
        gr.Markdown("AI task: summarize text")
        with gr.Row():
            with gr.Column():
                summary_prompt = gr.Textbox(
                    ai_tasks.text_summary.PROMPT,
                    label="Instructions",
                    interactive=True,
                )
            with gr.Column():
                summary = gr.Textbox(
                    label="Output: {summary}", lines=10, max_lines=10, interactive=False
                )

    with gr.Box():
        gr.Markdown("AI task: generate headline for image")
        with gr.Row():
            with gr.Column():
                headline_prompt = gr.Textbox(
                    ai_tasks.headlines_for_images.PROMPT,
                    label="Instructions",
                    interactive=True,
                    lines=20,
                )
            with gr.Column():
                headline = gr.Textbox(
                    label="Output: {headline}",
                    lines=10,
                    max_lines=10,
                    interactive=False,
                )
                headline_image = gr.Image()

    vars_ = [url, dimensions, text, images, image_infos, summary]

    execute.click(
        get_text_and_images_from_url, inputs=[url], outputs=[text, images]
    ).success(
        get_images_analysis,
        inputs=[images],
        outputs=[image_infos],
    ).success(
        summarize_text,
        inputs=[summary_prompt] + vars_,  # type: ignore
        outputs=[summary],
    ).success(
        get_headline_for_image,
        inputs=[headline_prompt] + vars_,  # type: ignore
        outputs=[headline],
    ).success(
        set_image,
        inputs=[headline],
        outputs=[headline_image],
    )

demo.launch()