File size: 1,671 Bytes
98d7794
94e028a
ae8a9c9
 
98d7794
 
 
 
 
ae8a9c9
94e028a
 
98d7794
94e028a
98d7794
 
ae8a9c9
 
48864a3
 
 
ae8a9c9
48864a3
ae8a9c9
48864a3
 
 
69d064b
48864a3
ae8a9c9
48864a3
ae8a9c9
 
69d064b
48864a3
 
ae8a9c9
 
98d7794
 
ae8a9c9
 
 
 
 
 
98d7794
ae8a9c9
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
from openai import AzureOpenAI
import json

# Access secrets from Hugging Face environment
AZURE_API_KEY = os.getenv("AZURE_API_KEY")
AZURE_ENDPOINT = os.getenv("AZURE_ENDPOINT")
API_VERSION = os.getenv("API_VERSION")
MODEL_NAME = os.getenv("MODEL_NAME")

# Use AzureOpenAI instead of OpenAI
client = AzureOpenAI(
    api_key=AZURE_API_KEY,
    azure_endpoint=AZURE_ENDPOINT,  # Use azure_endpoint instead of base_url
    api_version=API_VERSION
)

def analyze_parameter(test_name, value, reference):
    """Get AI analysis with strict output control"""
    prompt = f"""Analyze this medical parameter:
    Test: {test_name}
    Value: {value}
    Reference: {reference}

    Return JSON with:
    - status: "Good"/"Moderate"/"Immediate Attention"
    - reason: 15-word explanation
    - food: 3 specific food items
    - exercise: 1 measurable activity

    Example: {{
        "status": "Immediate Attention",
        "reason": "High LDL increases cardiovascular risk",
        "food": "Oats, walnuts, olive oil",
        "exercise": "45-min daily brisk walking"
    }}"""

    try:
        response = client.chat.completions.create(
            model=MODEL_NAME,
            messages=[{"role": "user", "content": prompt}],
            temperature=0.1,
            response_format={"type": "json_object"}
        )
        return json.loads(response.choices[0].message.content)
    except Exception as e:
        print(f"API Error: {str(e)}")
        return {
            "status": "Immediate Attention",
            "reason": "Requires professional evaluation",
            "food": "Maintain balanced diet",
            "exercise": "Consult doctor"
        }