Spaces:
Sleeping
Sleeping
Update backend.py
Browse files- backend.py +16 -29
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,6 +162,10 @@ No data β "No data available for [X]. Check that the printer ID is cor
|
|
| 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,20 +184,11 @@ def chat():
|
|
| 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 =
|
| 184 |
-
|
| 185 |
-
|
| 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.
|
| 197 |
print(f"\n[SQL] {sql}\n")
|
| 198 |
|
| 199 |
# ββ Step 2: run the query against Supabase βββββββββββββββββββββββββββββββ
|
|
@@ -210,23 +205,15 @@ def chat():
|
|
| 210 |
print(f"[DB RESULT] {db_text[:500]}\n")
|
| 211 |
|
| 212 |
# ββ Step 3: ask LLM to answer using the retrieved data βββββββββββββββββββ
|
| 213 |
-
answer_resp =
|
| 214 |
-
|
| 215 |
-
|
| 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.
|
| 227 |
-
print(f"[
|
| 228 |
return jsonify({"text": answer})
|
| 229 |
|
| 230 |
|
| 231 |
if __name__ == "__main__":
|
| 232 |
-
app.run(host="0.0.0.0", port=7860, debug=False)
|
|
|
|
| 7 |
from flask_cors import CORS
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from faster_whisper import WhisperModel
|
| 10 |
+
import google.generativeai as genai
|
| 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 |
+
genai.configure(api_key=os.environ["GEMINI_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 |
+
_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 |
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 = _sql_model.generate_content(
|
| 188 |
+
f"Database schema:\n{_SQL_SYSTEM}\n\nprinter_id: {printer_id}\nUser question: {prompt}",
|
| 189 |
+
generation_config=genai.GenerationConfig(max_output_tokens=400),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
print(f"[DB RESULT] {db_text[:500]}\n")
|
| 206 |
|
| 207 |
# ββ Step 3: ask LLM to answer using the retrieved data βββββββββββββββββββ
|
| 208 |
+
answer_resp = _answer_model.generate_content(
|
| 209 |
+
f"Data fetched from database:\n{db_text}\n\nOperator question: {prompt}",
|
| 210 |
+
generation_config=genai.GenerationConfig(max_output_tokens=300),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
)
|
| 212 |
|
| 213 |
+
answer = answer_resp.text
|
| 214 |
+
print(f"[GEMINI ANSWER] {answer}\n")
|
| 215 |
return jsonify({"text": answer})
|
| 216 |
|
| 217 |
|
| 218 |
if __name__ == "__main__":
|
| 219 |
+
app.run(host="0.0.0.0", port=7860, debug=False)
|