Spaces:
Running
Running
Formatted with PEP8.
Browse files- app/gradio_sample_generator.py +247 -59
- guidelines/python.md +11 -0
app/gradio_sample_generator.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import json
|
|
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
from langchain_openai import ChatOpenAI
|
@@ -9,21 +10,43 @@ def examples_to_json(examples):
|
|
9 |
pd_examples.columns = pd_examples.columns.str.lower()
|
10 |
return pd_examples.to_json(orient="records")
|
11 |
|
12 |
-
def process_json(
|
|
|
|
|
13 |
try:
|
14 |
# Convert the gradio dataframe into a JSON array
|
15 |
input_json = examples_to_json(examples)
|
16 |
-
|
17 |
-
model = ChatOpenAI(
|
|
|
|
|
18 |
generator = TaskDescriptionGenerator(model)
|
19 |
result = generator.process(input_json, generating_batch_size)
|
|
|
20 |
description = result["description"]
|
21 |
-
examples_directly = [
|
|
|
|
|
|
|
22 |
input_analysis = result["examples_from_briefs"]["input_analysis"]
|
23 |
new_example_briefs = result["examples_from_briefs"]["new_example_briefs"]
|
24 |
-
examples_from_briefs = [
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
except Exception as e:
|
28 |
raise gr.Error(f"An error occurred: {str(e)}")
|
29 |
|
@@ -47,48 +70,101 @@ def analyze_input(description, model_name, temperature):
|
|
47 |
except Exception as e:
|
48 |
raise gr.Error(f"An error occurred: {str(e)}")
|
49 |
|
50 |
-
def generate_briefs(
|
|
|
|
|
51 |
try:
|
52 |
-
model = ChatOpenAI(
|
|
|
|
|
53 |
generator = TaskDescriptionGenerator(model)
|
54 |
-
briefs = generator.generate_briefs(
|
|
|
|
|
55 |
return briefs
|
56 |
except Exception as e:
|
57 |
raise gr.Error(f"An error occurred: {str(e)}")
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
60 |
try:
|
61 |
input_json = examples_to_json(examples)
|
62 |
-
|
63 |
-
|
|
|
64 |
generator = TaskDescriptionGenerator(model)
|
65 |
-
result = generator.generate_examples_from_briefs(
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
return examples
|
68 |
except Exception as e:
|
69 |
raise gr.Error(f"An error occurred: {str(e)}")
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
72 |
try:
|
73 |
input_json = examples_to_json(raw_example)
|
74 |
model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
|
75 |
generator = TaskDescriptionGenerator(model)
|
76 |
-
result = generator.generate_examples_directly(
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
return examples
|
79 |
except Exception as e:
|
80 |
raise gr.Error(f"An error occurred: {str(e)}")
|
81 |
|
|
|
82 |
def format_selected_example(evt: gr.SelectData, examples):
|
83 |
if evt.index[0] < len(examples):
|
84 |
-
selected_example = examples.iloc[evt.index[0]]
|
85 |
-
json_example = json.dumps(
|
|
|
|
|
|
|
|
|
86 |
return json_example
|
87 |
return ""
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
with gr.Blocks(title="Task Description Generator") as demo:
|
90 |
gr.Markdown("# Task Description Generator")
|
91 |
-
gr.Markdown(
|
|
|
|
|
92 |
|
93 |
with gr.Row():
|
94 |
with gr.Column(scale=1): # Inputs column
|
@@ -97,88 +173,193 @@ with gr.Blocks(title="Task Description Generator") as demo:
|
|
97 |
headers=["Input", "Output"],
|
98 |
datatype=["str", "str"],
|
99 |
row_count=(1, "dynamic"),
|
|
|
100 |
)
|
|
|
|
|
|
|
|
|
101 |
model_name = gr.Dropdown(
|
102 |
label="Model Name",
|
103 |
-
choices=[
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
)
|
106 |
-
temperature = gr.Slider(label="Temperature", value=1.0, minimum=0.0, maximum=1.0, step=0.1)
|
107 |
-
generating_batch_size = gr.Slider(label="Generating Batch Size", value=3, minimum=1, maximum=10, step=1)
|
108 |
with gr.Row():
|
109 |
submit_button = gr.Button("Generate", variant="primary")
|
110 |
-
generate_description_button = gr.Button(
|
|
|
|
|
111 |
|
112 |
with gr.Column(scale=1): # Outputs column
|
113 |
-
description_output = gr.Textbox(
|
|
|
|
|
114 |
with gr.Row():
|
115 |
-
generate_examples_directly_button = gr.Button(
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
submit_button.click(
|
131 |
fn=process_json,
|
132 |
-
inputs=[
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
)
|
135 |
|
136 |
generate_description_button.click(
|
137 |
fn=generate_description_only,
|
138 |
-
inputs=[input_df, model_name, temperature],
|
139 |
-
outputs=[description_output]
|
140 |
)
|
141 |
|
142 |
generate_examples_directly_button.click(
|
143 |
fn=generate_examples_directly,
|
144 |
-
inputs=[
|
145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
)
|
147 |
|
148 |
analyze_input_button.click(
|
149 |
fn=analyze_input,
|
150 |
inputs=[description_output, model_name, temperature],
|
151 |
-
outputs=[input_analysis_output]
|
152 |
)
|
153 |
|
154 |
generate_briefs_button.click(
|
155 |
fn=generate_briefs,
|
156 |
-
inputs=[
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
158 |
)
|
159 |
|
160 |
generate_examples_from_briefs_button.click(
|
161 |
fn=generate_examples_from_briefs,
|
162 |
-
inputs=[
|
163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
)
|
165 |
|
166 |
examples_directly_output.select(
|
167 |
fn=format_selected_example,
|
168 |
inputs=[examples_directly_output],
|
169 |
-
outputs=[new_example_json]
|
170 |
)
|
171 |
|
172 |
examples_from_briefs_output.select(
|
173 |
fn=format_selected_example,
|
174 |
inputs=[examples_from_briefs_output],
|
175 |
-
outputs=[new_example_json]
|
176 |
)
|
177 |
|
178 |
examples_output.select(
|
179 |
fn=format_selected_example,
|
180 |
inputs=[examples_output],
|
181 |
-
outputs=[new_example_json]
|
182 |
)
|
183 |
|
184 |
gr.Markdown("### Manual Flagging")
|
@@ -189,8 +370,15 @@ with gr.Blocks(title="Task Description Generator") as demo:
|
|
189 |
flagging_callback = gr.CSVLogger()
|
190 |
flag_button.click(
|
191 |
lambda *args: flagging_callback.flag(args),
|
192 |
-
inputs=[
|
193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
194 |
)
|
195 |
|
196 |
if __name__ == "__main__":
|
|
|
1 |
import json
|
2 |
+
import tempfile
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
from langchain_openai import ChatOpenAI
|
|
|
10 |
pd_examples.columns = pd_examples.columns.str.lower()
|
11 |
return pd_examples.to_json(orient="records")
|
12 |
|
13 |
+
def process_json(
|
14 |
+
examples, model_name, generating_batch_size, temperature
|
15 |
+
):
|
16 |
try:
|
17 |
# Convert the gradio dataframe into a JSON array
|
18 |
input_json = examples_to_json(examples)
|
19 |
+
|
20 |
+
model = ChatOpenAI(
|
21 |
+
model=model_name, temperature=temperature, max_retries=3
|
22 |
+
)
|
23 |
generator = TaskDescriptionGenerator(model)
|
24 |
result = generator.process(input_json, generating_batch_size)
|
25 |
+
|
26 |
description = result["description"]
|
27 |
+
examples_directly = [
|
28 |
+
[example["input"], example["output"]]
|
29 |
+
for example in result["examples_directly"]["examples"]
|
30 |
+
]
|
31 |
input_analysis = result["examples_from_briefs"]["input_analysis"]
|
32 |
new_example_briefs = result["examples_from_briefs"]["new_example_briefs"]
|
33 |
+
examples_from_briefs = [
|
34 |
+
[example["input"], example["output"]]
|
35 |
+
for example in result["examples_from_briefs"]["examples"]
|
36 |
+
]
|
37 |
+
examples = [
|
38 |
+
[example["input"], example["output"]]
|
39 |
+
for example in result["additional_examples"]
|
40 |
+
]
|
41 |
+
|
42 |
+
return (
|
43 |
+
description,
|
44 |
+
examples_directly,
|
45 |
+
input_analysis,
|
46 |
+
new_example_briefs,
|
47 |
+
examples_from_briefs,
|
48 |
+
examples,
|
49 |
+
)
|
50 |
except Exception as e:
|
51 |
raise gr.Error(f"An error occurred: {str(e)}")
|
52 |
|
|
|
70 |
except Exception as e:
|
71 |
raise gr.Error(f"An error occurred: {str(e)}")
|
72 |
|
73 |
+
def generate_briefs(
|
74 |
+
description, input_analysis, generating_batch_size, model_name, temperature
|
75 |
+
):
|
76 |
try:
|
77 |
+
model = ChatOpenAI(
|
78 |
+
model=model_name, temperature=temperature, max_retries=3
|
79 |
+
)
|
80 |
generator = TaskDescriptionGenerator(model)
|
81 |
+
briefs = generator.generate_briefs(
|
82 |
+
description, input_analysis, generating_batch_size
|
83 |
+
)
|
84 |
return briefs
|
85 |
except Exception as e:
|
86 |
raise gr.Error(f"An error occurred: {str(e)}")
|
87 |
+
|
88 |
+
|
89 |
+
def generate_examples_from_briefs(
|
90 |
+
description, new_example_briefs, examples, generating_batch_size, model_name, temperature
|
91 |
+
):
|
92 |
try:
|
93 |
input_json = examples_to_json(examples)
|
94 |
+
model = ChatOpenAI(
|
95 |
+
model=model_name, temperature=temperature, max_retries=3
|
96 |
+
)
|
97 |
generator = TaskDescriptionGenerator(model)
|
98 |
+
result = generator.generate_examples_from_briefs(
|
99 |
+
description, new_example_briefs, input_json, generating_batch_size
|
100 |
+
)
|
101 |
+
examples = [
|
102 |
+
[example["input"], example["output"]]
|
103 |
+
for example in result["examples"]
|
104 |
+
]
|
105 |
return examples
|
106 |
except Exception as e:
|
107 |
raise gr.Error(f"An error occurred: {str(e)}")
|
108 |
+
|
109 |
+
|
110 |
+
def generate_examples_directly(
|
111 |
+
description, raw_example, generating_batch_size, model_name, temperature
|
112 |
+
):
|
113 |
try:
|
114 |
input_json = examples_to_json(raw_example)
|
115 |
model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
|
116 |
generator = TaskDescriptionGenerator(model)
|
117 |
+
result = generator.generate_examples_directly(
|
118 |
+
description, input_json, generating_batch_size
|
119 |
+
)
|
120 |
+
examples = [
|
121 |
+
[example["input"], example["output"]] for example in result["examples"]
|
122 |
+
]
|
123 |
return examples
|
124 |
except Exception as e:
|
125 |
raise gr.Error(f"An error occurred: {str(e)}")
|
126 |
|
127 |
+
|
128 |
def format_selected_example(evt: gr.SelectData, examples):
|
129 |
if evt.index[0] < len(examples):
|
130 |
+
selected_example = examples.iloc[evt.index[0]]
|
131 |
+
json_example = json.dumps(
|
132 |
+
{"input": selected_example.iloc[0], "output": selected_example.iloc[1]},
|
133 |
+
indent=2,
|
134 |
+
ensure_ascii=False,
|
135 |
+
)
|
136 |
return json_example
|
137 |
return ""
|
138 |
|
139 |
+
def import_json(file):
|
140 |
+
if file is not None:
|
141 |
+
df = pd.read_json(file.name)
|
142 |
+
# Uppercase the first letter of each column name
|
143 |
+
df.columns = df.columns.str.title()
|
144 |
+
return df
|
145 |
+
return None
|
146 |
+
|
147 |
+
def export_json(df):
|
148 |
+
if df is not None and not df.empty:
|
149 |
+
# Copy the dataframe and lowercase the column names
|
150 |
+
df_copy = df.copy()
|
151 |
+
df_copy.columns = df_copy.columns.str.lower()
|
152 |
+
|
153 |
+
json_str = df_copy.to_json(orient="records", indent=2)
|
154 |
+
|
155 |
+
# create a temporary file with the json string
|
156 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".json") as temp_file:
|
157 |
+
temp_file.write(json_str.encode("utf-8"))
|
158 |
+
temp_file_path = temp_file.name
|
159 |
+
|
160 |
+
return temp_file_path
|
161 |
+
return None
|
162 |
+
|
163 |
with gr.Blocks(title="Task Description Generator") as demo:
|
164 |
gr.Markdown("# Task Description Generator")
|
165 |
+
gr.Markdown(
|
166 |
+
"Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples."
|
167 |
+
)
|
168 |
|
169 |
with gr.Row():
|
170 |
with gr.Column(scale=1): # Inputs column
|
|
|
173 |
headers=["Input", "Output"],
|
174 |
datatype=["str", "str"],
|
175 |
row_count=(1, "dynamic"),
|
176 |
+
col_count=(2, "fixed"),
|
177 |
)
|
178 |
+
json_file = gr.File(
|
179 |
+
label="Import/Export JSON", file_types=[".json"], type="filepath"
|
180 |
+
)
|
181 |
+
export_button = gr.Button("Export to JSON")
|
182 |
model_name = gr.Dropdown(
|
183 |
label="Model Name",
|
184 |
+
choices=[
|
185 |
+
"llama3-70b-8192",
|
186 |
+
"llama3-8b-8192",
|
187 |
+
"llama-3.1-70b-versatile",
|
188 |
+
"llama-3.1-8b-instant",
|
189 |
+
"gemma2-9b-it",
|
190 |
+
],
|
191 |
+
value="llama3-70b-8192",
|
192 |
+
)
|
193 |
+
temperature = gr.Slider(
|
194 |
+
label="Temperature", value=1.0, minimum=0.0, maximum=1.0, step=0.1
|
195 |
+
)
|
196 |
+
generating_batch_size = gr.Slider(
|
197 |
+
label="Generating Batch Size", value=3, minimum=1, maximum=10, step=1
|
198 |
)
|
|
|
|
|
199 |
with gr.Row():
|
200 |
submit_button = gr.Button("Generate", variant="primary")
|
201 |
+
generate_description_button = gr.Button(
|
202 |
+
"Generate Description", variant="secondary"
|
203 |
+
)
|
204 |
|
205 |
with gr.Column(scale=1): # Outputs column
|
206 |
+
description_output = gr.Textbox(
|
207 |
+
label="Description", lines=5, show_copy_button=True
|
208 |
+
)
|
209 |
with gr.Row():
|
210 |
+
generate_examples_directly_button = gr.Button(
|
211 |
+
"Generate Examples Directly", variant="secondary"
|
212 |
+
)
|
213 |
+
analyze_input_button = gr.Button(
|
214 |
+
"Analyze Input", variant="secondary"
|
215 |
+
)
|
216 |
+
examples_directly_output = gr.DataFrame(
|
217 |
+
label="Examples Directly",
|
218 |
+
headers=["Input", "Output"],
|
219 |
+
interactive=False,
|
220 |
+
datatype=["str", "str"],
|
221 |
+
row_count=(1, "dynamic"),
|
222 |
+
col_count=(2, "fixed"),
|
223 |
+
)
|
224 |
+
input_analysis_output = gr.Textbox(
|
225 |
+
label="Input Analysis", lines=5, show_copy_button=True
|
226 |
+
)
|
227 |
+
generate_briefs_button = gr.Button(
|
228 |
+
"Generate Briefs", variant="secondary"
|
229 |
+
)
|
230 |
+
example_briefs_output = gr.Textbox(
|
231 |
+
label="Example Briefs", lines=5, show_copy_button=True
|
232 |
+
)
|
233 |
+
generate_examples_from_briefs_button = gr.Button(
|
234 |
+
"Generate Examples from Briefs", variant="secondary"
|
235 |
+
)
|
236 |
+
examples_from_briefs_output = gr.DataFrame(
|
237 |
+
label="Examples from Briefs",
|
238 |
+
headers=["Input", "Output"],
|
239 |
+
interactive=False,
|
240 |
+
datatype=["str", "str"],
|
241 |
+
row_count=(1, "dynamic"),
|
242 |
+
col_count=(2, "fixed"),
|
243 |
+
)
|
244 |
+
examples_output = gr.DataFrame(
|
245 |
+
label="Examples",
|
246 |
+
headers=["Input", "Output"],
|
247 |
+
interactive=False,
|
248 |
+
datatype=["str", "str"],
|
249 |
+
row_count=(1, "dynamic"),
|
250 |
+
col_count=(2, "fixed"),
|
251 |
+
)
|
252 |
+
new_example_json = gr.Textbox(
|
253 |
+
label="New Example JSON", lines=5, show_copy_button=True
|
254 |
+
)
|
255 |
+
|
256 |
+
clear_button = gr.ClearButton(
|
257 |
+
[
|
258 |
+
input_df,
|
259 |
+
description_output,
|
260 |
+
input_analysis_output,
|
261 |
+
example_briefs_output,
|
262 |
+
examples_from_briefs_output,
|
263 |
+
examples_output,
|
264 |
+
new_example_json,
|
265 |
+
]
|
266 |
+
)
|
267 |
+
|
268 |
+
json_file.change(
|
269 |
+
fn=import_json,
|
270 |
+
inputs=[json_file],
|
271 |
+
outputs=[input_df],
|
272 |
+
)
|
273 |
+
|
274 |
+
export_button.click(
|
275 |
+
fn=export_json,
|
276 |
+
inputs=[input_df],
|
277 |
+
outputs=[json_file],
|
278 |
+
)
|
279 |
|
280 |
submit_button.click(
|
281 |
fn=process_json,
|
282 |
+
inputs=[
|
283 |
+
input_df,
|
284 |
+
model_name,
|
285 |
+
generating_batch_size,
|
286 |
+
temperature,
|
287 |
+
],
|
288 |
+
outputs=[
|
289 |
+
description_output,
|
290 |
+
examples_directly_output,
|
291 |
+
input_analysis_output,
|
292 |
+
example_briefs_output,
|
293 |
+
examples_from_briefs_output,
|
294 |
+
examples_output,
|
295 |
+
],
|
296 |
)
|
297 |
|
298 |
generate_description_button.click(
|
299 |
fn=generate_description_only,
|
300 |
+
inputs=[input_df, model_name, temperature],
|
301 |
+
outputs=[description_output],
|
302 |
)
|
303 |
|
304 |
generate_examples_directly_button.click(
|
305 |
fn=generate_examples_directly,
|
306 |
+
inputs=[
|
307 |
+
description_output,
|
308 |
+
input_df,
|
309 |
+
generating_batch_size,
|
310 |
+
model_name,
|
311 |
+
temperature,
|
312 |
+
],
|
313 |
+
outputs=[examples_directly_output],
|
314 |
)
|
315 |
|
316 |
analyze_input_button.click(
|
317 |
fn=analyze_input,
|
318 |
inputs=[description_output, model_name, temperature],
|
319 |
+
outputs=[input_analysis_output],
|
320 |
)
|
321 |
|
322 |
generate_briefs_button.click(
|
323 |
fn=generate_briefs,
|
324 |
+
inputs=[
|
325 |
+
description_output,
|
326 |
+
input_analysis_output,
|
327 |
+
generating_batch_size,
|
328 |
+
model_name,
|
329 |
+
temperature,
|
330 |
+
],
|
331 |
+
outputs=[example_briefs_output],
|
332 |
)
|
333 |
|
334 |
generate_examples_from_briefs_button.click(
|
335 |
fn=generate_examples_from_briefs,
|
336 |
+
inputs=[
|
337 |
+
description_output,
|
338 |
+
example_briefs_output,
|
339 |
+
input_df,
|
340 |
+
generating_batch_size,
|
341 |
+
model_name,
|
342 |
+
temperature,
|
343 |
+
],
|
344 |
+
outputs=[examples_from_briefs_output],
|
345 |
)
|
346 |
|
347 |
examples_directly_output.select(
|
348 |
fn=format_selected_example,
|
349 |
inputs=[examples_directly_output],
|
350 |
+
outputs=[new_example_json],
|
351 |
)
|
352 |
|
353 |
examples_from_briefs_output.select(
|
354 |
fn=format_selected_example,
|
355 |
inputs=[examples_from_briefs_output],
|
356 |
+
outputs=[new_example_json],
|
357 |
)
|
358 |
|
359 |
examples_output.select(
|
360 |
fn=format_selected_example,
|
361 |
inputs=[examples_output],
|
362 |
+
outputs=[new_example_json],
|
363 |
)
|
364 |
|
365 |
gr.Markdown("### Manual Flagging")
|
|
|
370 |
flagging_callback = gr.CSVLogger()
|
371 |
flag_button.click(
|
372 |
lambda *args: flagging_callback.flag(args),
|
373 |
+
inputs=[
|
374 |
+
input_df,
|
375 |
+
model_name,
|
376 |
+
generating_batch_size,
|
377 |
+
description_output,
|
378 |
+
examples_output,
|
379 |
+
flag_reason,
|
380 |
+
],
|
381 |
+
outputs=[],
|
382 |
)
|
383 |
|
384 |
if __name__ == "__main__":
|
guidelines/python.md
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Python Guidelines
|
2 |
+
|
3 |
+
## General Guidelines
|
4 |
+
|
5 |
+
- Use 2 spaces for indentation.
|
6 |
+
- Use snake_case for variable names.
|
7 |
+
- Use camelCase for class names.
|
8 |
+
- Use UPPER_CASE for constants.
|
9 |
+
- Use triple quotes for multi-line strings.
|
10 |
+
- Format with PEP 8 in mind.
|
11 |
+
- Limit line length to 80 characters.
|