rosemariafontana commited on
Commit
5a083d5
·
verified ·
1 Parent(s): e86369d

added trials

Browse files
Files changed (1) hide show
  1. app.py +53 -13
app.py CHANGED
@@ -10,6 +10,27 @@ from enum import Enum
10
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
11
  client = OpenAI()
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # These are the necessary components that make up the Interactions
14
  class Role(str, Enum):
15
  PARTNER = 'partner'
@@ -28,6 +49,7 @@ class Interactions(BaseModel):
28
  nextSteps: List[str] = Field(..., title="Next Steps", description="List of individual next steps derived from the interaction")
29
  summary: str = Field(..., title="Summary", description="Summary of the interaction")
30
 
 
31
  # These are the components for Farm Activities, Fields, and Plantings
32
  class Status(str, Enum):
33
  ACTIVE = 'active'
@@ -119,7 +141,7 @@ def generate_json(specification, model_version):
119
 
120
  try:
121
  # Call OpenAI API to generate structured output based on prompt
122
- response = client.beta.chat.completions.parse(
123
  model=model_version, # Use GPT model that supports structured output
124
  messages=[
125
  {"role": "system", "content": "Extract the farm information."},
@@ -128,29 +150,46 @@ def generate_json(specification, model_version):
128
  response_format=FarmActivities,
129
  )
130
 
131
- generated_json = response.choices[0].message.parsed
132
- print(generated_json) # debugging
133
 
134
- pretty_json = generated_json.json()
135
 
136
- another_response = client.beta.chat.completions.parse(
137
  model=model_version, # Use GPT model that supports structured output
138
  messages=[
139
- {"role": "system", "content": "Extract the farm information."},
140
  {"role": "user", "content": specification}
141
  ],
142
  response_format=Interactions,
143
  )
144
 
145
- another_generated_json = another_response.choices[0].message.parsed
146
- print(another_generated_json) # debugging 2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
- another_pretty_json = another_generated_json.json()
149
 
150
  if 'error' in response:
151
  raise ValueError(f"API error: {response['error']['message']}")
152
 
153
- return pretty_json, another_pretty_json
154
 
155
  except ValidationError as e:
156
  return {"error": str(e)}
@@ -265,8 +304,9 @@ with gr.Blocks() as demo:
265
  soil_data_input = gr.Textbox(label="Enter your soil data", placeholder="Description, Structure, Biology", visible=False)
266
  yield_data_input = gr.Textbox(label="Enter your yield data", placeholder="Quantity, Quality", visible=False)
267
 
268
- output_box = gr.Textbox(label="Fields and Activities Output Data")
269
- another_output_box = gr.Textbox(label="Interactions Output Data")
 
270
 
271
  def update_visibility(radio, additional_options):
272
  value = radio
@@ -291,7 +331,7 @@ with gr.Blocks() as demo:
291
  submit_button.click(
292
  fn=process_specifications,
293
  inputs=[data_input, model_version_input, json_creation_input, additional_json_creation_options, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input],
294
- outputs=[output_box, another_output_box]
295
  )
296
 
297
  clear_button = gr.ClearButton(components=[data_input, model_version_input, json_creation_input, additional_json_creation_options, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input])
 
10
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
11
  client = OpenAI()
12
 
13
+ # These are the necessary components that make up the Trials
14
+ class Variables(BaseModel):
15
+ controlled: List[str] = Field(..., title="Controlled Variables", description="A list of controlled variables, which will be constant (controlled) across all trials")
16
+ independent: List[str] = Field(..., title="Independent Variables", description="A list of independent variables (ie treatments), which will be intentionally varied across one or more trials")
17
+ outcome: List[str] = Field(..., title="Outcome Variables", description="A list of outcome variables (ie dependent or response variables)")
18
+
19
+ class Treatment(BaseModel):
20
+ name: str = Field(..., title="Name", description="The treatment name")
21
+ description: str = Field(..., title="Description", description="The treatment description, including the conditions within this treatment")
22
+ crops: List[str] = Field(..., title="Crops", description="A list of crops being tested in this treatment")
23
+ fields: List[str] = Field(..., title="Fields", description="A list of fields in which this treatment has occured or will occur")
24
+ learnings: List[str] = Field(..., title="Learnings", description="A list of lessons learned from this experiment")
25
+ variables: Variable = Field(..., title="Variables", description="Variables (ie factors) in this experiment. Some variables are constant (controlled) and some will vary in order to learn something (independent)")
26
+ confoundingFactors = List[str] = Field(..., title="Confounding Factors", description="A list of factors which may impact the outcomes of the experiment that were not planned for")
27
+
28
+ class Trial(BaseModel):
29
+ name: str = Field(..., title="Name", description="The name of this trial")
30
+ description: str = Field(..., title="Description", description="A description of this trial")
31
+ treatments: List[Treatment] = Field(..., title="Treatments", description="A list of different treatments (strips or blocks with the same conditions applied) performed by the partner")
32
+
33
+ #################################################################################
34
  # These are the necessary components that make up the Interactions
35
  class Role(str, Enum):
36
  PARTNER = 'partner'
 
49
  nextSteps: List[str] = Field(..., title="Next Steps", description="List of individual next steps derived from the interaction")
50
  summary: str = Field(..., title="Summary", description="Summary of the interaction")
51
 
52
+ #################################################################################
53
  # These are the components for Farm Activities, Fields, and Plantings
54
  class Status(str, Enum):
55
  ACTIVE = 'active'
 
141
 
142
  try:
143
  # Call OpenAI API to generate structured output based on prompt
144
+ farm_info_response = client.beta.chat.completions.parse(
145
  model=model_version, # Use GPT model that supports structured output
146
  messages=[
147
  {"role": "system", "content": "Extract the farm information."},
 
150
  response_format=FarmActivities,
151
  )
152
 
153
+ farm_generated_json = farm_info_response.choices[0].message.parsed
154
+ print(farm_generated_json) # debugging
155
 
156
+ farm_pretty_json = farm_generated_json.json()
157
 
158
+ interactions_response = client.beta.chat.completions.parse(
159
  model=model_version, # Use GPT model that supports structured output
160
  messages=[
161
+ {"role": "system", "content": "Extract the interactions information."},
162
  {"role": "user", "content": specification}
163
  ],
164
  response_format=Interactions,
165
  )
166
 
167
+ interactions_generated_json = interactions_response.choices[0].message.parsed
168
+ print(interactions_generated_json) # debugging 2
169
+
170
+ interactions_pretty_json = interactions_generated_json.json()
171
+
172
+ if 'error' in response:
173
+ raise ValueError(f"API error: {response['error']['message']}")
174
+
175
+ trial_response = client.beta.chat.completions.parse(
176
+ model=model_version, # Use GPT model that supports structured output
177
+ messages=[
178
+ {"role": "system", "content": "Extract the trial information."},
179
+ {"role": "user", "content": specification}
180
+ ],
181
+ response_format=Trial,
182
+ )
183
+
184
+ trial_generated_json = trial_response.choices[0].message.parsed
185
+ print(trial_generated_json) # debugging 2
186
 
187
+ trial_pretty_json = trial_generated_json.json()
188
 
189
  if 'error' in response:
190
  raise ValueError(f"API error: {response['error']['message']}")
191
 
192
+ return farm_pretty_json, interactions_pretty_json, trial_pretty_json
193
 
194
  except ValidationError as e:
195
  return {"error": str(e)}
 
304
  soil_data_input = gr.Textbox(label="Enter your soil data", placeholder="Description, Structure, Biology", visible=False)
305
  yield_data_input = gr.Textbox(label="Enter your yield data", placeholder="Quantity, Quality", visible=False)
306
 
307
+ farm_output_box = gr.Textbox(label="Fields and Activities Output Data")
308
+ interactions_output_box = gr.Textbox(label="Interactions Output Data")
309
+ trials_output_box = gr.Textbox(label="Trials Output Data")
310
 
311
  def update_visibility(radio, additional_options):
312
  value = radio
 
331
  submit_button.click(
332
  fn=process_specifications,
333
  inputs=[data_input, model_version_input, json_creation_input, additional_json_creation_options, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input],
334
+ outputs=[farm_output_box, interactions_output_box, trials_output_box]
335
  )
336
 
337
  clear_button = gr.ClearButton(components=[data_input, model_version_input, json_creation_input, additional_json_creation_options, field_data_input, planting_data_input, logs_data_input, soil_data_input, yield_data_input])