lgaleana commited on
Commit
2f0960b
1 Parent(s): d5931c3

Ad example

Browse files
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
 
3
  import actions as a
4
- from examples import summarize_website
5
  from components import all_tasks, Tasks
6
 
7
 
@@ -61,5 +61,7 @@ with gr.Blocks() as demo:
61
  prev_tasks.append(task)
62
  with gr.Tab("Example: Summarize website"):
63
  summarize_website.render()
 
 
64
 
65
  demo.launch()
 
1
  import gradio as gr
2
 
3
  import actions as a
4
+ from examples import generate_ad, summarize_website
5
  from components import all_tasks, Tasks
6
 
7
 
 
61
  prev_tasks.append(task)
62
  with gr.Tab("Example: Summarize website"):
63
  summarize_website.render()
64
+ with gr.Tab("Example: Generate ad"):
65
+ generate_ad.render()
66
 
67
  demo.launch()
components.py CHANGED
@@ -193,41 +193,35 @@ class CodeTask(TaskComponent):
193
  print(f"Generating code.")
194
  try:
195
  raw_output = llm_call(
196
- f"""
197
- Write one python function for the following request:
198
- {code_prompt}
199
-
200
- Use pip packages where available.
201
- For example, if you wanted to make a google search, use the googlesearch-python package instead of scraping google.
202
- Include only the necessary imports.
203
- Instead of printing or saving to disk, the function should return the data.
204
- """
205
  )
206
  with ThreadPoolExecutor() as executor:
207
  packages, script = tuple(
208
  executor.map(
209
  llm_call,
210
  [
211
- f"""
212
- The following text has some python code:
213
- {raw_output}
214
 
215
- Find the pip packages that need to be installed and get their corresponsing names in pip.
216
- Package names in the imports and in pip might be different. Use the correct pip names.
217
 
218
- Put them in a JSON:
219
- ```
220
- {{
221
- "packages": Python list to be used with eval(). If no packages, empty list.
222
- }}
223
- ```
224
- """,
225
- f"""
226
- The following text has some python code:
227
- {raw_output}
228
-
229
- Extract it. Remove anything after the function definition.
230
- """,
231
  ],
232
  )
233
  )
 
193
  print(f"Generating code.")
194
  try:
195
  raw_output = llm_call(
196
+ f"""Write one python function for the following request:
197
+ {code_prompt}
198
+
199
+ Use pip packages where available.
200
+ For example, if you wanted to make a google search, use the googlesearch-python package instead of scraping google.
201
+ Include only the necessary imports.
202
+ Instead of printing or saving to disk, the function should return the data."""
 
 
203
  )
204
  with ThreadPoolExecutor() as executor:
205
  packages, script = tuple(
206
  executor.map(
207
  llm_call,
208
  [
209
+ f"""The following text has some python code:
210
+ {raw_output}
 
211
 
212
+ Find the pip packages that need to be installed and get their corresponsing names in pip.
213
+ Package names in the imports and in pip might be different. Use the correct pip names.
214
 
215
+ Put them in a JSON:
216
+ ```
217
+ {{
218
+ "packages": Python list to be used with eval(). If no packages, empty list.
219
+ }}
220
+ ```""",
221
+ f"""The following text has some python code:
222
+ {raw_output}
223
+
224
+ Extract it. Remove anything after the function definition.""",
 
 
 
225
  ],
226
  )
227
  )
examples/__init__.py CHANGED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ import gradio as gr
3
+
4
+ from components import Task, TaskComponent
5
+
6
+
7
+ def demo_buttons(demo_id, tasks: List[TaskComponent]):
8
+ error_message = gr.HighlightedText(value=None, visible=False)
9
+ execute_btn = gr.Button("Execute")
10
+
11
+ # Sequential execution
12
+ execution_event = execute_btn.click(
13
+ # Clear error message
14
+ lambda: gr.HighlightedText.update(value=None, visible=False),
15
+ inputs=[],
16
+ outputs=[error_message],
17
+ )
18
+ prev_tasks = []
19
+ for task in tasks:
20
+ execution_event = execution_event.then(
21
+ execute_task,
22
+ inputs=[demo_id, task.component_id, error_message]
23
+ + task.inputs
24
+ + [t.output for t in prev_tasks],
25
+ outputs=[task.output, error_message],
26
+ )
27
+ prev_tasks.append(task)
28
+
29
+
30
+ demo_tasks = {}
31
+
32
+
33
+ def execute_task(demo_id: str, task_id: int, error_value, *args):
34
+ error_update = gr.HighlightedText.update(
35
+ value=error_value, visible=error_value is not None
36
+ )
37
+
38
+ if error_value:
39
+ return ["", error_update]
40
+
41
+ task_id = int(task_id)
42
+ n_inputs = demo_tasks[demo_id][task_id].n_inputs
43
+
44
+ task_inputs = args[:n_inputs]
45
+ prev_task_outputs = args[n_inputs:]
46
+
47
+ non_empty_inputs = [i for i in task_inputs if i]
48
+ if not non_empty_inputs:
49
+ return ["", error_update]
50
+
51
+ # Put task outputs in a dictionary with names.
52
+ vars_in_scope = {f"{Task.vname}{i}": o for i, o in enumerate(prev_task_outputs)}
53
+
54
+ try:
55
+ # Task logic gets inserted into the right index
56
+ output = demo_tasks[demo_id][task_id].execute(
57
+ *task_inputs, vars_in_scope=vars_in_scope
58
+ )
59
+ return [output, error_update]
60
+ except Exception as e:
61
+ import traceback
62
+
63
+ print(traceback.format_tb(e.__traceback__))
64
+ return [
65
+ "ERROR",
66
+ gr.HighlightedText.update(
67
+ value=[(f"Error in Task {task_id} :: {e}", "ERROR")],
68
+ visible=True,
69
+ ),
70
+ ]
examples/actions.py DELETED
@@ -1,45 +0,0 @@
1
- import gradio as gr
2
-
3
- from components import Task
4
-
5
- demo_tasks = {}
6
-
7
-
8
- def execute_task(demo_id: str, task_id: int, error_value, *args):
9
- error_update = gr.HighlightedText.update(
10
- value=error_value, visible=error_value is not None
11
- )
12
-
13
- if error_value:
14
- return ["", error_update]
15
-
16
- task_id = int(task_id)
17
- n_inputs = demo_tasks[demo_id][task_id].n_inputs
18
-
19
- task_inputs = args[:n_inputs]
20
- prev_task_outputs = args[n_inputs:]
21
-
22
- non_empty_inputs = [i for i in task_inputs if i]
23
- if not non_empty_inputs:
24
- return ["", error_update]
25
-
26
- # Put task outputs in a dictionary with names.
27
- vars_in_scope = {f"{Task.vname}{i}": o for i, o in enumerate(prev_task_outputs)}
28
-
29
- try:
30
- # Task logic gets inserted into the right index
31
- output = demo_tasks[demo_id][task_id].execute(
32
- *task_inputs, vars_in_scope=vars_in_scope
33
- )
34
- return [output, error_update]
35
- except Exception as e:
36
- import traceback
37
-
38
- print(traceback.format_tb(e.__traceback__))
39
- return [
40
- "ERROR",
41
- gr.HighlightedText.update(
42
- value=[(f"Error in Task {task_id} :: {e}", "ERROR")],
43
- visible=True,
44
- ),
45
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
examples/generate_ad.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from components import AITask, CodeTask
3
+
4
+ from examples import demo_buttons, demo_tasks
5
+
6
+
7
+ DEMO_ID = __name__
8
+ tasks = [
9
+ CodeTask(
10
+ 0,
11
+ "https://openai.com/",
12
+ visible=True,
13
+ code_value="Get text from a website. No html. No empty lines.",
14
+ ),
15
+ AITask(
16
+ 1,
17
+ """Your goal is to create an ad for a website.
18
+ You are an expert in prompt engineering.
19
+ You will use an AI image generator to generate an image for your ad.
20
+
21
+ Here is the text from the website:
22
+ {t0}
23
+
24
+ Create a prompt for the AI image generator.""",
25
+ visible=True,
26
+ ),
27
+ CodeTask(
28
+ 2,
29
+ "{t1}",
30
+ visible=True,
31
+ code_value="Use openai key <put_your_key_in_here>. Generate an image from a prompt. Return the url.",
32
+ ),
33
+ AITask(
34
+ 1,
35
+ """Here is the text from a website:
36
+ {t0}
37
+
38
+ Here is a prompt that was used by an AI image generator to generate an image for an ad:
39
+ {t1}
40
+
41
+ Create a headline for the ad.""",
42
+ visible=True,
43
+ ),
44
+ ]
45
+ demo_tasks[DEMO_ID] = tasks
46
+
47
+
48
+ def render():
49
+ demo_id = gr.Textbox(DEMO_ID, visible=False)
50
+ tasks[0].render()
51
+ tasks[1].render()
52
+ tasks[2].render()
53
+ tasks[3].render()
54
+ demo_buttons(demo_id, tasks)
examples/summarize_website.py CHANGED
@@ -1,43 +1,24 @@
1
  import gradio as gr
2
  from components import AITask, CodeTask
3
 
4
- import examples.actions as ea
5
 
6
 
7
- DEMO_ID = "hello"
8
  tasks = [
9
  CodeTask(
10
  0,
11
- "https://openai.com/",
12
  visible=True,
13
- code_value="Get text from a website (no html). No empty lines.",
14
  ),
15
  AITask(1, "Summarize: {t0}", visible=True),
16
  ]
17
- ea.demo_tasks[DEMO_ID] = tasks
18
 
19
 
20
  def render():
21
  demo_id = gr.Textbox(DEMO_ID, visible=False)
22
  tasks[0].render()
23
  tasks[1].render()
24
- error_message = gr.HighlightedText(value=None, visible=False)
25
- execute_btn = gr.Button("Execute")
26
-
27
- # Sequential execution
28
- execution_event = execute_btn.click(
29
- # Clear error message
30
- lambda: gr.HighlightedText.update(value=None, visible=False),
31
- inputs=[],
32
- outputs=[error_message],
33
- )
34
- prev_tasks = []
35
- for task in tasks:
36
- execution_event = execution_event.then(
37
- ea.execute_task,
38
- inputs=[demo_id, task.component_id, error_message]
39
- + task.inputs
40
- + [t.output for t in prev_tasks],
41
- outputs=[task.output, error_message],
42
- )
43
- prev_tasks.append(task)
 
1
  import gradio as gr
2
  from components import AITask, CodeTask
3
 
4
+ from examples import demo_buttons, demo_tasks
5
 
6
 
7
+ DEMO_ID = __name__
8
  tasks = [
9
  CodeTask(
10
  0,
11
+ "https://huggingface.co/",
12
  visible=True,
13
+ code_value="Get text from a website. No html. No empty lines.",
14
  ),
15
  AITask(1, "Summarize: {t0}", visible=True),
16
  ]
17
+ demo_tasks[DEMO_ID] = tasks
18
 
19
 
20
  def render():
21
  demo_id = gr.Textbox(DEMO_ID, visible=False)
22
  tasks[0].render()
23
  tasks[1].render()
24
+ demo_buttons(demo_id, tasks)