Spaces:
Sleeping
Sleeping
update chatgpt function
Browse files
app.py
CHANGED
@@ -99,45 +99,46 @@ def get_bmi_classes_range(predicted_bmi_class, bmi_range):
|
|
99 |
|
100 |
def chatgpt_openai(height="", gender="", bmi_class_model1="", age=""):
|
101 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
Do not include any explanations, only provide
|
106 |
|
107 |
-
Do not output something that begins with "```json". please give json in define format.
|
108 |
-
|
109 |
Output JSON in this format:
|
110 |
{{
|
111 |
"bmi_range": {{
|
112 |
-
"min":
|
113 |
-
"max":
|
114 |
}}
|
115 |
}}"""
|
116 |
-
client = OpenAI()
|
117 |
-
completion = client.chat.completions.create(
|
118 |
-
model="gpt-4o",
|
119 |
-
messages=[
|
120 |
-
{
|
121 |
-
"role": "system",
|
122 |
-
"content": "You are a health expert specializing in analyzing patient information, including gender, height, and BMI classification, to provide data-driven health insights based on USA statistics.",
|
123 |
-
},
|
124 |
-
{"role": "user", "content": content},
|
125 |
-
],
|
126 |
-
)
|
127 |
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
# Check if the response has 'choices' and it's a list with at least one element
|
132 |
-
if "choices"
|
133 |
raise ValueError("Invalid response structure from OpenAI API")
|
134 |
|
135 |
-
#
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
139 |
try:
|
140 |
-
json_object = json.loads(
|
141 |
except json.JSONDecodeError as e:
|
142 |
print(f"JSON Decode Error: {e}")
|
143 |
raise ValueError("Failed to parse JSON response from OpenAI API")
|
|
|
99 |
|
100 |
def chatgpt_openai(height="", gender="", bmi_class_model1="", age=""):
|
101 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
102 |
+
|
103 |
+
content = f"""I have a person {gender} who is {age} and classified as {bmi_class_model1}.
|
104 |
+
Can you suggest a reasonable range of BMI values that typically represent people in this category?
|
105 |
+
Do not include any explanations, only provide an RFC8259 compliant JSON response following this format without deviation.
|
106 |
|
|
|
|
|
107 |
Output JSON in this format:
|
108 |
{{
|
109 |
"bmi_range": {{
|
110 |
+
"min": 10.0,
|
111 |
+
"max": 18.4
|
112 |
}}
|
113 |
}}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
try:
|
116 |
+
client = OpenAI()
|
117 |
+
response = client.chat.completions.create(
|
118 |
+
model="gpt-4",
|
119 |
+
messages=[
|
120 |
+
{"role": "system", "content": "You are a health expert specializing in analyzing patient information, including gender, height, and BMI classification, to provide data-driven health insights based on USA statistics."},
|
121 |
+
{"role": "user", "content": content},
|
122 |
+
],
|
123 |
+
)
|
124 |
+
except Exception as e:
|
125 |
+
print(f"Error calling OpenAI API: {e}")
|
126 |
+
raise ValueError("Failed to connect to OpenAI API")
|
127 |
+
|
128 |
+
# Print the entire response to understand its structure
|
129 |
+
print("Raw response from OpenAI:", response)
|
130 |
|
131 |
# Check if the response has 'choices' and it's a list with at least one element
|
132 |
+
if not hasattr(response, "choices") or len(response.choices) == 0:
|
133 |
raise ValueError("Invalid response structure from OpenAI API")
|
134 |
|
135 |
+
# Extract the content from the first choice
|
136 |
+
message_content = response.choices[0].message.content
|
137 |
+
print("Response content to be parsed:", message_content)
|
138 |
+
|
139 |
+
# Parse the content as JSON
|
140 |
try:
|
141 |
+
json_object = json.loads(message_content.strip())
|
142 |
except json.JSONDecodeError as e:
|
143 |
print(f"JSON Decode Error: {e}")
|
144 |
raise ValueError("Failed to parse JSON response from OpenAI API")
|