lukmanaj commited on
Commit
d551f05
·
verified ·
1 Parent(s): ef98f76

Update app2.py

Browse files
Files changed (1) hide show
  1. app2.py +37 -14
app2.py CHANGED
@@ -5,7 +5,7 @@ import json
5
  import re
6
 
7
  MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
8
-
9
  PROMPT_TEMPLATE = """
10
  You are a clinical triage assistant. A patient describes their symptoms as follows:
11
 
@@ -64,23 +64,46 @@ def triage_response(symptoms: str) -> dict:
64
 
65
  prompt = build_prompt(symptoms)
66
 
67
- body = {
68
- "model": "mistral-large-latest",
69
- "messages": [{"role": "user", "content": prompt}],
70
- "temperature": 0.3
71
- }
72
-
73
  try:
74
- response = requests.post(MISTRAL_API_URL, headers=headers, json=body)
75
- response.raise_for_status()
76
- raw_output = response.json()["choices"][0]["message"]["content"]
 
 
 
 
 
 
 
 
 
 
 
 
77
  cleaned_output = extract_json(raw_output)
 
78
  try:
79
- return json.loads(cleaned_output)
80
- except Exception as e:
81
- return {"error": "Invalid JSON format", "raw_output": raw_output, "exception": str(e)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  except Exception as e:
83
- return {"error": str(e)}
 
84
 
85
  # Launch Gradio app with MCP support
86
  demo = gr.Interface(
 
5
  import re
6
 
7
  MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
8
+ client = Mistral(api_key=MISTRAL_API_KEY) if MISTRAL_API_KEY else None
9
  PROMPT_TEMPLATE = """
10
  You are a clinical triage assistant. A patient describes their symptoms as follows:
11
 
 
64
 
65
  prompt = build_prompt(symptoms)
66
 
 
 
 
 
 
 
67
  try:
68
+ # Create chat completion using Mistral client
69
+ response = client.chat.complete(
70
+ model="mistral-large-latest",
71
+ messages=[
72
+ {
73
+ "role": "user",
74
+ "content": prompt
75
+ }
76
+ ],
77
+ temperature=0.3,
78
+ max_tokens=500
79
+ )
80
+
81
+ # Extract content from response
82
+ raw_output = response.choices[0].message.content
83
  cleaned_output = extract_json(raw_output)
84
+
85
  try:
86
+ result = json.loads(cleaned_output)
87
+ # Validate required fields
88
+ required_fields = ["urgency_level", "possible_condition", "recommended_action"]
89
+ if all(field in result for field in required_fields):
90
+ return result
91
+ else:
92
+ return {
93
+ "error": "Missing required fields in response",
94
+ "raw_output": raw_output,
95
+ "parsed_result": result
96
+ }
97
+ except json.JSONDecodeError as e:
98
+ return {
99
+ "error": "Invalid JSON format",
100
+ "raw_output": raw_output,
101
+ "exception": str(e)
102
+ }
103
+
104
  except Exception as e:
105
+ return {"error": f"Mistral API error: {str(e)}"}
106
+
107
 
108
  # Launch Gradio app with MCP support
109
  demo = gr.Interface(