rosemariafontana commited on
Commit
a4907a2
·
verified ·
1 Parent(s): a245ab0

update for modular jsons

Browse files
Files changed (1) hide show
  1. app.py +76 -64
app.py CHANGED
@@ -146,73 +146,85 @@ def generate_json(specification, model_version):
146
  def generate_json_pieces(specification, model_version, additional_json_creation_options, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input):
147
 
148
  if additional_json_creation_options == "Explicit specific pieces":
 
 
 
 
 
 
 
 
 
 
 
 
149
 
150
- try:
151
- # Call OpenAI API to generate structured output based on prompt
152
- field_response = client.beta.chat.completions.parse(
153
- model=model_version, # Use GPT model that supports structured output
154
- messages=[
155
- {"role": "system", "content": "Extract the field information."},
156
- {"role": "user", "content": field_data_input}
157
- ],
158
- response_format=FarmActivitiesLite,
159
- )
160
-
161
- plant_response = client.beta.chat.completions.parse(
162
- model=model_version, # Use GPT model that supports structured output
163
- messages=[
164
- {"role": "system", "content": "Extract the planting information."},
165
- {"role": "user", "content": planting_data_input}
166
- ],
167
- response_format=PlantingLite,
168
- )
 
 
 
 
 
 
 
 
 
169
 
170
- log_response = client.beta.chat.completions.parse(
171
- model=model_version, # Use GPT model that supports structured output
172
- messages=[
173
- {"role": "system", "content": "Extract the planting information."},
174
- {"role": "user", "content": logs_data_input}
175
- ],
176
- response_format=Log,
177
- )
178
 
179
- soil_response = client.beta.chat.completions.parse(
180
- model=model_version, # Use GPT model that supports structured output
181
- messages=[
182
- {"role": "system", "content": "Extract the planting information."},
183
- {"role": "user", "content": soil_data_input}
184
- ],
185
- response_format=Soil,
186
- )
 
 
 
 
 
 
 
 
187
 
188
- yield_response = client.beta.chat.completions.parse(
189
- model=model_version, # Use GPT model that supports structured output
190
- messages=[
191
- {"role": "system", "content": "Extract the planting information."},
192
- {"role": "user", "content": yield_data_input}
193
- ],
194
- response_format=Yield,
195
- )
196
-
197
- combined_json = field_response.choices[0].message.parsed.copy()
198
- combined_json["plantings"] = plant_response.choices[0].message.parsed
199
- combined_json["plantings"]["logs"] = log_response.choices[0].message.parsed
200
- combined_json["plantings"]["soil"] = soil_response.choices[0].message.parsed
201
- combined_json["plantings"]["yield"] = yield_response.choices[0].message.parsed
202
-
203
- print(combined_json) # debugging
204
 
205
- pretty_json = combined_json.json()
 
 
 
206
 
207
- if 'error' in response:
208
- raise ValueError(f"API error: {response['error']['message']}")
209
-
210
- return pretty_json
211
-
212
- except ValidationError as e:
213
- return {"error": str(e)}
214
- except Exception as e:
215
- return {"error": "Failed to generate valid JSON. " + str(e)}
216
 
217
  def process_specifications(data, model_version, json_creation, additional_json_creation_options, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input):
218
  # This method just drives the process
@@ -248,12 +260,12 @@ with gr.Blocks() as demo:
248
  def update_visibility2(radio):
249
  value = radio
250
  if value == "Explicit specific pieces":
251
- return [gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(1))]
252
  else:
253
- return [gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(0))]
254
 
255
  json_creation_input.change(update_visibility, json_creation_input, additional_json_creation_options)
256
- additional_json_creation_options.change(fn=update_visibility2, inputs=[additional_json_creation_options], outputs=[field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input])
257
 
258
  submit_button = gr.Button("Generate JSON")
259
  submit_button.click(
 
146
  def generate_json_pieces(specification, model_version, additional_json_creation_options, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input):
147
 
148
  if additional_json_creation_options == "Explicit specific pieces":
149
+ field_data_specification = field_data_input
150
+ planting_data_specification = planting_data_input
151
+ logs_data_specification = logs_data_input
152
+ soil_data_specification = soil_data_input
153
+ yield_data_specification = yield_data_input
154
+
155
+ elif additional_json_creation_options == "Parse from one big input text":
156
+ field_data_specification = specification
157
+ planting_data_specification = specification
158
+ logs_data_specification = specification
159
+ soil_data_specification = specification
160
+ yield_data_specification = specification
161
 
162
+ try:
163
+ # Call OpenAI API to generate structured output based on prompt
164
+ field_response = client.beta.chat.completions.parse(
165
+ model=model_version, # Use GPT model that supports structured output
166
+ messages=[
167
+ {"role": "system", "content": "Extract the field information."},
168
+ {"role": "user", "content": field_data_specification}
169
+ ],
170
+ response_format=FarmActivitiesLite,
171
+ )
172
+
173
+ plant_response = client.beta.chat.completions.parse(
174
+ model=model_version, # Use GPT model that supports structured output
175
+ messages=[
176
+ {"role": "system", "content": "Extract the planting information."},
177
+ {"role": "user", "content": planting_data_specification}
178
+ ],
179
+ response_format=PlantingLite,
180
+ )
181
+
182
+ log_response = client.beta.chat.completions.parse(
183
+ model=model_version, # Use GPT model that supports structured output
184
+ messages=[
185
+ {"role": "system", "content": "Extract the planting information."},
186
+ {"role": "user", "content": logs_data_specification}
187
+ ],
188
+ response_format=Log,
189
+ )
190
 
191
+ soil_response = client.beta.chat.completions.parse(
192
+ model=model_version, # Use GPT model that supports structured output
193
+ messages=[
194
+ {"role": "system", "content": "Extract the planting information."},
195
+ {"role": "user", "content": soil_data_specification}
196
+ ],
197
+ response_format=Soil,
198
+ )
199
 
200
+ yield_response = client.beta.chat.completions.parse(
201
+ model=model_version, # Use GPT model that supports structured output
202
+ messages=[
203
+ {"role": "system", "content": "Extract the planting information."},
204
+ {"role": "user", "content": yield_data_specification}
205
+ ],
206
+ response_format=Yield,
207
+ )
208
+
209
+ combined_json = field_response.choices[0].message.parsed.copy()
210
+ combined_json["plantings"] = plant_response.choices[0].message.parsed
211
+ combined_json["plantings"]["logs"] = log_response.choices[0].message.parsed
212
+ combined_json["plantings"]["soil"] = soil_response.choices[0].message.parsed
213
+ combined_json["plantings"]["yield"] = yield_response.choices[0].message.parsed
214
+
215
+ print(combined_json) # debugging
216
 
217
+ pretty_json = combined_json.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
+ if 'error' in response:
220
+ raise ValueError(f"API error: {response['error']['message']}")
221
+
222
+ return pretty_json
223
 
224
+ except ValidationError as e:
225
+ return {"error": str(e)}
226
+ except Exception as e:
227
+ return {"error": "Failed to generate valid JSON. " + str(e)}
 
 
 
 
 
228
 
229
  def process_specifications(data, model_version, json_creation, additional_json_creation_options, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input):
230
  # This method just drives the process
 
260
  def update_visibility2(radio):
261
  value = radio
262
  if value == "Explicit specific pieces":
263
+ return [gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(1))]
264
  else:
265
+ return [gr.Textbox(visible=bool(1)), gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(0)), gr.Textbox(visible=bool(0))]
266
 
267
  json_creation_input.change(update_visibility, json_creation_input, additional_json_creation_options)
268
+ additional_json_creation_options.change(fn=update_visibility2, inputs=[additional_json_creation_options], outputs=[data_input, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input])
269
 
270
  submit_button = gr.Button("Generate JSON")
271
  submit_button.click(