Spaces:
Sleeping
Sleeping
Update server.py
Browse files
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.
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
@@ -48,7 +48,7 @@ SEARCH_MAP = {
|
|
| 48 |
"מתח": ["3.3V", "5V", "VIN", "Power"]
|
| 49 |
}
|
| 50 |
|
| 51 |
-
# --- שומר שפה
|
| 52 |
def is_valid_text(text: str) -> bool:
|
| 53 |
for char in text:
|
| 54 |
code = ord(char)
|
|
@@ -98,25 +98,23 @@ def expand_query(query: str) -> List[str]:
|
|
| 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("
|
| 102 |
-
text = text.replace("
|
| 103 |
-
text = text.replace("TLM-SH_board", "הלוח הראשי")
|
| 104 |
text = text.replace("StudentBoard", "הלוח הראשי")
|
| 105 |
-
text = text.replace("
|
| 106 |
-
text = text.replace("
|
| 107 |
|
| 108 |
# 2. ניקוי שמות גנריים
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
-
text = text.replace("
|
| 116 |
-
text = text.replace("
|
| 117 |
-
text = text.replace("U4", "הממיר")
|
| 118 |
|
| 119 |
-
#
|
| 120 |
text = text.replace("connects to", "מחובר אל")
|
| 121 |
text = text.replace("Pin", "פין")
|
| 122 |
|
|
@@ -162,10 +160,10 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 162 |
relevant_lines.append(f"- {clean_line}")
|
| 163 |
|
| 164 |
if relevant_lines:
|
| 165 |
-
context_str = "רשימת החיבורים
|
| 166 |
return context_str, search_terms
|
| 167 |
else:
|
| 168 |
-
return "נמצא
|
| 169 |
|
| 170 |
return raw_content, search_terms
|
| 171 |
|
|
@@ -187,7 +185,7 @@ class Message(BaseModel):
|
|
| 187 |
class ChatCompletionRequest(BaseModel):
|
| 188 |
messages: List[Message]
|
| 189 |
max_tokens: Optional[int] = 512
|
| 190 |
-
temperature: Optional[float] = 0.
|
| 191 |
stream: Optional[bool] = False
|
| 192 |
|
| 193 |
@app.on_event("startup")
|
|
@@ -228,19 +226,21 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 228 |
context, _ = query_knowledge_base(last_user_msg.content)
|
| 229 |
|
| 230 |
if context:
|
|
|
|
| 231 |
sys_msg = (
|
| 232 |
-
"
|
| 233 |
-
"
|
| 234 |
-
"1.
|
| 235 |
-
"2.
|
| 236 |
-
"3.
|
| 237 |
-
|
|
|
|
| 238 |
)
|
| 239 |
else:
|
| 240 |
-
# הנחיית חירום בעברית בלבד
|
| 241 |
sys_msg = (
|
| 242 |
-
"
|
| 243 |
-
"
|
|
|
|
| 244 |
)
|
| 245 |
|
| 246 |
if request.messages[0].role == "system":
|
|
@@ -256,11 +256,11 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 256 |
gen_kwargs = dict(
|
| 257 |
inputs=prompt_ids,
|
| 258 |
max_new_tokens=request.max_tokens,
|
| 259 |
-
temperature=0.
|
| 260 |
top_p=0.9,
|
| 261 |
do_sample=True,
|
| 262 |
pad_token_id=tokenizer.eos_token_id,
|
| 263 |
-
repetition_penalty=1.1
|
| 264 |
)
|
| 265 |
|
| 266 |
if request.stream:
|
|
@@ -272,10 +272,9 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 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
|
| 279 |
break
|
| 280 |
|
| 281 |
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.3.0")
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 48 |
"מתח": ["3.3V", "5V", "VIN", "Power"]
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# --- שומר שפה ---
|
| 52 |
def is_valid_text(text: str) -> bool:
|
| 53 |
for char in text:
|
| 54 |
code = ord(char)
|
|
|
|
| 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("LevelConvertor", "ממיר רמות") # חשוב: הסרת השם באנגלית
|
| 102 |
+
text = text.replace("BOB_19002", "") # הסרת המק\"ט המיותר
|
|
|
|
| 103 |
text = text.replace("StudentBoard", "הלוח הראשי")
|
| 104 |
+
text = text.replace("0201080606", "")
|
| 105 |
+
text = text.replace("040709_0804060105", "")
|
| 106 |
|
| 107 |
# 2. ניקוי שמות גנריים
|
| 108 |
+
text = text.replace("LCD1 (NEXTION)", "מסך המגע")
|
| 109 |
+
text = text.replace("HMI (NEXTION)", "מסך המגע")
|
| 110 |
+
text = text.replace("HMI", "המסך")
|
| 111 |
+
text = text.replace("NEXTION", "המסך")
|
| 112 |
|
| 113 |
+
# 3. ניקוי שאריות
|
| 114 |
+
text = text.replace("()", "") # הסרת סוגריים ריקים שנשארו
|
| 115 |
+
text = text.replace(" ", " ") # הסרת רווחים כפולים
|
|
|
|
| 116 |
|
| 117 |
+
# 4. תרגום פעלים
|
| 118 |
text = text.replace("connects to", "מחובר אל")
|
| 119 |
text = text.replace("Pin", "פין")
|
| 120 |
|
|
|
|
| 160 |
relevant_lines.append(f"- {clean_line}")
|
| 161 |
|
| 162 |
if relevant_lines:
|
| 163 |
+
context_str = "רשימת החיבורים שנמצאה:\n" + "\n".join(relevant_lines)
|
| 164 |
return context_str, search_terms
|
| 165 |
else:
|
| 166 |
+
return "נמצא שרטוט, אך לא נמצאו חיבורים ספציפיים לרכיב זה.", search_terms
|
| 167 |
|
| 168 |
return raw_content, search_terms
|
| 169 |
|
|
|
|
| 185 |
class ChatCompletionRequest(BaseModel):
|
| 186 |
messages: List[Message]
|
| 187 |
max_tokens: Optional[int] = 512
|
| 188 |
+
temperature: Optional[float] = 0.2
|
| 189 |
stream: Optional[bool] = False
|
| 190 |
|
| 191 |
@app.on_event("startup")
|
|
|
|
| 226 |
context, _ = query_knowledge_base(last_user_msg.content)
|
| 227 |
|
| 228 |
if context:
|
| 229 |
+
# הנחיית מערכת באנגלית - עובד הרבה יותר טוב למודל הזה
|
| 230 |
sys_msg = (
|
| 231 |
+
"You are a technical expert for the 'Telem Smart Home' project. You speak Hebrew.\n"
|
| 232 |
+
"Instructions:\n"
|
| 233 |
+
"1. Answer ONLY in Hebrew. Do not use English words unless they are part names (like ESP32).\n"
|
| 234 |
+
"2. Use the provided Context below to answer.\n"
|
| 235 |
+
"3. Summarize the connections. For example, say 'The screen connects to the Level Shifter'.\n"
|
| 236 |
+
"4. Do not make up information.\n"
|
| 237 |
+
f"\nContext:\n{context}"
|
| 238 |
)
|
| 239 |
else:
|
|
|
|
| 240 |
sys_msg = (
|
| 241 |
+
"You are a Hebrew-speaking assistant. "
|
| 242 |
+
"If the user says 'Shalom', reply with 'שלום! איך אוכל לעזור?'. "
|
| 243 |
+
"Otherwise, say you don't have that information."
|
| 244 |
)
|
| 245 |
|
| 246 |
if request.messages[0].role == "system":
|
|
|
|
| 256 |
gen_kwargs = dict(
|
| 257 |
inputs=prompt_ids,
|
| 258 |
max_new_tokens=request.max_tokens,
|
| 259 |
+
temperature=0.2,
|
| 260 |
top_p=0.9,
|
| 261 |
do_sample=True,
|
| 262 |
pad_token_id=tokenizer.eos_token_id,
|
| 263 |
+
repetition_penalty=1.1 # מאוזן
|
| 264 |
)
|
| 265 |
|
| 266 |
if request.stream:
|
|
|
|
| 272 |
async def stream_generator():
|
| 273 |
full_text = ""
|
| 274 |
for text in streamer:
|
|
|
|
| 275 |
if text:
|
| 276 |
if not is_valid_text(text):
|
| 277 |
+
logger.warning(f"Blocked: {text}")
|
| 278 |
break
|
| 279 |
|
| 280 |
full_text += text
|