Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,27 +7,63 @@ GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
|
| 7 |
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 8 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 9 |
|
| 10 |
-
SYSTEM_PREAMBLE = """You are
|
| 11 |
-
Answer truthfully and concisely using
|
| 12 |
-
If you are not certain, say so plainly
|
| 13 |
-
|
| 14 |
-
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def get_ai_response(user_message, history):
|
| 18 |
if not GROQ_API_KEY:
|
| 19 |
return "β οΈ GROQ_API_KEY not set. Add it in Space β Settings β Secrets."
|
| 20 |
|
| 21 |
-
# Build messages β keep last 6 turns for speed
|
| 22 |
messages = [{"role": "system", "content": SYSTEM_PREAMBLE}]
|
| 23 |
for user_text, assistant_text in history[-6:]:
|
| 24 |
messages.append({"role": "user", "content": user_text})
|
| 25 |
messages.append({"role": "assistant", "content": assistant_text})
|
| 26 |
messages.append({"role": "user", "content": user_message})
|
| 27 |
|
| 28 |
-
# Call Groq β near-instant β‘
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
GROQ_API_URL,
|
| 32 |
headers={
|
| 33 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
@@ -41,42 +77,50 @@ def get_ai_response(user_message, history):
|
|
| 41 |
},
|
| 42 |
timeout=30
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return data["choices"][0]["message"]["content"].strip()
|
| 48 |
-
else:
|
| 49 |
-
return f"β οΈ API Error {response.status_code}: {response.text[:100]}"
|
| 50 |
-
|
| 51 |
except Exception as e:
|
| 52 |
return f"β οΈ Connection error: {str(e)}"
|
| 53 |
|
| 54 |
-
# ββ
|
| 55 |
-
with gr.Blocks(title="ForgeSync
|
| 56 |
-
gr.Markdown("
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
return "", history
|
| 64 |
-
history = history + [[
|
| 65 |
-
|
| 66 |
-
history[-1][1] =
|
| 67 |
return "", history
|
| 68 |
-
|
| 69 |
with gr.Row():
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 78 |
-
submit_btn.click(respond, [msg, chatbot], [msg, chatbot])
|
| 79 |
|
| 80 |
-
# ββ
|
| 81 |
if __name__ == "__main__":
|
| 82 |
demo.launch()
|
|
|
|
| 7 |
GROQ_MODEL = "llama-3.3-70b-versatile"
|
| 8 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 9 |
|
| 10 |
+
SYSTEM_PREAMBLE = """You are Forge AI, an intelligent assistant for an Australian disability support platform.
|
| 11 |
+
You are careful, factual, and professional. Answer truthfully and concisely using verifiable information.
|
| 12 |
+
If you are not certain, say so plainly β do not invent regulations, legislation, medical advice, or data.
|
| 13 |
+
Use markdown formatting: **bold**, bullet points, and clear sections where helpful.
|
| 14 |
+
Always note that critical decisions should be verified with qualified professionals."""
|
| 15 |
|
| 16 |
+
PROMPT_CATEGORIES = [
|
| 17 |
+
{
|
| 18 |
+
"label": "Participants & Care",
|
| 19 |
+
"prompts": [
|
| 20 |
+
"Summarise today's shift notes and progress notes. List key highlights and anything requiring follow-up.",
|
| 21 |
+
"Give me a wellbeing snapshot. Include mood trends, health concerns, and quality-of-life observations.",
|
| 22 |
+
"Identify participants with active alerts or overdue assessments. Summarise key risks and recommend actions.",
|
| 23 |
+
"Draft a goal progress report for the current month."
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"label": "Compliance & Risk",
|
| 28 |
+
"prompts": [
|
| 29 |
+
"Review recent incidents. Flag any that may involve restrictive practices or mandatory reporting obligations.",
|
| 30 |
+
"Assess readiness against NDIS Practice Standards. Identify gaps and give a readiness score out of 10.",
|
| 31 |
+
"Summarise worker compliance status. Who has expiring credentials or overdue training?",
|
| 32 |
+
"List any potential mandatory reporting obligations that may apply to recent incidents."
|
| 33 |
+
]
|
| 34 |
+
},
|
| 35 |
+
{
|
| 36 |
+
"label": "Funding & Finance",
|
| 37 |
+
"prompts": [
|
| 38 |
+
"Forecast which participants are at risk of running out of funding before their plan end date.",
|
| 39 |
+
"Summarise budget utilisation. Flag those over 80% with >3 months remaining and those under 40%.",
|
| 40 |
+
"Check for billing anomalies β unsubmitted claims, duplicate charges, or shifts without matching timesheets."
|
| 41 |
+
]
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"label": "Rostering & Operations",
|
| 45 |
+
"prompts": [
|
| 46 |
+
"Identify any shifts this week that are unassigned or have no confirmed worker.",
|
| 47 |
+
"Analyse worker workloads. Who is overworked and who has capacity?",
|
| 48 |
+
"Generate a comprehensive end-of-day handover summary.",
|
| 49 |
+
"List participants with NDIS plan reviews due in the next 60 days."
|
| 50 |
+
]
|
| 51 |
+
}
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
# ββ AI Call ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
def get_ai_response(user_message, history):
|
| 56 |
if not GROQ_API_KEY:
|
| 57 |
return "β οΈ GROQ_API_KEY not set. Add it in Space β Settings β Secrets."
|
| 58 |
|
|
|
|
| 59 |
messages = [{"role": "system", "content": SYSTEM_PREAMBLE}]
|
| 60 |
for user_text, assistant_text in history[-6:]:
|
| 61 |
messages.append({"role": "user", "content": user_text})
|
| 62 |
messages.append({"role": "assistant", "content": assistant_text})
|
| 63 |
messages.append({"role": "user", "content": user_message})
|
| 64 |
|
|
|
|
| 65 |
try:
|
| 66 |
+
resp = requests.post(
|
| 67 |
GROQ_API_URL,
|
| 68 |
headers={
|
| 69 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
|
|
| 77 |
},
|
| 78 |
timeout=30
|
| 79 |
)
|
| 80 |
+
if resp.status_code == 200:
|
| 81 |
+
return resp.json()["choices"][0]["message"]["content"].strip()
|
| 82 |
+
return f"β οΈ Error {resp.status_code}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
return f"β οΈ Connection error: {str(e)}"
|
| 85 |
|
| 86 |
+
# ββ UI ββββββοΏ½οΏ½ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 87 |
+
with gr.Blocks(title="ForgeSync AI", css="footer {display: none}") as demo:
|
| 88 |
+
gr.Markdown("""
|
| 89 |
+
# π§ ForgeSync AI
|
| 90 |
+
Your intelligent assistant for NDIS care operations β replies in ~1β2 seconds
|
| 91 |
+
""")
|
| 92 |
+
|
| 93 |
+
# Quick Prompts
|
| 94 |
+
active_cat = gr.State(0)
|
| 95 |
+
with gr.Accordion("π‘ Quick Prompts", open=True):
|
| 96 |
+
cat_buttons = [gr.Button(cat["label"], size="sm") for cat in PROMPT_CATEGORIES]
|
| 97 |
+
prompt_buttons = []
|
| 98 |
+
for idx, cat in enumerate(PROMPT_CATEGORIES):
|
| 99 |
+
with gr.Row(visible=(idx == 0)) as row:
|
| 100 |
+
for p in cat["prompts"]:
|
| 101 |
+
prompt_buttons.append(gr.Button(p, size="sm"))
|
| 102 |
+
active_cat[gr.Button] = row # type: ignore
|
| 103 |
+
|
| 104 |
+
# Chat
|
| 105 |
+
chatbot = gr.Chatbot(height=450, bubble_full_width=False)
|
| 106 |
+
|
| 107 |
+
def respond(msg, history):
|
| 108 |
+
if not msg.strip():
|
| 109 |
return "", history
|
| 110 |
+
history = history + [[msg, None]]
|
| 111 |
+
reply = get_ai_response(msg, history[:-1])
|
| 112 |
+
history[-1][1] = reply
|
| 113 |
return "", history
|
| 114 |
+
|
| 115 |
with gr.Row():
|
| 116 |
+
txt = gr.Textbox(placeholder="Ask anything about participants, compliance, funding, rostering...", scale=4)
|
| 117 |
+
send = gr.Button("Send", variant="primary")
|
| 118 |
+
|
| 119 |
+
txt.submit(respond, [txt, chatbot], [txt, chatbot])
|
| 120 |
+
send.click(respond, [txt, chatbot], [txt, chatbot])
|
| 121 |
+
|
| 122 |
+
gr.Markdown("*Always verify critical decisions with qualified professionals.*")
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
# ββ Run ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 125 |
if __name__ == "__main__":
|
| 126 |
demo.launch()
|