Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -14,8 +14,7 @@ from pydantic import BaseModel
|
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
| 16 |
# --- Configuration ---
|
| 17 |
-
|
| 18 |
-
MODEL_ID = "Qwen/Qwen2.5-3B-Instruct"
|
| 19 |
PORT = int(os.environ.get("PORT", 8000))
|
| 20 |
HOST = "0.0.0.0"
|
| 21 |
DB_FILE = "chatbot.db"
|
|
@@ -24,7 +23,7 @@ JSON_SOURCE = "database.json"
|
|
| 24 |
logging.basicConfig(level=logging.INFO)
|
| 25 |
logger = logging.getLogger(__name__)
|
| 26 |
|
| 27 |
-
app = FastAPI(title="Hebrew Smart Home Technician
|
| 28 |
|
| 29 |
app.add_middleware(
|
| 30 |
CORSMiddleware,
|
|
@@ -38,19 +37,6 @@ model = None
|
|
| 38 |
tokenizer = None
|
| 39 |
device = "cpu"
|
| 40 |
|
| 41 |
-
# --- Language Guard (New Feature) ---
|
| 42 |
-
def is_valid_text(text: str) -> bool:
|
| 43 |
-
"""
|
| 44 |
-
Checks if text contains forbidden characters (Chinese, Cyrillic/Russian).
|
| 45 |
-
Returns False if forbidden characters are found.
|
| 46 |
-
"""
|
| 47 |
-
for char in text:
|
| 48 |
-
# CJK Unified Ideographs (Chinese)
|
| 49 |
-
if 0x4E00 <= ord(char) <= 0x9FFF: return False
|
| 50 |
-
# Cyrillic (Russian)
|
| 51 |
-
if 0x0400 <= ord(char) <= 0x04FF: return False
|
| 52 |
-
return True
|
| 53 |
-
|
| 54 |
# --- מילון מונחים לחיפוש ---
|
| 55 |
SEARCH_MAP = {
|
| 56 |
"בקר": ["ESP32", "U2", "Controller"],
|
|
@@ -100,15 +86,34 @@ def expand_query(query: str) -> List[str]:
|
|
| 100 |
return list(search_terms)
|
| 101 |
|
| 102 |
def simplify_schematic_text(text: str) -> str:
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
text = text.replace("ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO", "בקר ESP32")
|
| 105 |
text = text.replace("BOB_19002", "ממיר רמות")
|
|
|
|
| 106 |
text = text.replace("TLM-SH_board", "הלוח הראשי")
|
| 107 |
text = text.replace("StudentBoard", "הלוח הראשי")
|
| 108 |
-
text = text.replace("
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
text = text.replace("connects to", "מחובר אל")
|
| 111 |
text = text.replace("Pin", "פין")
|
|
|
|
| 112 |
return text
|
| 113 |
|
| 114 |
def query_knowledge_base(query: str) -> str:
|
|
@@ -120,6 +125,7 @@ def query_knowledge_base(query: str) -> str:
|
|
| 120 |
results = []
|
| 121 |
seen_ids = set()
|
| 122 |
|
|
|
|
| 123 |
for term in search_terms:
|
| 124 |
cursor.execute("SELECT id, content, topic FROM knowledge WHERE content LIKE ? OR topic LIKE ?", (f'%{term}%', f'%{term}%'))
|
| 125 |
rows = cursor.fetchall()
|
|
@@ -130,10 +136,7 @@ def query_knowledge_base(query: str) -> str:
|
|
| 130 |
score += 100
|
| 131 |
elif term.lower() in topic.lower():
|
| 132 |
score += 10
|
| 133 |
-
|
| 134 |
-
for num in numbers_in_query:
|
| 135 |
-
if str(num) in content:
|
| 136 |
-
score += 50
|
| 137 |
results.append((score, content))
|
| 138 |
seen_ids.add(row_id)
|
| 139 |
conn.close()
|
|
@@ -142,20 +145,30 @@ def query_knowledge_base(query: str) -> str:
|
|
| 142 |
|
| 143 |
if results:
|
| 144 |
raw_content = results[0][1]
|
|
|
|
|
|
|
| 145 |
if "Schematic" in raw_content:
|
| 146 |
lines = raw_content.split('\n')
|
| 147 |
relevant_lines = []
|
| 148 |
target_terms = [t.lower() for t in search_terms if len(t) > 1]
|
|
|
|
| 149 |
for line in lines:
|
| 150 |
-
#
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
relevant_lines.append(
|
| 154 |
|
|
|
|
| 155 |
if relevant_lines:
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
return raw_content
|
|
|
|
| 159 |
return ""
|
| 160 |
|
| 161 |
def save_message(role: str, content: str):
|
|
@@ -174,18 +187,15 @@ class Message(BaseModel):
|
|
| 174 |
class ChatCompletionRequest(BaseModel):
|
| 175 |
messages: List[Message]
|
| 176 |
max_tokens: Optional[int] = 512
|
| 177 |
-
temperature: Optional[float] = 0.3
|
| 178 |
stream: Optional[bool] = False
|
| 179 |
|
| 180 |
@app.on_event("startup")
|
| 181 |
async def startup_event():
|
| 182 |
global model, tokenizer, device
|
| 183 |
init_db()
|
| 184 |
-
|
| 185 |
-
# UPGRADE LOGIC
|
| 186 |
-
logger.info(f"Loading UPGRADED model: {MODEL_ID}...")
|
| 187 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 188 |
-
|
| 189 |
try:
|
| 190 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 191 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -195,10 +205,10 @@ async def startup_event():
|
|
| 195 |
low_cpu_mem_usage=True,
|
| 196 |
trust_remote_code=True
|
| 197 |
)
|
| 198 |
-
logger.info("Model loaded
|
| 199 |
except Exception as e:
|
| 200 |
logger.error(f"Load failed: {e}")
|
| 201 |
-
raise RuntimeError("Model failed
|
| 202 |
|
| 203 |
def run_generation(model, tokenizer, params):
|
| 204 |
try:
|
|
@@ -217,18 +227,18 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 217 |
save_message("user", last_user_msg.content)
|
| 218 |
context = query_knowledge_base(last_user_msg.content)
|
| 219 |
|
|
|
|
| 220 |
if context:
|
| 221 |
sys_msg = (
|
| 222 |
-
"
|
| 223 |
-
"ה
|
| 224 |
-
"
|
| 225 |
-
"
|
| 226 |
-
"
|
| 227 |
-
"
|
| 228 |
-
f"\nמידע טכני:\n{context}"
|
| 229 |
)
|
| 230 |
else:
|
| 231 |
-
sys_msg = "אתה עוזר חכם לפרויקט תלם. ענה בעברית
|
| 232 |
|
| 233 |
if request.messages[0].role == "system":
|
| 234 |
request.messages[0].content = sys_msg
|
|
@@ -247,7 +257,7 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 247 |
top_p=0.9,
|
| 248 |
do_sample=True,
|
| 249 |
pad_token_id=tokenizer.eos_token_id,
|
| 250 |
-
repetition_penalty=1.
|
| 251 |
)
|
| 252 |
|
| 253 |
if request.stream:
|
|
@@ -259,8 +269,7 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 259 |
async def stream_generator():
|
| 260 |
full_text = ""
|
| 261 |
for text in streamer:
|
| 262 |
-
|
| 263 |
-
if text and is_valid_text(text):
|
| 264 |
full_text += text
|
| 265 |
yield f"data: {json.dumps({'choices': [{'delta': {'content': text}}]}, ensure_ascii=False)}\n\n"
|
| 266 |
save_message("assistant", full_text)
|
|
|
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
| 16 |
# --- Configuration ---
|
| 17 |
+
MODEL_ID = "Qwen/Qwen2.5-3B-Instruct" # Using the smarter 3B model
|
|
|
|
| 18 |
PORT = int(os.environ.get("PORT", 8000))
|
| 19 |
HOST = "0.0.0.0"
|
| 20 |
DB_FILE = "chatbot.db"
|
|
|
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
+
app = FastAPI(title="Hebrew Smart Home Technician", version="13.0.0")
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 37 |
tokenizer = None
|
| 38 |
device = "cpu"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# --- מילון מונחים לחיפוש ---
|
| 41 |
SEARCH_MAP = {
|
| 42 |
"בקר": ["ESP32", "U2", "Controller"],
|
|
|
|
| 86 |
return list(search_terms)
|
| 87 |
|
| 88 |
def simplify_schematic_text(text: str) -> str:
|
| 89 |
+
"""
|
| 90 |
+
Cleans raw schematic lines into readable Hebrew.
|
| 91 |
+
Order matters to prevent double-replacement!
|
| 92 |
+
"""
|
| 93 |
+
# 1. Clean specific long component names first
|
| 94 |
text = text.replace("ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO", "בקר ESP32")
|
| 95 |
text = text.replace("BOB_19002", "ממיר רמות")
|
| 96 |
+
text = text.replace("06060106_070403", "ממיר רמות") # ID from your DB
|
| 97 |
text = text.replace("TLM-SH_board", "הלוח הראשי")
|
| 98 |
text = text.replace("StudentBoard", "הלוח הראשי")
|
| 99 |
+
text = text.replace("040709_0804060105", "הלוח הראשי") # ID from your DB
|
| 100 |
+
|
| 101 |
+
# 2. Clean Generic names
|
| 102 |
+
# Check if we haven't already replaced it (simple check)
|
| 103 |
+
if "NEXTION" in text:
|
| 104 |
+
text = text.replace("LCD1 (NEXTION)", "מסך המגע")
|
| 105 |
+
text = text.replace("HMI (NEXTION)", "מסך המגע")
|
| 106 |
+
text = text.replace("NEXTION", "מסך") # Fallback
|
| 107 |
+
|
| 108 |
+
text = text.replace("LCD1", "מסך")
|
| 109 |
+
text = text.replace("U2", "הבקר")
|
| 110 |
+
text = text.replace("U3", "הלוח")
|
| 111 |
+
text = text.replace("U4", "הממיר")
|
| 112 |
+
|
| 113 |
+
# 3. Clean Verbs
|
| 114 |
text = text.replace("connects to", "מחובר אל")
|
| 115 |
text = text.replace("Pin", "פין")
|
| 116 |
+
|
| 117 |
return text
|
| 118 |
|
| 119 |
def query_knowledge_base(query: str) -> str:
|
|
|
|
| 125 |
results = []
|
| 126 |
seen_ids = set()
|
| 127 |
|
| 128 |
+
# Run Search
|
| 129 |
for term in search_terms:
|
| 130 |
cursor.execute("SELECT id, content, topic FROM knowledge WHERE content LIKE ? OR topic LIKE ?", (f'%{term}%', f'%{term}%'))
|
| 131 |
rows = cursor.fetchall()
|
|
|
|
| 136 |
score += 100
|
| 137 |
elif term.lower() in topic.lower():
|
| 138 |
score += 10
|
| 139 |
+
|
|
|
|
|
|
|
|
|
|
| 140 |
results.append((score, content))
|
| 141 |
seen_ids.add(row_id)
|
| 142 |
conn.close()
|
|
|
|
| 145 |
|
| 146 |
if results:
|
| 147 |
raw_content = results[0][1]
|
| 148 |
+
|
| 149 |
+
# SCHEMATIC PROCESSING
|
| 150 |
if "Schematic" in raw_content:
|
| 151 |
lines = raw_content.split('\n')
|
| 152 |
relevant_lines = []
|
| 153 |
target_terms = [t.lower() for t in search_terms if len(t) > 1]
|
| 154 |
+
|
| 155 |
for line in lines:
|
| 156 |
+
# Find lines that mention our search terms AND contain connection info
|
| 157 |
+
if "connect" in line.lower() and any(t in line.lower() for t in target_terms):
|
| 158 |
+
clean_line = simplify_schematic_text(line.strip())
|
| 159 |
+
relevant_lines.append(f"- {clean_line}")
|
| 160 |
|
| 161 |
+
# If we found specific lines, return ONLY them
|
| 162 |
if relevant_lines:
|
| 163 |
+
context_str = "רשימת החיבורים הרלוונטית שנמצאה:\n" + "\n".join(relevant_lines)
|
| 164 |
+
logger.info(f"✅ Found {len(relevant_lines)} specific schematic lines.")
|
| 165 |
+
return context_str
|
| 166 |
+
else:
|
| 167 |
+
# We found the schematic file but no specific lines for this component
|
| 168 |
+
return "נמצא קובץ שרטוט, אך לא נמצאו חיבורים ספציפיים לרכיב שביקשת."
|
| 169 |
|
| 170 |
return raw_content
|
| 171 |
+
|
| 172 |
return ""
|
| 173 |
|
| 174 |
def save_message(role: str, content: str):
|
|
|
|
| 187 |
class ChatCompletionRequest(BaseModel):
|
| 188 |
messages: List[Message]
|
| 189 |
max_tokens: Optional[int] = 512
|
| 190 |
+
temperature: Optional[float] = 0.3
|
| 191 |
stream: Optional[bool] = False
|
| 192 |
|
| 193 |
@app.on_event("startup")
|
| 194 |
async def startup_event():
|
| 195 |
global model, tokenizer, device
|
| 196 |
init_db()
|
| 197 |
+
logger.info(f"Loading model: {MODEL_ID}...")
|
|
|
|
|
|
|
| 198 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 199 |
try:
|
| 200 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 201 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 205 |
low_cpu_mem_usage=True,
|
| 206 |
trust_remote_code=True
|
| 207 |
)
|
| 208 |
+
logger.info("Model loaded!")
|
| 209 |
except Exception as e:
|
| 210 |
logger.error(f"Load failed: {e}")
|
| 211 |
+
raise RuntimeError("Model failed")
|
| 212 |
|
| 213 |
def run_generation(model, tokenizer, params):
|
| 214 |
try:
|
|
|
|
| 227 |
save_message("user", last_user_msg.content)
|
| 228 |
context = query_knowledge_base(last_user_msg.content)
|
| 229 |
|
| 230 |
+
# --- הנחיה ברורה וחד-משמעית ---
|
| 231 |
if context:
|
| 232 |
sys_msg = (
|
| 233 |
+
"אתה עוזר טכני לפרויקט 'בית חכם תלם'. ענה אך ורק בעברית.\n"
|
| 234 |
+
"השתמש במידע המצורף למטה כדי לענות על השאלה.\n"
|
| 235 |
+
"המידע מכיל רשימת חיבורים שתורגמה לעברית. קרא אותה וסכם אותה למשתמש במשפט ברור.\n"
|
| 236 |
+
"לדוגמה: 'המסך מחובר לבקר בפיני התקשורת RX ו-TX'.\n"
|
| 237 |
+
"אל תמציא מידע שאינו מופיע ברשימה.\n"
|
| 238 |
+
f"\nמידע מהמערכת (Context):\n{context}"
|
|
|
|
| 239 |
)
|
| 240 |
else:
|
| 241 |
+
sys_msg = "אתה עוזר חכם לפרויקט תלם. ענה בעברית. לא נמצא מידע ספציפי על השאלה במאגר."
|
| 242 |
|
| 243 |
if request.messages[0].role == "system":
|
| 244 |
request.messages[0].content = sys_msg
|
|
|
|
| 257 |
top_p=0.9,
|
| 258 |
do_sample=True,
|
| 259 |
pad_token_id=tokenizer.eos_token_id,
|
| 260 |
+
repetition_penalty=1.1
|
| 261 |
)
|
| 262 |
|
| 263 |
if request.stream:
|
|
|
|
| 269 |
async def stream_generator():
|
| 270 |
full_text = ""
|
| 271 |
for text in streamer:
|
| 272 |
+
if text:
|
|
|
|
| 273 |
full_text += text
|
| 274 |
yield f"data: {json.dumps({'choices': [{'delta': {'content': text}}]}, ensure_ascii=False)}\n\n"
|
| 275 |
save_message("assistant", full_text)
|