Update app.py
Browse files
app.py
CHANGED
|
@@ -3,16 +3,105 @@ from pydantic import BaseModel, Field, validator, ValidationError
|
|
| 3 |
import gradio as gr
|
| 4 |
from openai import OpenAI
|
| 5 |
from typing import List, Dict, Any, Optional, Literal, Union
|
|
|
|
| 6 |
|
| 7 |
# Chatbot model
|
| 8 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 9 |
client = OpenAI()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class Interactions(BaseModel):
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def generate_json(specification):
|
| 17 |
"""
|
| 18 |
Function to prompt OpenAI API to generate structured JSON output.
|
|
@@ -23,10 +112,10 @@ def generate_json(specification):
|
|
| 23 |
response = client.beta.chat.completions.parse(
|
| 24 |
model="gpt-4o-2024-08-06", # Use GPT model that supports structured output
|
| 25 |
messages=[
|
| 26 |
-
{"role": "system", "content": "Extract the farm
|
| 27 |
{"role": "user", "content": specification}
|
| 28 |
],
|
| 29 |
-
response_format=
|
| 30 |
)
|
| 31 |
|
| 32 |
generated_json = response.choices[0].message.parsed
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from openai import OpenAI
|
| 5 |
from typing import List, Dict, Any, Optional, Literal, Union
|
| 6 |
+
from enum import Enum
|
| 7 |
|
| 8 |
# Chatbot model
|
| 9 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 10 |
client = OpenAI()
|
| 11 |
|
| 12 |
+
# These are the necessary components that make up the Interactions
|
| 13 |
+
class Date(BaseModel):
|
| 14 |
+
date: datetime = Field(..., description="Date of the interaction")
|
| 15 |
|
| 16 |
+
class Role(str, Enum):
|
| 17 |
+
PARTNER = 'partner'
|
| 18 |
+
STAFF = 'staff'
|
| 19 |
+
AGRONOMIST = 'agronomist'
|
| 20 |
+
OTHER = 'other'
|
| 21 |
+
|
| 22 |
+
class Person(BaseModel):
|
| 23 |
+
name: str = Field(..., title="Name", description="Name of this person")
|
| 24 |
+
role: str[Role] = Field(..., title="Role", description="Role of this person")
|
| 25 |
+
|
| 26 |
class Interactions(BaseModel):
|
| 27 |
+
people: List[Person] = Field(..., title="People", description="People involved or mentioned during interaction")
|
| 28 |
+
date: Date = Field(..., title="Date", description="Date of the interaction")
|
| 29 |
+
next_meeting: Date = Field(..., title="Date of next meeting", description="Proposed date of the next future interaction")
|
| 30 |
+
next_steps: List[str] = Field(..., title="Next Steps", description="List of individual next steps derived from the interaction")
|
| 31 |
+
summary: str = Field(..., title="Summary", description="Summary of the interaction")
|
| 32 |
+
|
| 33 |
+
# These are the components for Farm Activities, Fields, and Plantings
|
| 34 |
+
class Status(str, Enum):
|
| 35 |
+
ACTIVE = 'active'
|
| 36 |
+
ARCHIVED = 'archived'
|
| 37 |
+
|
| 38 |
+
# Depending on how well this works, come back and hard-code this based on some parameter(s)
|
| 39 |
+
class Convention(str, Enum):
|
| 40 |
+
ACTIVITY: 'log--activity'
|
| 41 |
+
OBSERVATION: 'log--observation'
|
| 42 |
+
FLAMING: 'log--activity--flaming'
|
| 43 |
+
GRAZING: 'log--activity--grazing'
|
| 44 |
+
MOWING: 'log--activity--mowing'
|
| 45 |
+
SOLARIZATION: 'log--activity--solarization'
|
| 46 |
+
TERMINATION: 'log--activity--termination'
|
| 47 |
+
TILLAGE: 'log--activity--tillage'
|
| 48 |
+
HARVEST: 'log--activity--harvest'
|
| 49 |
+
HERBICIDE: 'log--input--herbicide_or_pesticide'
|
| 50 |
+
IRRIGATION: 'log--input--irrigation'
|
| 51 |
+
LIME: 'log--input--lime'
|
| 52 |
+
ORGANIC: 'log--input--organic_matter'
|
| 53 |
+
SEEDTREAT: 'log--input--seed_treatment'
|
| 54 |
+
SEEDLINGTREAT: 'log--input--seedling_treatment'
|
| 55 |
+
MODUS: 'log--lab_test--modus_lab_test'
|
| 56 |
+
SEEDING: 'log--seeding--seeding'
|
| 57 |
+
TRANSPLANT: 'log--transplanting--transplant'
|
| 58 |
+
|
| 59 |
+
class Structure(str, Enum):
|
| 60 |
+
CLAY: 'clay'
|
| 61 |
+
SANDYCLAY: 'sandy clay'
|
| 62 |
+
SILTYCLAY: 'silty clay'
|
| 63 |
+
SANDYCLAYLOAM: 'sandy clay loam'
|
| 64 |
+
SILYCLAMLOAM: 'silty clay loam'
|
| 65 |
+
CLAYLOAM: 'clay loam'
|
| 66 |
+
SANDYLOAM: 'sandy loam'
|
| 67 |
+
SILTLOAM: 'silt loam'
|
| 68 |
+
LOAM: 'loam'
|
| 69 |
+
LOAMYSAND: 'loamy sand'
|
| 70 |
+
SAND: 'sand'
|
| 71 |
+
SILT: 'silt'
|
| 72 |
+
|
| 73 |
+
class Soil(BaseModel):
|
| 74 |
+
description: str = Field(..., title="Description", description="A general description of the soil")
|
| 75 |
+
structure: str[Structure] = Field(..., title="Structure", description="The structure of the soil using options from the major soil texture classes (sand, clay, silt)")
|
| 76 |
+
|
| 77 |
+
class Log(BaseModel):
|
| 78 |
+
convention: str[Convention] = Field(..., title="Logs", description="This log's convention (i.e. this log's category or type)")
|
| 79 |
+
date: Date = Field(..., title="Date", description="Date the log (i.e. action of the activity or input) was performed")
|
| 80 |
+
description: str = Field(..., title="Description", description="A description of the details of the log (i.e. details about farm activity performed")
|
| 81 |
+
|
| 82 |
+
class Planting(BaseModel):
|
| 83 |
+
name: str = Field(..., title="Name", description="The name of the planting")
|
| 84 |
+
status: str[Status] = Field(..., title="Status", description="The status of the planting. \"active\" is a planting which is currently still in the field. \"archived\" is a planting which is no longer in the field (has been terminated or harvested)")
|
| 85 |
+
crop: List[str] = Field(..., title="Crop", description="A list of the crops in this planting")
|
| 86 |
+
variety: List[str] = Field(..., title="Variety", description="A list of the crop varieties in this planting")
|
| 87 |
+
logs: List[Log] = Field(..., title="Logs", description="A list of all the logs that are associated with the farm activities")
|
| 88 |
+
structure: List[Structure] = Field(..., title="Structure", description="Information about the soil associated with or observed during the time of this planting")
|
| 89 |
+
biology: str = Field(..., title="Biology", description="Biological activity levels of the soil, including fluffiness, worms and bugs, and other evidence of soil biological activity")
|
| 90 |
|
| 91 |
+
class Yield(BaseModel):
|
| 92 |
+
quantity: str = Field(..., title="Quantity", description="A description of the total yield (harvested amount) from this planting, including units when available")
|
| 93 |
+
quality: str = Field(..., title="Quality", description="The product quality of the harvest. For example, small or large fruits, sweet or tart flavor, easily molding or containing mold, high number of product seconds, etc.")
|
| 94 |
|
| 95 |
+
class Farm_Activities(BaseModel):
|
| 96 |
+
name: str = Field(..., title="Name", description="The name of the agricultural field.")
|
| 97 |
+
description: str = Field(..., title="Description", description="The description of the agricultural field.")
|
| 98 |
+
plantings: List[Planting] = Field(..., title="Plantings", description="All of the plantings which have occurred on this field.")
|
| 99 |
+
harvest_yield: Yield = Field(..., title="Yield", description="Quantitative and qualitative observations regarding the yield of harvest")
|
| 100 |
+
|
| 101 |
+
# These are the components for Trials
|
| 102 |
+
# TBD
|
| 103 |
+
|
| 104 |
+
# This is to make stuff happen
|
| 105 |
def generate_json(specification):
|
| 106 |
"""
|
| 107 |
Function to prompt OpenAI API to generate structured JSON output.
|
|
|
|
| 112 |
response = client.beta.chat.completions.parse(
|
| 113 |
model="gpt-4o-2024-08-06", # Use GPT model that supports structured output
|
| 114 |
messages=[
|
| 115 |
+
{"role": "system", "content": "Extract the farm information."},
|
| 116 |
{"role": "user", "content": specification}
|
| 117 |
],
|
| 118 |
+
response_format=Farm_Activities,
|
| 119 |
)
|
| 120 |
|
| 121 |
generated_json = response.choices[0].message.parsed
|