Spaces:
Sleeping
Sleeping
fortinaiti i la babaji
Browse files- backend.py +28 -15
backend.py
CHANGED
|
@@ -7,7 +7,7 @@ from flask import Flask, request, jsonify
|
|
| 7 |
from flask_cors import CORS
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from faster_whisper import WhisperModel
|
| 10 |
-
import
|
| 11 |
from supabase import create_client
|
| 12 |
|
| 13 |
load_dotenv()
|
|
@@ -21,7 +21,7 @@ print("Loading Whisper model (base)β¦")
|
|
| 21 |
whisper_model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 22 |
print("Whisper ready.")
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
SYSTEM_PROMPT = """You are a concise diagnostic assistant for HP Metal Jet S100 industrial 3D metal printers.
|
| 27 |
You receive real-time sensor data and answer operator questions in 1-3 short sentences.
|
|
@@ -162,10 +162,6 @@ No data β "No data available for [X]. Check that the printer ID is cor
|
|
| 162 |
"""
|
| 163 |
|
| 164 |
|
| 165 |
-
_sql_model = genai.GenerativeModel(model_name="gemini-2.0-flash", system_instruction=_SQL_SYSTEM)
|
| 166 |
-
_answer_model = genai.GenerativeModel(model_name="gemini-2.0-flash", system_instruction=_ANSWER_SYSTEM)
|
| 167 |
-
|
| 168 |
-
|
| 169 |
def _extract_sql(text: str) -> str:
|
| 170 |
text = text.strip()
|
| 171 |
# strip markdown code fences if the model adds them anyway
|
|
@@ -184,11 +180,20 @@ def chat():
|
|
| 184 |
return jsonify({"error": "prompt is required"}), 400
|
| 185 |
|
| 186 |
# ββ Step 1: ask LLM to generate a SQL query for the needed data ββββββββββ
|
| 187 |
-
sql_resp =
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
)
|
| 191 |
-
sql = _extract_sql(sql_resp.text)
|
| 192 |
print(f"\n[SQL] {sql}\n")
|
| 193 |
|
| 194 |
# ββ Step 2: run the query against Supabase βββββββββββββββββββββββββββββββ
|
|
@@ -205,13 +210,21 @@ def chat():
|
|
| 205 |
print(f"[DB RESULT] {db_text[:500]}\n")
|
| 206 |
|
| 207 |
# ββ Step 3: ask LLM to answer using the retrieved data βββββββββββββββββββ
|
| 208 |
-
answer_resp =
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
-
answer = answer_resp.text
|
| 214 |
-
print(f"[
|
| 215 |
return jsonify({"text": answer})
|
| 216 |
|
| 217 |
|
|
|
|
| 7 |
from flask_cors import CORS
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from faster_whisper import WhisperModel
|
| 10 |
+
import anthropic
|
| 11 |
from supabase import create_client
|
| 12 |
|
| 13 |
load_dotenv()
|
|
|
|
| 21 |
whisper_model = WhisperModel("base", device="cpu", compute_type="int8")
|
| 22 |
print("Whisper ready.")
|
| 23 |
|
| 24 |
+
claude = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
|
| 25 |
|
| 26 |
SYSTEM_PROMPT = """You are a concise diagnostic assistant for HP Metal Jet S100 industrial 3D metal printers.
|
| 27 |
You receive real-time sensor data and answer operator questions in 1-3 short sentences.
|
|
|
|
| 162 |
"""
|
| 163 |
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
def _extract_sql(text: str) -> str:
|
| 166 |
text = text.strip()
|
| 167 |
# strip markdown code fences if the model adds them anyway
|
|
|
|
| 180 |
return jsonify({"error": "prompt is required"}), 400
|
| 181 |
|
| 182 |
# ββ Step 1: ask LLM to generate a SQL query for the needed data ββββββββββ
|
| 183 |
+
sql_resp = claude.messages.create(
|
| 184 |
+
model="claude-haiku-4-5-20251001",
|
| 185 |
+
max_tokens=400,
|
| 186 |
+
system=_SQL_SYSTEM,
|
| 187 |
+
messages=[{
|
| 188 |
+
"role": "user",
|
| 189 |
+
"content": (
|
| 190 |
+
f"Database schema:\n{_SQL_SYSTEM}\n\n"
|
| 191 |
+
f"printer_id: {printer_id}\n"
|
| 192 |
+
f"User question: {prompt}"
|
| 193 |
+
),
|
| 194 |
+
}],
|
| 195 |
)
|
| 196 |
+
sql = _extract_sql(sql_resp.content[0].text)
|
| 197 |
print(f"\n[SQL] {sql}\n")
|
| 198 |
|
| 199 |
# ββ Step 2: run the query against Supabase βββββββββββββββββββββββββββββββ
|
|
|
|
| 210 |
print(f"[DB RESULT] {db_text[:500]}\n")
|
| 211 |
|
| 212 |
# ββ Step 3: ask LLM to answer using the retrieved data βββββββββββββββββββ
|
| 213 |
+
answer_resp = claude.messages.create(
|
| 214 |
+
model="claude-haiku-4-5-20251001",
|
| 215 |
+
max_tokens=300,
|
| 216 |
+
system=_ANSWER_SYSTEM,
|
| 217 |
+
messages=[{
|
| 218 |
+
"role": "user",
|
| 219 |
+
"content": (
|
| 220 |
+
f"Data fetched from database:\n{db_text}\n\n"
|
| 221 |
+
f"Operator question: {prompt}"
|
| 222 |
+
),
|
| 223 |
+
}],
|
| 224 |
)
|
| 225 |
|
| 226 |
+
answer = answer_resp.content[0].text
|
| 227 |
+
print(f"[CLAUDE ANSWER] {answer}\n")
|
| 228 |
return jsonify({"text": answer})
|
| 229 |
|
| 230 |
|