Fix demos
Browse files- app.py +7 -5
- components.py +13 -8
- examples/__init__.py +6 -1
- examples/best_clubs.py +14 -28
- examples/generate_ad.py +9 -8
- examples/summarize_website.py +5 -4
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
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 |
|
@@ -16,6 +16,7 @@ with gr.Blocks() as demo:
|
|
16 |
<br>**AI Task**: Ask ChatGPT to do something for you. Eg, summarize a text.
|
17 |
<br>**Code Task**: ChatGPT will create a python function to do something for you. Eg, get the text from a website.
|
18 |
<br> Use this task for things that ChatGPT can't do on its own, like access the internet or iterate over 4K+ tokens.
|
|
|
19 |
<br>
|
20 |
<br>Output from previous tasks can be referenced in subsequen tasks with {tn}. Max 10 tasks allowed (for now).
|
21 |
"""
|
@@ -59,9 +60,10 @@ with gr.Blocks() as demo:
|
|
59 |
outputs=task.outputs + [error_message],
|
60 |
)
|
61 |
prev_tasks.append(task)
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
|
67 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
import actions as a
|
4 |
+
from examples import best_clubs, generate_ad, summarize_website
|
5 |
from components import all_tasks, Tasks
|
6 |
|
7 |
|
|
|
16 |
<br>**AI Task**: Ask ChatGPT to do something for you. Eg, summarize a text.
|
17 |
<br>**Code Task**: ChatGPT will create a python function to do something for you. Eg, get the text from a website.
|
18 |
<br> Use this task for things that ChatGPT can't do on its own, like access the internet or iterate over 4K+ tokens.
|
19 |
+
<br> The code must be generated before executing the tasks.
|
20 |
<br>
|
21 |
<br>Output from previous tasks can be referenced in subsequen tasks with {tn}. Max 10 tasks allowed (for now).
|
22 |
"""
|
|
|
60 |
outputs=task.outputs + [error_message],
|
61 |
)
|
62 |
prev_tasks.append(task)
|
63 |
+
|
64 |
+
# Examples
|
65 |
+
summarize_website.render()
|
66 |
+
generate_ad.render()
|
67 |
+
best_clubs.render()
|
68 |
|
69 |
demo.launch()
|
components.py
CHANGED
@@ -56,7 +56,7 @@ class TaskComponent(Component, ABC):
|
|
56 |
|
57 |
def format_input(self, input: str, vars_in_scope: Dict[str, Any]) -> str:
|
58 |
input = input.strip()
|
59 |
-
prompt_vars = [v for v in re.findall("{(
|
60 |
undefined_vars = prompt_vars - vars_in_scope.keys()
|
61 |
if len(undefined_vars) > 0:
|
62 |
raise KeyError(
|
@@ -174,6 +174,7 @@ class CodeTask(TaskComponent):
|
|
174 |
@staticmethod
|
175 |
def generate_code(code_prompt: str):
|
176 |
import json
|
|
|
177 |
|
178 |
raw_output = ""
|
179 |
packages = ""
|
@@ -196,9 +197,9 @@ class CodeTask(TaskComponent):
|
|
196 |
print(f"Generating code.")
|
197 |
try:
|
198 |
raw_output = llm_call(
|
199 |
-
f"""
|
200 |
-
{code_prompt}
|
201 |
|
|
|
202 |
Use pip packages where available.
|
203 |
For example, if you wanted to make a google search, use the googlesearch-python package instead of scraping google.
|
204 |
Include only the necessary imports.
|
@@ -215,7 +216,7 @@ Instead of printing or saving to disk, the function should return the data."""
|
|
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.
|
@@ -228,11 +229,15 @@ Extract it. Remove anything after the function definition.""",
|
|
228 |
],
|
229 |
)
|
230 |
)
|
231 |
-
packages
|
232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
except Exception as e:
|
234 |
-
import traceback
|
235 |
-
|
236 |
traceback.print_exc()
|
237 |
error_message = gr.HighlightedText.update(
|
238 |
value=[(str(e), "ERROR")], visible=True
|
|
|
56 |
|
57 |
def format_input(self, input: str, vars_in_scope: Dict[str, Any]) -> str:
|
58 |
input = input.strip()
|
59 |
+
prompt_vars = [v for v in re.findall("{(.*)}", input)]
|
60 |
undefined_vars = prompt_vars - vars_in_scope.keys()
|
61 |
if len(undefined_vars) > 0:
|
62 |
raise KeyError(
|
|
|
174 |
@staticmethod
|
175 |
def generate_code(code_prompt: str):
|
176 |
import json
|
177 |
+
import traceback
|
178 |
|
179 |
raw_output = ""
|
180 |
packages = ""
|
|
|
197 |
print(f"Generating code.")
|
198 |
try:
|
199 |
raw_output = llm_call(
|
200 |
+
f"""{code_prompt}
|
|
|
201 |
|
202 |
+
Write one python function for the request above.
|
203 |
Use pip packages where available.
|
204 |
For example, if you wanted to make a google search, use the googlesearch-python package instead of scraping google.
|
205 |
Include only the necessary imports.
|
|
|
216 |
Find the pip packages that need to be installed and get their corresponsing names in pip.
|
217 |
Package names in the imports and in pip might be different. Use the correct pip names.
|
218 |
|
219 |
+
Put them in a valid JSON:
|
220 |
```
|
221 |
{{
|
222 |
"packages": Python list to be used with eval(). If no packages, empty list.
|
|
|
229 |
],
|
230 |
)
|
231 |
)
|
232 |
+
for packages in re.findall("{.*}", packages, re.DOTALL):
|
233 |
+
try:
|
234 |
+
packages = json.loads(packages)
|
235 |
+
packages = packages["packages"]
|
236 |
+
except:
|
237 |
+
print(packages)
|
238 |
+
traceback.print_exc()
|
239 |
+
packages = "ERROR"
|
240 |
except Exception as e:
|
|
|
|
|
241 |
traceback.print_exc()
|
242 |
error_message = gr.HighlightedText.update(
|
243 |
value=[(str(e), "ERROR")], visible=True
|
examples/__init__.py
CHANGED
@@ -19,7 +19,7 @@ def demo_buttons(demo_id, tasks: List[TaskComponent]):
|
|
19 |
for task in tasks:
|
20 |
if isinstance(task, CodeTask):
|
21 |
execution_event = execution_event.then(
|
22 |
-
|
23 |
inputs=[task.code_prompt],
|
24 |
outputs=[
|
25 |
task.raw_output,
|
@@ -80,3 +80,8 @@ def execute_task(demo_id: str, task_id: int, error_value, *args):
|
|
80 |
visible=True,
|
81 |
),
|
82 |
]
|
|
|
|
|
|
|
|
|
|
|
|
19 |
for task in tasks:
|
20 |
if isinstance(task, CodeTask):
|
21 |
execution_event = execution_event.then(
|
22 |
+
generate_code,
|
23 |
inputs=[task.code_prompt],
|
24 |
outputs=[
|
25 |
task.raw_output,
|
|
|
80 |
visible=True,
|
81 |
),
|
82 |
]
|
83 |
+
|
84 |
+
|
85 |
+
def generate_code(code_prompt: str, error_value):
|
86 |
+
if not error_value and code_prompt:
|
87 |
+
return CodeTask.generate_code(code_prompt)
|
examples/best_clubs.py
CHANGED
@@ -8,37 +8,23 @@ DEMO_ID = __name__
|
|
8 |
tasks = [
|
9 |
CodeTask(
|
10 |
0,
|
11 |
-
"
|
12 |
visible=True,
|
13 |
code_value="Make a google search.",
|
14 |
),
|
15 |
-
AITask(
|
16 |
-
1,
|
17 |
-
"""Your goal is to create an ad for a website.
|
18 |
-
You will use an AI image generator to generate an image for your ad.
|
19 |
-
|
20 |
-
Here is the text from the website:
|
21 |
-
{t0}
|
22 |
-
|
23 |
-
Create a prompt for the AI image generator.
|
24 |
-
Avoid logos.""",
|
25 |
-
visible=True,
|
26 |
-
),
|
27 |
CodeTask(
|
28 |
-
|
29 |
-
"{
|
30 |
visible=True,
|
31 |
-
code_value="
|
32 |
),
|
33 |
AITask(
|
34 |
-
|
35 |
-
"""Here is the
|
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 |
-
|
|
|
42 |
visible=True,
|
43 |
),
|
44 |
]
|
@@ -46,9 +32,9 @@ demo_tasks[DEMO_ID] = tasks
|
|
46 |
|
47 |
|
48 |
def render():
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
8 |
tasks = [
|
9 |
CodeTask(
|
10 |
0,
|
11 |
+
"nightlife in NYC",
|
12 |
visible=True,
|
13 |
code_value="Make a google search.",
|
14 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
CodeTask(
|
16 |
+
1,
|
17 |
+
"{t0}",
|
18 |
visible=True,
|
19 |
+
code_value="Get the main content from a list of urls. Top 5. No html. No empty lines. Include only the first 3000 characters. Use the correct headers.",
|
20 |
),
|
21 |
AITask(
|
22 |
+
2,
|
23 |
+
"""Here is the content from a list of websites:
|
|
|
|
|
|
|
24 |
{t1}
|
25 |
|
26 |
+
What is the overal topic?
|
27 |
+
Extract the most relevant points.""",
|
28 |
visible=True,
|
29 |
),
|
30 |
]
|
|
|
32 |
|
33 |
|
34 |
def render():
|
35 |
+
with gr.Tab("Example: Nightlife in NYC"):
|
36 |
+
demo_id = gr.Textbox(DEMO_ID, visible=False)
|
37 |
+
tasks[0].render()
|
38 |
+
tasks[1].render()
|
39 |
+
tasks[2].render()
|
40 |
+
demo_buttons(demo_id, tasks)
|
examples/generate_ad.py
CHANGED
@@ -10,7 +10,7 @@ tasks = [
|
|
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,
|
@@ -28,7 +28,7 @@ Avoid logos.""",
|
|
28 |
2,
|
29 |
"{t1}",
|
30 |
visible=True,
|
31 |
-
code_value="Use openai key <
|
32 |
),
|
33 |
AITask(
|
34 |
1,
|
@@ -46,9 +46,10 @@ demo_tasks[DEMO_ID] = tasks
|
|
46 |
|
47 |
|
48 |
def render():
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
10 |
0,
|
11 |
"https://openai.com/",
|
12 |
visible=True,
|
13 |
+
code_value="Get text from a website. No html. No empty lines. Use the correct headers.",
|
14 |
),
|
15 |
AITask(
|
16 |
1,
|
|
|
28 |
2,
|
29 |
"{t1}",
|
30 |
visible=True,
|
31 |
+
code_value="Use openai key <put_your_openai_key_here>. Generate an image from a prompt. Return the url. (not the content)",
|
32 |
),
|
33 |
AITask(
|
34 |
1,
|
|
|
46 |
|
47 |
|
48 |
def render():
|
49 |
+
with gr.Tab("Example: Generate an ad"):
|
50 |
+
demo_id = gr.Textbox(DEMO_ID, visible=False)
|
51 |
+
tasks[0].render()
|
52 |
+
tasks[1].render()
|
53 |
+
tasks[2].render()
|
54 |
+
tasks[3].render()
|
55 |
+
demo_buttons(demo_id, tasks)
|
examples/summarize_website.py
CHANGED
@@ -18,7 +18,8 @@ demo_tasks[DEMO_ID] = tasks
|
|
18 |
|
19 |
|
20 |
def render():
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
18 |
|
19 |
|
20 |
def render():
|
21 |
+
with gr.Tab("Example: Summarize a website"):
|
22 |
+
demo_id = gr.Textbox(DEMO_ID, visible=False)
|
23 |
+
tasks[0].render()
|
24 |
+
tasks[1].render()
|
25 |
+
demo_buttons(demo_id, tasks)
|