yaleh commited on
Commit
fe75183
·
1 Parent(s): 3368d8a

Updated dockerfile.

Browse files
Files changed (2) hide show
  1. Dockerfile +28 -3
  2. app/streamlit_sample_generator.py +128 -58
Dockerfile CHANGED
@@ -1,5 +1,7 @@
1
- # Use an official Python runtime as the base image
2
- FROM python:3.10
 
 
3
 
4
  # Set the working directory in the container
5
  WORKDIR /app
@@ -19,4 +21,27 @@ RUN poetry install --with=dev
19
  EXPOSE 7860
20
 
21
  # Run the script when the container launches
22
- CMD python app/gradio_meta_prompt.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # gradio
2
+ FROM python:3.10 as gradio
3
+
4
+ # Set the working directory in the container
5
 
6
  # Set the working directory in the container
7
  WORKDIR /app
 
21
  EXPOSE 7860
22
 
23
  # Run the script when the container launches
24
+ CMD python app/gradio_meta_prompt.py
25
+
26
+ # streamlit
27
+ FROM python:3.10 as streamlit
28
+
29
+ # Set the working directory in the container
30
+ WORKDIR /app
31
+ RUN pip install --no-cache-dir -U poetry
32
+
33
+ # Copy all files from the current directory to the working directory in the container
34
+ COPY config.yml poetry.lock pyproject.toml /app/
35
+
36
+ RUN poetry config virtualenvs.create false
37
+ RUN poetry install --with=dev
38
+
39
+ COPY meta_prompt /app/meta_prompt/
40
+ COPY app /app/app/
41
+ RUN poetry install --with=dev
42
+
43
+ # Expose the Streamlit default port
44
+ EXPOSE 8501
45
+
46
+ # Run the Streamlit script when the container launches
47
+ CMD ["streamlit", "run", "app/streamlit_sample_generator.py"]
app/streamlit_sample_generator.py CHANGED
@@ -4,93 +4,124 @@ import json
4
  from langchain_openai import ChatOpenAI
5
  from meta_prompt.sample_generator import TaskDescriptionGenerator
6
 
 
7
  def process_json(input_json, model_name, generating_batch_size, temperature):
8
  try:
9
- model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
 
10
  generator = TaskDescriptionGenerator(model)
11
  result = generator.process(input_json, generating_batch_size)
12
  description = result["description"]
13
- examples_directly = [[example["input"], example["output"]] for example in result["examples_directly"]["examples"]]
 
14
  input_analysis = result["examples_from_briefs"]["input_analysis"]
15
  new_example_briefs = result["examples_from_briefs"]["new_example_briefs"]
16
- examples_from_briefs = [[example["input"], example["output"]] for example in result["examples_from_briefs"]["examples"]]
17
- examples = [[example["input"], example["output"]] for example in result["additional_examples"]]
 
 
18
  return description, examples_directly, input_analysis, new_example_briefs, examples_from_briefs, examples
19
  except Exception as e:
20
  st.warning(f"An error occurred: {str(e)}. Returning default values.")
21
  return "", [], "", [], [], []
22
-
 
23
  def generate_description_only(input_json, model_name, temperature):
24
  try:
25
- model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
 
26
  generator = TaskDescriptionGenerator(model)
27
  description = generator.generate_description(input_json)
28
  return description
29
  except Exception as e:
30
  st.error(f"An error occurred: {str(e)}")
31
 
 
32
  def analyze_input(description, model_name, temperature):
33
  try:
34
- model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
 
35
  generator = TaskDescriptionGenerator(model)
36
  input_analysis = generator.analyze_input(description)
37
  return input_analysis
38
  except Exception as e:
39
  st.error(f"An error occurred: {str(e)}")
40
-
 
41
  def generate_briefs(description, input_analysis, generating_batch_size, model_name, temperature):
42
  try:
43
- model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
 
44
  generator = TaskDescriptionGenerator(model)
45
- briefs = generator.generate_briefs(description, input_analysis, generating_batch_size)
 
46
  return briefs
47
  except Exception as e:
48
  st.error(f"An error occurred: {str(e)}")
49
-
 
50
  def generate_examples_from_briefs(description, new_example_briefs, input_str, generating_batch_size, model_name, temperature):
51
  try:
52
- model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
 
53
  generator = TaskDescriptionGenerator(model)
54
- result = generator.generate_examples_from_briefs(description, new_example_briefs, input_str, generating_batch_size)
55
- examples = [[example["input"], example["output"]] for example in result["examples"]]
 
 
56
  return examples
57
  except Exception as e:
58
  st.error(f"An error occurred: {str(e)}")
59
-
 
60
  def generate_examples_directly(description, raw_example, generating_batch_size, model_name, temperature):
61
  try:
62
- model = ChatOpenAI(model=model_name, temperature=temperature, max_retries=3)
 
63
  generator = TaskDescriptionGenerator(model)
64
- result = generator.generate_examples_directly(description, raw_example, generating_batch_size)
65
- examples = [[example["input"], example["output"]] for example in result["examples"]]
 
 
66
  return examples
67
  except Exception as e:
68
  st.error(f"An error occurred: {str(e)}")
69
 
 
70
  def example_directly_selected():
71
  if 'selected_example_directly_id' in st.session_state:
72
  try:
73
- selected_example_id = st.session_state.selected_example_directly_id['selection']['rows'][0]
74
- selected_example = st.session_state.examples_directly_dataframe.iloc[selected_example_id].to_dict()
75
- st.session_state.selected_example = json.dumps({k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
 
 
 
76
  except Exception as e:
77
  st.session_state.selected_example = None
78
 
 
79
  def example_from_briefs_selected():
80
  if 'selected_example_from_briefs_id' in st.session_state:
81
  try:
82
- selected_example_id = st.session_state.selected_example_from_briefs_id['selection']['rows'][0]
83
- selected_example = st.session_state.examples_from_briefs_dataframe.iloc[selected_example_id].to_dict()
84
- st.session_state.selected_example = json.dumps({k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
 
 
 
85
  except Exception as e:
86
  st.session_state.selected_example = None
87
 
 
88
  def example_selected():
89
  if 'selected_example_id' in st.session_state:
90
  try:
91
  selected_example_id = st.session_state.selected_example_id['selection']['rows'][0]
92
- selected_example = st.session_state.examples_dataframe.iloc[selected_example_id].to_dict()
93
- st.session_state.selected_example = json.dumps({k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
 
 
94
  except Exception as e:
95
  st.session_state.selected_example = None
96
 
@@ -106,45 +137,66 @@ if 'example_briefs_output_text' not in st.session_state:
106
  st.session_state.example_briefs_output_text = ''
107
 
108
  if 'examples_from_briefs_dataframe' not in st.session_state:
109
- st.session_state.examples_from_briefs_dataframe = pd.DataFrame(columns=["Input", "Output"])
 
110
 
111
  if 'examples_directly_dataframe' not in st.session_state:
112
- st.session_state.examples_directly_dataframe = pd.DataFrame(columns=["Input", "Output"])
 
113
 
114
  if 'examples_dataframe' not in st.session_state:
115
- st.session_state.examples_dataframe = pd.DataFrame(columns=["Input", "Output"])
 
116
 
117
  if 'selected_example' not in st.session_state:
118
  st.session_state.selected_example = None
119
 
 
120
  def update_description_output_text():
121
- st.session_state.description_output_text = generate_description_only(input_json, model_name, temperature)
 
 
122
 
123
  def update_input_analysis_output_text():
124
- st.session_state.input_analysis_output_text = analyze_input(description_output, model_name, temperature)
 
 
125
 
126
  def update_example_briefs_output_text():
127
- st.session_state.example_briefs_output_text = generate_briefs(description_output, input_analysis_output, generating_batch_size, model_name, temperature)
 
 
128
 
129
  def update_examples_from_briefs_dataframe():
130
- examples = generate_examples_from_briefs(description_output, example_briefs_output, input_json, generating_batch_size, model_name, temperature)
131
- st.session_state.examples_from_briefs_dataframe = pd.DataFrame(examples, columns=["Input", "Output"])
 
 
 
132
 
133
  def update_examples_directly_dataframe():
134
- examples = generate_examples_directly(description_output, input_json, generating_batch_size, model_name, temperature)
135
- st.session_state.examples_directly_dataframe = pd.DataFrame(examples, columns=["Input", "Output"])
 
 
 
136
 
137
  def generate_examples_dataframe():
138
- result = process_json(input_json, model_name, generating_batch_size, temperature)
 
139
  description, examples_directly, input_analysis, new_example_briefs, examples_from_briefs, examples = result
140
  st.session_state.description_output_text = description
141
- st.session_state.examples_directly_dataframe = pd.DataFrame(examples_directly, columns=["Input", "Output"])
 
142
  st.session_state.input_analysis_output_text = input_analysis
143
  st.session_state.example_briefs_output_text = new_example_briefs
144
- st.session_state.examples_from_briefs_dataframe = pd.DataFrame(examples_from_briefs, columns=["Input", "Output"])
145
- st.session_state.examples_dataframe = pd.DataFrame(examples, columns=["Input", "Output"])
 
 
146
  st.session_state.selected_example = None
147
 
 
148
  # Streamlit UI
149
  st.title("Task Description Generator")
150
  st.markdown("Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples.")
@@ -153,7 +205,8 @@ st.markdown("Enter a JSON object with 'input' and 'output' fields to generate a
153
  input_json = st.text_area("Input JSON", height=200)
154
  model_name = st.selectbox(
155
  "Model Name",
156
- ["llama3-70b-8192", "llama3-8b-8192", "llama-3.1-70b-versatile", "llama-3.1-8b-instant", "gemma2-9b-it"],
 
157
  index=0
158
  )
159
  temperature = st.slider("Temperature", 0.0, 1.0, 1.0, 0.1)
@@ -162,28 +215,41 @@ generating_batch_size = st.slider("Generating Batch Size", 1, 10, 3, 1)
162
  # Buttons
163
  col1, col2 = st.columns(2)
164
  with col1:
165
- submit_button = st.button("Generate", type="primary", on_click=generate_examples_dataframe)
 
166
  with col2:
167
- generate_description_button = st.button("Generate Description", on_click=update_description_output_text)
 
168
 
169
  # Output column
170
 
171
- description_output = st.text_area("Description", value=st.session_state.description_output_text, height=100)
 
172
 
173
  col3, col4 = st.columns(2)
174
  with col3:
175
- generate_examples_directly_button = st.button("Generate Examples Directly", on_click=update_examples_directly_dataframe)
 
176
  with col4:
177
- analyze_input_button = st.button("Analyze Input", on_click=update_input_analysis_output_text)
 
178
 
179
- examples_directly_output = st.dataframe(st.session_state.examples_directly_dataframe, use_container_width=True, selection_mode="single-row", key="selected_example_directly_id", on_select=example_directly_selected)
180
- input_analysis_output = st.text_area("Input Analysis", value=st.session_state.input_analysis_output_text, height=100)
181
- generate_briefs_button = st.button("Generate Briefs", on_click=update_example_briefs_output_text)
182
- example_briefs_output = st.text_area("Example Briefs", value=st.session_state.example_briefs_output_text, height=100)
183
- generate_examples_from_briefs_button = st.button("Generate Examples from Briefs", on_click=update_examples_from_briefs_dataframe)
184
- examples_from_briefs_output = st.dataframe(st.session_state.examples_from_briefs_dataframe, use_container_width=True, selection_mode="single-row", key="selected_example_from_briefs_id", on_select=example_from_briefs_selected)
185
- examples_output = st.dataframe(st.session_state.examples_dataframe, use_container_width=True, selection_mode="single-row", key="selected_example_id", on_select=example_selected)
186
- new_example_json = st.text_area("New Example JSON", value=st.session_state.selected_example, height=100)
 
 
 
 
 
 
 
 
187
 
188
  # Button actions
189
  if submit_button:
@@ -202,13 +268,17 @@ if submit_button:
202
  st.error(f"An error occurred: {str(e)}")
203
 
204
  if generate_examples_directly_button:
205
- examples_directly_output = generate_examples_directly(description_output, input_json, generating_batch_size, model_name, temperature)
 
206
 
207
  if analyze_input_button:
208
- input_analysis_output = analyze_input(description_output, model_name, temperature)
 
209
 
210
  if generate_briefs_button:
211
- example_briefs_output = generate_briefs(description_output, input_analysis_output, generating_batch_size, model_name, temperature)
 
212
 
213
  if generate_examples_from_briefs_button:
214
- examples_from_briefs_output = generate_examples_from_briefs(description_output, example_briefs_output, input_json, generating_batch_size, model_name, temperature)
 
 
4
  from langchain_openai import ChatOpenAI
5
  from meta_prompt.sample_generator import TaskDescriptionGenerator
6
 
7
+
8
  def process_json(input_json, model_name, generating_batch_size, temperature):
9
  try:
10
+ model = ChatOpenAI(
11
+ model=model_name, temperature=temperature, max_retries=3)
12
  generator = TaskDescriptionGenerator(model)
13
  result = generator.process(input_json, generating_batch_size)
14
  description = result["description"]
15
+ examples_directly = [[example["input"], example["output"]]
16
+ for example in result["examples_directly"]["examples"]]
17
  input_analysis = result["examples_from_briefs"]["input_analysis"]
18
  new_example_briefs = result["examples_from_briefs"]["new_example_briefs"]
19
+ examples_from_briefs = [[example["input"], example["output"]]
20
+ for example in result["examples_from_briefs"]["examples"]]
21
+ examples = [[example["input"], example["output"]]
22
+ for example in result["additional_examples"]]
23
  return description, examples_directly, input_analysis, new_example_briefs, examples_from_briefs, examples
24
  except Exception as e:
25
  st.warning(f"An error occurred: {str(e)}. Returning default values.")
26
  return "", [], "", [], [], []
27
+
28
+
29
  def generate_description_only(input_json, model_name, temperature):
30
  try:
31
+ model = ChatOpenAI(
32
+ model=model_name, temperature=temperature, max_retries=3)
33
  generator = TaskDescriptionGenerator(model)
34
  description = generator.generate_description(input_json)
35
  return description
36
  except Exception as e:
37
  st.error(f"An error occurred: {str(e)}")
38
 
39
+
40
  def analyze_input(description, model_name, temperature):
41
  try:
42
+ model = ChatOpenAI(
43
+ model=model_name, temperature=temperature, max_retries=3)
44
  generator = TaskDescriptionGenerator(model)
45
  input_analysis = generator.analyze_input(description)
46
  return input_analysis
47
  except Exception as e:
48
  st.error(f"An error occurred: {str(e)}")
49
+
50
+
51
  def generate_briefs(description, input_analysis, generating_batch_size, model_name, temperature):
52
  try:
53
+ model = ChatOpenAI(
54
+ model=model_name, temperature=temperature, max_retries=3)
55
  generator = TaskDescriptionGenerator(model)
56
+ briefs = generator.generate_briefs(
57
+ description, input_analysis, generating_batch_size)
58
  return briefs
59
  except Exception as e:
60
  st.error(f"An error occurred: {str(e)}")
61
+
62
+
63
  def generate_examples_from_briefs(description, new_example_briefs, input_str, generating_batch_size, model_name, temperature):
64
  try:
65
+ model = ChatOpenAI(
66
+ model=model_name, temperature=temperature, max_retries=3)
67
  generator = TaskDescriptionGenerator(model)
68
+ result = generator.generate_examples_from_briefs(
69
+ description, new_example_briefs, input_str, generating_batch_size)
70
+ examples = [[example["input"], example["output"]]
71
+ for example in result["examples"]]
72
  return examples
73
  except Exception as e:
74
  st.error(f"An error occurred: {str(e)}")
75
+
76
+
77
  def generate_examples_directly(description, raw_example, generating_batch_size, model_name, temperature):
78
  try:
79
+ model = ChatOpenAI(
80
+ model=model_name, temperature=temperature, max_retries=3)
81
  generator = TaskDescriptionGenerator(model)
82
+ result = generator.generate_examples_directly(
83
+ description, raw_example, generating_batch_size)
84
+ examples = [[example["input"], example["output"]]
85
+ for example in result["examples"]]
86
  return examples
87
  except Exception as e:
88
  st.error(f"An error occurred: {str(e)}")
89
 
90
+
91
  def example_directly_selected():
92
  if 'selected_example_directly_id' in st.session_state:
93
  try:
94
+ selected_example_id = st.session_state.selected_example_directly_id[
95
+ 'selection']['rows'][0]
96
+ selected_example = st.session_state.examples_directly_dataframe.iloc[selected_example_id].to_dict(
97
+ )
98
+ st.session_state.selected_example = json.dumps(
99
+ {k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
100
  except Exception as e:
101
  st.session_state.selected_example = None
102
 
103
+
104
  def example_from_briefs_selected():
105
  if 'selected_example_from_briefs_id' in st.session_state:
106
  try:
107
+ selected_example_id = st.session_state.selected_example_from_briefs_id[
108
+ 'selection']['rows'][0]
109
+ selected_example = st.session_state.examples_from_briefs_dataframe.iloc[selected_example_id].to_dict(
110
+ )
111
+ st.session_state.selected_example = json.dumps(
112
+ {k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
113
  except Exception as e:
114
  st.session_state.selected_example = None
115
 
116
+
117
  def example_selected():
118
  if 'selected_example_id' in st.session_state:
119
  try:
120
  selected_example_id = st.session_state.selected_example_id['selection']['rows'][0]
121
+ selected_example = st.session_state.examples_dataframe.iloc[selected_example_id].to_dict(
122
+ )
123
+ st.session_state.selected_example = json.dumps(
124
+ {k.lower(): v for k, v in selected_example.items()}, ensure_ascii=False)
125
  except Exception as e:
126
  st.session_state.selected_example = None
127
 
 
137
  st.session_state.example_briefs_output_text = ''
138
 
139
  if 'examples_from_briefs_dataframe' not in st.session_state:
140
+ st.session_state.examples_from_briefs_dataframe = pd.DataFrame(columns=[
141
+ "Input", "Output"])
142
 
143
  if 'examples_directly_dataframe' not in st.session_state:
144
+ st.session_state.examples_directly_dataframe = pd.DataFrame(
145
+ columns=["Input", "Output"])
146
 
147
  if 'examples_dataframe' not in st.session_state:
148
+ st.session_state.examples_dataframe = pd.DataFrame(
149
+ columns=["Input", "Output"])
150
 
151
  if 'selected_example' not in st.session_state:
152
  st.session_state.selected_example = None
153
 
154
+
155
  def update_description_output_text():
156
+ st.session_state.description_output_text = generate_description_only(
157
+ input_json, model_name, temperature)
158
+
159
 
160
  def update_input_analysis_output_text():
161
+ st.session_state.input_analysis_output_text = analyze_input(
162
+ description_output, model_name, temperature)
163
+
164
 
165
  def update_example_briefs_output_text():
166
+ st.session_state.example_briefs_output_text = generate_briefs(
167
+ description_output, input_analysis_output, generating_batch_size, model_name, temperature)
168
+
169
 
170
  def update_examples_from_briefs_dataframe():
171
+ examples = generate_examples_from_briefs(
172
+ description_output, example_briefs_output, input_json, generating_batch_size, model_name, temperature)
173
+ st.session_state.examples_from_briefs_dataframe = pd.DataFrame(
174
+ examples, columns=["Input", "Output"])
175
+
176
 
177
  def update_examples_directly_dataframe():
178
+ examples = generate_examples_directly(
179
+ description_output, input_json, generating_batch_size, model_name, temperature)
180
+ st.session_state.examples_directly_dataframe = pd.DataFrame(
181
+ examples, columns=["Input", "Output"])
182
+
183
 
184
  def generate_examples_dataframe():
185
+ result = process_json(input_json, model_name,
186
+ generating_batch_size, temperature)
187
  description, examples_directly, input_analysis, new_example_briefs, examples_from_briefs, examples = result
188
  st.session_state.description_output_text = description
189
+ st.session_state.examples_directly_dataframe = pd.DataFrame(
190
+ examples_directly, columns=["Input", "Output"])
191
  st.session_state.input_analysis_output_text = input_analysis
192
  st.session_state.example_briefs_output_text = new_example_briefs
193
+ st.session_state.examples_from_briefs_dataframe = pd.DataFrame(
194
+ examples_from_briefs, columns=["Input", "Output"])
195
+ st.session_state.examples_dataframe = pd.DataFrame(
196
+ examples, columns=["Input", "Output"])
197
  st.session_state.selected_example = None
198
 
199
+
200
  # Streamlit UI
201
  st.title("Task Description Generator")
202
  st.markdown("Enter a JSON object with 'input' and 'output' fields to generate a task description and additional examples.")
 
205
  input_json = st.text_area("Input JSON", height=200)
206
  model_name = st.selectbox(
207
  "Model Name",
208
+ ["llama3-70b-8192", "llama3-8b-8192", "llama-3.1-70b-versatile",
209
+ "llama-3.1-8b-instant", "gemma2-9b-it"],
210
  index=0
211
  )
212
  temperature = st.slider("Temperature", 0.0, 1.0, 1.0, 0.1)
 
215
  # Buttons
216
  col1, col2 = st.columns(2)
217
  with col1:
218
+ submit_button = st.button(
219
+ "Generate", type="primary", on_click=generate_examples_dataframe)
220
  with col2:
221
+ generate_description_button = st.button(
222
+ "Generate Description", on_click=update_description_output_text)
223
 
224
  # Output column
225
 
226
+ description_output = st.text_area(
227
+ "Description", value=st.session_state.description_output_text, height=100)
228
 
229
  col3, col4 = st.columns(2)
230
  with col3:
231
+ generate_examples_directly_button = st.button(
232
+ "Generate Examples Directly", on_click=update_examples_directly_dataframe)
233
  with col4:
234
+ analyze_input_button = st.button(
235
+ "Analyze Input", on_click=update_input_analysis_output_text)
236
 
237
+ examples_directly_output = st.dataframe(st.session_state.examples_directly_dataframe, use_container_width=True,
238
+ selection_mode="single-row", key="selected_example_directly_id", on_select=example_directly_selected)
239
+ input_analysis_output = st.text_area(
240
+ "Input Analysis", value=st.session_state.input_analysis_output_text, height=100)
241
+ generate_briefs_button = st.button(
242
+ "Generate Briefs", on_click=update_example_briefs_output_text)
243
+ example_briefs_output = st.text_area(
244
+ "Example Briefs", value=st.session_state.example_briefs_output_text, height=100)
245
+ generate_examples_from_briefs_button = st.button(
246
+ "Generate Examples from Briefs", on_click=update_examples_from_briefs_dataframe)
247
+ examples_from_briefs_output = st.dataframe(st.session_state.examples_from_briefs_dataframe, use_container_width=True,
248
+ selection_mode="single-row", key="selected_example_from_briefs_id", on_select=example_from_briefs_selected)
249
+ examples_output = st.dataframe(st.session_state.examples_dataframe, use_container_width=True,
250
+ selection_mode="single-row", key="selected_example_id", on_select=example_selected)
251
+ new_example_json = st.text_area(
252
+ "New Example JSON", value=st.session_state.selected_example, height=100)
253
 
254
  # Button actions
255
  if submit_button:
 
268
  st.error(f"An error occurred: {str(e)}")
269
 
270
  if generate_examples_directly_button:
271
+ examples_directly_output = generate_examples_directly(
272
+ description_output, input_json, generating_batch_size, model_name, temperature)
273
 
274
  if analyze_input_button:
275
+ input_analysis_output = analyze_input(
276
+ description_output, model_name, temperature)
277
 
278
  if generate_briefs_button:
279
+ example_briefs_output = generate_briefs(
280
+ description_output, input_analysis_output, generating_batch_size, model_name, temperature)
281
 
282
  if generate_examples_from_briefs_button:
283
+ examples_from_briefs_output = generate_examples_from_briefs(
284
+ description_output, example_briefs_output, input_json, generating_batch_size, model_name, temperature)