Spaces:
Running
Running
Optimized UI.
Browse files- demo/sample_generator.ipynb +95 -91
demo/sample_generator.ipynb
CHANGED
@@ -2,49 +2,54 @@
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
-
"execution_count":
|
6 |
-
"metadata": {},
|
7 |
-
"outputs": [],
|
8 |
-
"source": [
|
9 |
-
"# import os\n",
|
10 |
-
"\n",
|
11 |
-
"# # Load configuration from YAML file\n",
|
12 |
-
"# config = {\n",
|
13 |
-
"# \"model_name\": \"llama3-70b-8192\",\n",
|
14 |
-
"# # \"model_name\": \"llama3-8b-8192\",\n",
|
15 |
-
"# # \"model_name\": \"llama-3.1-70b-versatile\",\n",
|
16 |
-
"# # \"model_name\": \"llama-3.1-8b-instant\",\n",
|
17 |
-
"# # \"model_name\": \"gemma2-9b-it\",\n",
|
18 |
-
"# }\n"
|
19 |
-
]
|
20 |
-
},
|
21 |
-
{
|
22 |
-
"cell_type": "code",
|
23 |
-
"execution_count": 2,
|
24 |
"metadata": {},
|
25 |
"outputs": [],
|
26 |
"source": [
|
27 |
"# Define prompt strings as constants\n",
|
28 |
"DESCRIPTION_PROMPT = [\n",
|
29 |
-
" (\"system\", \"\"\"Given the
|
30 |
" \n",
|
31 |
"{raw_example}\n",
|
32 |
"\n",
|
33 |
-
"Provide a concise description of the task type, including the format and\n",
|
34 |
-
"
|
35 |
-
"
|
36 |
"\n",
|
37 |
"Format your response as follows:\n",
|
38 |
"Task Description: [Your description here]\n",
|
39 |
"\"\"\")\n",
|
40 |
"]\n",
|
41 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
"INPUT_ANALYSIS_PROMPT = [\n",
|
43 |
-
" (\"system\", \"\"\"
|
44 |
-
"
|
45 |
-
"
|
46 |
-
"
|
47 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
"\n",
|
49 |
"Format your response as follows:\n",
|
50 |
"Input Analysis: [Your analysis here]\n",
|
@@ -125,7 +130,7 @@
|
|
125 |
},
|
126 |
{
|
127 |
"cell_type": "code",
|
128 |
-
"execution_count":
|
129 |
"metadata": {},
|
130 |
"outputs": [],
|
131 |
"source": [
|
@@ -163,9 +168,9 @@
|
|
163 |
" | RunnablePassthrough.assign(description = self.description_chain)\n",
|
164 |
" | {\n",
|
165 |
" \"description\": lambda x: x[\"description\"],\n",
|
166 |
-
" \"examples_from_briefs\": RunnablePassthrough.assign(input_analysis =
|
167 |
-
" | RunnablePassthrough.assign(new_example_briefs =
|
168 |
-
" | self.examples_from_briefs_chain,\n",
|
169 |
" \"examples\": self.examples_chain\n",
|
170 |
" }\n",
|
171 |
" | RunnablePassthrough.assign(\n",
|
@@ -207,77 +212,76 @@
|
|
207 |
},
|
208 |
{
|
209 |
"cell_type": "code",
|
210 |
-
"execution_count":
|
211 |
"metadata": {},
|
212 |
-
"outputs": [
|
213 |
-
{
|
214 |
-
"name": "stderr",
|
215 |
-
"output_type": "stream",
|
216 |
-
"text": [
|
217 |
-
"/home/yale/work/meta-prompt/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
218 |
-
" from .autonotebook import tqdm as notebook_tqdm\n"
|
219 |
-
]
|
220 |
-
},
|
221 |
-
{
|
222 |
-
"name": "stdout",
|
223 |
-
"output_type": "stream",
|
224 |
-
"text": [
|
225 |
-
"Running on local URL: http://127.0.0.1:7861\n",
|
226 |
-
"\n",
|
227 |
-
"To create a public link, set `share=True` in `launch()`.\n"
|
228 |
-
]
|
229 |
-
},
|
230 |
-
{
|
231 |
-
"data": {
|
232 |
-
"text/html": [
|
233 |
-
"<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
234 |
-
],
|
235 |
-
"text/plain": [
|
236 |
-
"<IPython.core.display.HTML object>"
|
237 |
-
]
|
238 |
-
},
|
239 |
-
"metadata": {},
|
240 |
-
"output_type": "display_data"
|
241 |
-
},
|
242 |
-
{
|
243 |
-
"name": "stderr",
|
244 |
-
"output_type": "stream",
|
245 |
-
"text": [
|
246 |
-
"/home/yale/work/meta-prompt/.venv/lib/python3.10/site-packages/langchain_core/_api/deprecation.py:141: LangChainDeprecationWarning: The class `ChatOpenAI` was deprecated in LangChain 0.0.10 and will be removed in 0.3.0. An updated version of the class exists in the langchain-openai package and should be used instead. To use it run `pip install -U langchain-openai` and import as `from langchain_openai import ChatOpenAI`.\n",
|
247 |
-
" warn_deprecated(\n"
|
248 |
-
]
|
249 |
-
}
|
250 |
-
],
|
251 |
"source": [
|
252 |
"import gradio as gr\n",
|
253 |
"\n",
|
254 |
-
"def process_json(input_json, model_name, generating_batch_size
|
255 |
" try:\n",
|
256 |
-
" model = ChatOpenAI(model=model_name)\n",
|
257 |
" generator = TaskDescriptionGenerator(model)\n",
|
258 |
" result = generator.process(input_json, generating_batch_size)\n",
|
259 |
" description = result[\"description\"]\n",
|
|
|
260 |
" examples = [[example[\"input\"], example[\"output\"]] for example in result[\"additional_examples\"]]\n",
|
261 |
-
" return description, examples\n",
|
262 |
" except Exception as e:\n",
|
263 |
" raise gr.Error(f\"An error occurred: {str(e)}\")\n",
|
264 |
"\n",
|
265 |
-
"
|
266 |
-
"
|
267 |
-
"
|
268 |
-
"
|
269 |
-
"
|
270 |
-
"
|
271 |
-
"
|
272 |
-
"
|
273 |
-
"
|
274 |
-
"
|
275 |
-
"
|
276 |
-
"
|
277 |
-
"
|
278 |
-
"
|
279 |
-
"
|
280 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
281 |
"\n",
|
282 |
"if __name__ == \"__main__\":\n",
|
283 |
" demo.launch()"
|
|
|
2 |
"cells": [
|
3 |
{
|
4 |
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
"metadata": {},
|
7 |
"outputs": [],
|
8 |
"source": [
|
9 |
"# Define prompt strings as constants\n",
|
10 |
"DESCRIPTION_PROMPT = [\n",
|
11 |
+
" (\"system\", \"\"\"Given the JSON example(s) for a task type:\n",
|
12 |
" \n",
|
13 |
"{raw_example}\n",
|
14 |
"\n",
|
15 |
+
"Provide a concise description of the task type, including the format and style\n",
|
16 |
+
"of the input and output. If there are multiple examples, provide an overall\n",
|
17 |
+
"description and ignore unique parts.\n",
|
18 |
"\n",
|
19 |
"Format your response as follows:\n",
|
20 |
"Task Description: [Your description here]\n",
|
21 |
"\"\"\")\n",
|
22 |
"]\n",
|
23 |
"\n",
|
24 |
+
"# INPUT_ANALYSIS_PROMPT = [\n",
|
25 |
+
"# (\"system\", \"\"\"Describe input dimensions and attributes for a specific task type.\n",
|
26 |
+
"# Provide names, very brief descriptions, ranges, typical values, extreme values and\n",
|
27 |
+
"# examples for each.\n",
|
28 |
+
"\n",
|
29 |
+
"# Format your response as follows:\n",
|
30 |
+
"# Input Analysis: [Your analysis here]\n",
|
31 |
+
"# \"\"\"),\n",
|
32 |
+
"# (\"user\", \"\"\"Task Description:\n",
|
33 |
+
"\n",
|
34 |
+
"# {description}\n",
|
35 |
+
"\n",
|
36 |
+
"# \"\"\")\n",
|
37 |
+
"# ]\n",
|
38 |
+
"\n",
|
39 |
"INPUT_ANALYSIS_PROMPT = [\n",
|
40 |
+
" (\"system\", \"\"\"For the specific task type, analyze the possible task inputs across multiple dimensions.\n",
|
41 |
+
" \n",
|
42 |
+
"Conduct a detailed analysis and enumerate:\n",
|
43 |
+
"\n",
|
44 |
+
"1. Core Attributes: Identify the fundamental properties or characteristics of this input type.\n",
|
45 |
+
"1. Variation Dimensions: For each dimension that may vary, specify:\n",
|
46 |
+
" - Dimension name\n",
|
47 |
+
" - Possible range of values or options\n",
|
48 |
+
" - Impact on input nature or task difficulty\n",
|
49 |
+
"1. Constraints: List any rules or limitations that must be adhered to.\n",
|
50 |
+
"1. Edge Cases: Describe extreme or special scenarios that may test the robustness of task processing.\n",
|
51 |
+
"1. External Factors: Enumerate factors that might influence input generation or task completion.\n",
|
52 |
+
"1. Potential Extensions: Propose ways to expand or modify this input type to create new variants.\n",
|
53 |
"\n",
|
54 |
"Format your response as follows:\n",
|
55 |
"Input Analysis: [Your analysis here]\n",
|
|
|
130 |
},
|
131 |
{
|
132 |
"cell_type": "code",
|
133 |
+
"execution_count": null,
|
134 |
"metadata": {},
|
135 |
"outputs": [],
|
136 |
"source": [
|
|
|
168 |
" | RunnablePassthrough.assign(description = self.description_chain)\n",
|
169 |
" | {\n",
|
170 |
" \"description\": lambda x: x[\"description\"],\n",
|
171 |
+
" \"examples_from_briefs\": RunnablePassthrough.assign(input_analysis = self.input_analysis_chain)\n",
|
172 |
+
" | RunnablePassthrough.assign(new_example_briefs = self.briefs_chain) \n",
|
173 |
+
" | RunnablePassthrough.assign(examples = self.examples_from_briefs_chain | (lambda x: x[\"examples\"])),\n",
|
174 |
" \"examples\": self.examples_chain\n",
|
175 |
" }\n",
|
176 |
" | RunnablePassthrough.assign(\n",
|
|
|
212 |
},
|
213 |
{
|
214 |
"cell_type": "code",
|
215 |
+
"execution_count": null,
|
216 |
"metadata": {},
|
217 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
"source": [
|
219 |
"import gradio as gr\n",
|
220 |
"\n",
|
221 |
+
"def process_json(input_json, model_name, generating_batch_size, temperature):\n",
|
222 |
" try:\n",
|
223 |
+
" model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)\n",
|
224 |
" generator = TaskDescriptionGenerator(model)\n",
|
225 |
" result = generator.process(input_json, generating_batch_size)\n",
|
226 |
" description = result[\"description\"]\n",
|
227 |
+
" input_analysis = result[\"examples_from_briefs\"][\"input_analysis\"]\n",
|
228 |
" examples = [[example[\"input\"], example[\"output\"]] for example in result[\"additional_examples\"]]\n",
|
229 |
+
" return description, input_analysis, examples\n",
|
230 |
" except Exception as e:\n",
|
231 |
" raise gr.Error(f\"An error occurred: {str(e)}\")\n",
|
232 |
"\n",
|
233 |
+
"def format_selected_example(evt: gr.SelectData, examples):\n",
|
234 |
+
" if evt.index[0] < len(examples):\n",
|
235 |
+
" selected_example = examples.iloc[evt.index[0]] # Use iloc to access by integer position\n",
|
236 |
+
" json_example = json.dumps({\"input\": selected_example.iloc[0], \"output\": selected_example.iloc[1]}, indent=2, ensure_ascii=False)\n",
|
237 |
+
" return json_example\n",
|
238 |
+
" return \"\"\n",
|
239 |
+
"\n",
|
240 |
+
"with gr.Blocks(title=\"Task Description Generator\") as demo:\n",
|
241 |
+
" gr.Markdown(\"# Task Description Generator\")\n",
|
242 |
+
" gr.Markdown(\"Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples.\")\n",
|
243 |
+
"\n",
|
244 |
+
" with gr.Row():\n",
|
245 |
+
" with gr.Column(scale=1): # Inputs column\n",
|
246 |
+
" input_json = gr.Textbox(label=\"Input JSON\", lines=10, show_copy_button=True)\n",
|
247 |
+
" model_name = gr.Dropdown(\n",
|
248 |
+
" label=\"Model Name\",\n",
|
249 |
+
" choices=[\"llama3-70b-8192\", \"llama3-8b-8192\", \"llama-3.1-70b-versatile\", \"llama-3.1-8b-instant\", \"gemma2-9b-it\"],\n",
|
250 |
+
" value=\"llama3-70b-8192\"\n",
|
251 |
+
" )\n",
|
252 |
+
" temperature = gr.Slider(label=\"Temperature\", value=1.0, minimum=0.0, maximum=1.0, step=0.1)\n",
|
253 |
+
" generating_batch_size = gr.Slider(label=\"Generating Batch Size\", value=3, minimum=1, maximum=10, step=1)\n",
|
254 |
+
" submit_button = gr.Button(\"Generate\", variant=\"primary\")\n",
|
255 |
+
"\n",
|
256 |
+
" with gr.Column(scale=1): # Outputs column\n",
|
257 |
+
" description_output = gr.Textbox(label=\"Description\", lines=5, show_copy_button=True)\n",
|
258 |
+
" input_analysis_output = gr.Textbox(label=\"Input Analysis\", lines=5, show_copy_button=True)\n",
|
259 |
+
" examples_output = gr.DataFrame(label=\"Examples\", headers=[\"Input\", \"Output\"], interactive=False)\n",
|
260 |
+
" new_example_json = gr.Textbox(label=\"New Example JSON\", lines=5, show_copy_button=True)\n",
|
261 |
+
"\n",
|
262 |
+
" submit_button.click(\n",
|
263 |
+
" fn=process_json,\n",
|
264 |
+
" inputs=[input_json, model_name, generating_batch_size, temperature],\n",
|
265 |
+
" outputs=[description_output, input_analysis_output, examples_output]\n",
|
266 |
+
" )\n",
|
267 |
+
"\n",
|
268 |
+
" examples_output.select(\n",
|
269 |
+
" fn=format_selected_example,\n",
|
270 |
+
" inputs=[examples_output],\n",
|
271 |
+
" outputs=[new_example_json]\n",
|
272 |
+
" )\n",
|
273 |
+
"\n",
|
274 |
+
" gr.Markdown(\"### Manual Flagging\")\n",
|
275 |
+
" with gr.Row():\n",
|
276 |
+
" flag_button = gr.Button(\"Flag\")\n",
|
277 |
+
" flag_reason = gr.Textbox(label=\"Reason for flagging\")\n",
|
278 |
+
"\n",
|
279 |
+
" flagging_callback = gr.CSVLogger()\n",
|
280 |
+
" flag_button.click(\n",
|
281 |
+
" lambda *args: flagging_callback.flag(args),\n",
|
282 |
+
" inputs=[input_json, model_name, generating_batch_size, description_output, examples_output, flag_reason],\n",
|
283 |
+
" outputs=[]\n",
|
284 |
+
" )\n",
|
285 |
"\n",
|
286 |
"if __name__ == \"__main__\":\n",
|
287 |
" demo.launch()"
|