ItamarMordo commited on
Commit
fbccb0e
·
verified ·
1 Parent(s): 07dbf58

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +14 -22
server.py CHANGED
@@ -23,7 +23,7 @@ JSON_SOURCE = "database.json"
23
  logging.basicConfig(level=logging.INFO)
24
  logger = logging.getLogger(__name__)
25
 
26
- app = FastAPI(title="Hebrew Smart Home Technician", version="13.1.0")
27
 
28
  app.add_middleware(
29
  CORSMiddleware,
@@ -50,20 +50,12 @@ SEARCH_MAP = {
50
 
51
  # --- שומר שפה (מורחב) ---
52
  def is_valid_text(text: str) -> bool:
53
- """
54
- בודק אם הטקסט מכיל תווים בשפות אסורות.
55
- מחזיר False אם נמצא תו אסור.
56
- """
57
  for char in text:
58
  code = ord(char)
59
- # CJK Unified Ideographs (Chinese)
60
- if 0x4E00 <= code <= 0x9FFF: return False
61
- # Cyrillic (Russian)
62
- if 0x0400 <= code <= 0x04FF: return False
63
- # Hiragana & Katakana (Japanese)
64
- if 0x3040 <= code <= 0x30FF: return False
65
- # Hangul (Korean)
66
- if 0xAC00 <= code <= 0xD7AF: return False
67
  return True
68
 
69
  def init_db():
@@ -104,13 +96,16 @@ def expand_query(query: str) -> List[str]:
104
  return list(search_terms)
105
 
106
  def simplify_schematic_text(text: str) -> str:
 
107
  text = text.replace("ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO", "בקר ESP32")
108
  text = text.replace("BOB_19002", "ממיר רמות")
109
  text = text.replace("06060106_070403", "ממיר רמות")
110
  text = text.replace("TLM-SH_board", "הלוח הראשי")
111
  text = text.replace("StudentBoard", "הלוח הראשי")
112
- text = text.replace("040709_0804060105", "הלוח הראשי")
 
113
 
 
114
  if "NEXTION" in text:
115
  text = text.replace("LCD1 (NEXTION)", "מסך המגע")
116
  text = text.replace("HMI (NEXTION)", "מסך המגע")
@@ -121,6 +116,7 @@ def simplify_schematic_text(text: str) -> str:
121
  text = text.replace("U3", "הלוח")
122
  text = text.replace("U4", "הממיר")
123
 
 
124
  text = text.replace("connects to", "מחובר אל")
125
  text = text.replace("Pin", "פין")
126
 
@@ -241,11 +237,10 @@ async def chat_completions(request: ChatCompletionRequest):
241
  f"\nמידע טכני:\n{context}"
242
  )
243
  else:
244
- # הנחיית חירום משופרת למקרים ללא הקשר
245
  sys_msg = (
246
- "System: You are a helpful assistant speaking ONLY Hebrew.\n"
247
- "Instruction: Reply to the user in Hebrew. Do not use Chinese, Russian, or English.\n"
248
- "If the user says Shalom, say Shalom back."
249
  )
250
 
251
  if request.messages[0].role == "system":
@@ -277,13 +272,10 @@ async def chat_completions(request: ChatCompletionRequest):
277
  async def stream_generator():
278
  full_text = ""
279
  for text in streamer:
280
- # הגנה בזמן אמת: אם מתגלה סינית, עוצרים מיד!
281
  if text:
282
  if not is_valid_text(text):
283
  logger.warning(f"Blocked forbidden text: {text}")
284
- # אופציה א: דלג על התו (עלול ליצור מילים שבורות)
285
- # continue
286
- # אופציה ב (מומלצת): עצור את התשובה כדי לא להציג זבל
287
  break
288
 
289
  full_text += text
 
23
  logging.basicConfig(level=logging.INFO)
24
  logger = logging.getLogger(__name__)
25
 
26
+ app = FastAPI(title="Hebrew Smart Home Technician", version="13.2.0")
27
 
28
  app.add_middleware(
29
  CORSMiddleware,
 
50
 
51
  # --- שומר שפה (מורחב) ---
52
  def is_valid_text(text: str) -> bool:
 
 
 
 
53
  for char in text:
54
  code = ord(char)
55
+ if 0x4E00 <= code <= 0x9FFF: return False # Chinese
56
+ if 0x0400 <= code <= 0x04FF: return False # Russian
57
+ if 0x3040 <= code <= 0x30FF: return False # Japanese
58
+ if 0xAC00 <= code <= 0xD7AF: return False # Korean
 
 
 
 
59
  return True
60
 
61
  def init_db():
 
96
  return list(search_terms)
97
 
98
  def simplify_schematic_text(text: str) -> str:
99
+ # 1. ניקוי שמות רכיבים טכניים מה-DB החדש
100
  text = text.replace("ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO", "בקר ESP32")
101
  text = text.replace("BOB_19002", "ממיר רמות")
102
  text = text.replace("06060106_070403", "ממיר רמות")
103
  text = text.replace("TLM-SH_board", "הלוח הראשי")
104
  text = text.replace("StudentBoard", "הלוח הראשי")
105
+ text = text.replace("040709_0804060105", "הלוח הראשי")
106
+ text = text.replace("0201080606", "הלוח הראשי")
107
 
108
+ # 2. ניקוי שמות גנריים
109
  if "NEXTION" in text:
110
  text = text.replace("LCD1 (NEXTION)", "מסך המגע")
111
  text = text.replace("HMI (NEXTION)", "מסך המגע")
 
116
  text = text.replace("U3", "הלוח")
117
  text = text.replace("U4", "הממיר")
118
 
119
+ # 3. תרגום פעלים
120
  text = text.replace("connects to", "מחובר אל")
121
  text = text.replace("Pin", "פין")
122
 
 
237
  f"\nמידע טכני:\n{context}"
238
  )
239
  else:
240
+ # הנחיית חירום בעברית בלבד
241
  sys_msg = (
242
+ "תפקידך: עוזר חכם הדובר עברית בלבד.\n"
243
+ "הוראה: ענה למשתמש בעברית. אם הוא אומר 'שלום', ענה 'שלום! איך אוכל לעזור?'."
 
244
  )
245
 
246
  if request.messages[0].role == "system":
 
272
  async def stream_generator():
273
  full_text = ""
274
  for text in streamer:
275
+ # הגנה בזמן אמת
276
  if text:
277
  if not is_valid_text(text):
278
  logger.warning(f"Blocked forbidden text: {text}")
 
 
 
279
  break
280
 
281
  full_text += text