Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -14,11 +14,7 @@ from pydantic import BaseModel
|
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
| 16 |
# --- Configuration ---
|
| 17 |
-
|
| 18 |
-
MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
|
| 19 |
-
# OPTION 2: Switch back to 3B for 2x speed boost (Uncomment below line)
|
| 20 |
-
# MODEL_ID = "Qwen/Qwen2.5-3B-Instruct"
|
| 21 |
-
|
| 22 |
PORT = int(os.environ.get("PORT", 8000))
|
| 23 |
HOST = "0.0.0.0"
|
| 24 |
DB_FILE = "chatbot.db"
|
|
@@ -27,7 +23,7 @@ JSON_SOURCE = "database.json"
|
|
| 27 |
logging.basicConfig(level=logging.INFO)
|
| 28 |
logger = logging.getLogger(__name__)
|
| 29 |
|
| 30 |
-
app = FastAPI(title="Hebrew Smart Home Technician
|
| 31 |
|
| 32 |
app.add_middleware(
|
| 33 |
CORSMiddleware,
|
|
@@ -49,21 +45,9 @@ SEARCH_MAP = {
|
|
| 49 |
"חיבור": ["Connect", "Wire", "Signal"],
|
| 50 |
"תקלה": ["Error", "Problem", "Fix"],
|
| 51 |
"זרם": ["ACS712", "Sensor"],
|
| 52 |
-
"מתח": ["3.3V", "5V", "VIN", "Power"]
|
| 53 |
-
"פרויקט": ["Project", "Essence", "General", "מהות"], # Added for general queries
|
| 54 |
-
"בית חכם": ["Smart Home", "Project", "מהות"] # Added for general queries
|
| 55 |
}
|
| 56 |
|
| 57 |
-
# --- שומר שפה ---
|
| 58 |
-
def is_valid_text(text: str) -> bool:
|
| 59 |
-
for char in text:
|
| 60 |
-
code = ord(char)
|
| 61 |
-
if 0x4E00 <= code <= 0x9FFF: return False # Chinese
|
| 62 |
-
if 0x0400 <= code <= 0x04FF: return False # Russian
|
| 63 |
-
if 0x3040 <= code <= 0x30FF: return False # Japanese
|
| 64 |
-
if 0xAC00 <= code <= 0xD7AF: return False # Korean
|
| 65 |
-
return True
|
| 66 |
-
|
| 67 |
def init_db():
|
| 68 |
if os.path.exists(DB_FILE): os.remove(DB_FILE)
|
| 69 |
conn = sqlite3.connect(DB_FILE)
|
|
@@ -102,27 +86,27 @@ def expand_query(query: str) -> List[str]:
|
|
| 102 |
return list(search_terms)
|
| 103 |
|
| 104 |
def simplify_schematic_text(text: str) -> str:
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
text = text.replace("
|
| 111 |
-
text = text.replace("BOB_19002", "")
|
| 112 |
-
text = text.replace("
|
| 113 |
-
text = text.replace("
|
| 114 |
-
text = text.replace("
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
text = text.replace("()", "")
|
| 118 |
-
text = text.replace(" ", " ")
|
| 119 |
text = text.replace("connects to", "מחובר אל")
|
| 120 |
text = text.replace("Pin", "פין")
|
|
|
|
| 121 |
return text
|
| 122 |
|
| 123 |
-
def query_knowledge_base(query: str) ->
|
| 124 |
conn = sqlite3.connect(DB_FILE)
|
| 125 |
cursor = conn.cursor()
|
|
|
|
| 126 |
search_terms = expand_query(query)
|
| 127 |
logger.info(f"🔍 Searching for: {search_terms}")
|
| 128 |
|
|
@@ -130,23 +114,24 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 130 |
seen_ids = set()
|
| 131 |
|
| 132 |
for term in search_terms:
|
| 133 |
-
# Improved SQL query to better match topics as well
|
| 134 |
cursor.execute("SELECT id, content, topic FROM knowledge WHERE content LIKE ? OR topic LIKE ?", (f'%{term}%', f'%{term}%'))
|
| 135 |
rows = cursor.fetchall()
|
| 136 |
for row_id, content, topic in rows:
|
| 137 |
if row_id not in seen_ids:
|
| 138 |
score = 1
|
| 139 |
|
| 140 |
-
#
|
| 141 |
if "Schematic" in content and any(x in query for x in ["שרטוט", "חיבור", "פין", "לאן", "מחובר", "מסך"]):
|
| 142 |
score += 100
|
| 143 |
elif term.lower() in topic.lower():
|
| 144 |
-
score += 10
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
-
# Boost if topic matches general project questions
|
| 147 |
-
if any(x in topic for x in ["מהות הפרויקט", "רקע", "כללי"]) and any(q in query for q in ["מה זה", "הסבר", "פרויקט"]):
|
| 148 |
-
score += 50
|
| 149 |
-
|
| 150 |
results.append((score, content))
|
| 151 |
seen_ids.add(row_id)
|
| 152 |
conn.close()
|
|
@@ -155,25 +140,25 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 155 |
|
| 156 |
if results:
|
| 157 |
raw_content = results[0][1]
|
|
|
|
|
|
|
| 158 |
if "Schematic" in raw_content:
|
| 159 |
lines = raw_content.split('\n')
|
| 160 |
relevant_lines = []
|
| 161 |
target_terms = [t.lower() for t in search_terms if len(t) > 1]
|
| 162 |
|
| 163 |
for line in lines:
|
|
|
|
| 164 |
if "connect" in line.lower() and any(t in line.lower() for t in target_terms):
|
| 165 |
-
|
| 166 |
-
relevant_lines.append(f"- {clean_line}")
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
else:
|
| 172 |
-
return "נמצא שרטוט, אך לא נמצאו חיבורים ספציפיים לרכיב זה.", search_terms
|
| 173 |
|
| 174 |
-
return raw_content
|
| 175 |
-
|
| 176 |
-
return ""
|
| 177 |
|
| 178 |
def save_message(role: str, content: str):
|
| 179 |
try:
|
|
@@ -190,7 +175,7 @@ class Message(BaseModel):
|
|
| 190 |
|
| 191 |
class ChatCompletionRequest(BaseModel):
|
| 192 |
messages: List[Message]
|
| 193 |
-
max_tokens: Optional[int] =
|
| 194 |
temperature: Optional[float] = 0.2
|
| 195 |
stream: Optional[bool] = False
|
| 196 |
|
|
@@ -204,7 +189,7 @@ async def startup_event():
|
|
| 204 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 205 |
model = AutoModelForCausalLM.from_pretrained(
|
| 206 |
MODEL_ID,
|
| 207 |
-
torch_dtype=torch.float16 if device != "cpu" else
|
| 208 |
device_map=device,
|
| 209 |
low_cpu_mem_usage=True,
|
| 210 |
trust_remote_code=True
|
|
@@ -229,23 +214,20 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 229 |
context = ""
|
| 230 |
if last_user_msg:
|
| 231 |
save_message("user", last_user_msg.content)
|
| 232 |
-
context
|
| 233 |
|
| 234 |
if context:
|
| 235 |
sys_msg = (
|
| 236 |
-
"תפקידך:
|
| 237 |
"הנחיות:\n"
|
| 238 |
-
"1. דבר
|
| 239 |
-
"2. ה
|
| 240 |
-
"3. ע
|
| 241 |
-
|
|
|
|
| 242 |
)
|
| 243 |
else:
|
| 244 |
-
sys_msg =
|
| 245 |
-
"You are a Hebrew-speaking assistant. "
|
| 246 |
-
"If the user says 'Shalom', reply with 'שלום! איך אוכל לעזור?'. "
|
| 247 |
-
"Otherwise, strictly say you don't have that information in Hebrew."
|
| 248 |
-
)
|
| 249 |
|
| 250 |
if request.messages[0].role == "system":
|
| 251 |
request.messages[0].content = sys_msg
|
|
@@ -259,12 +241,12 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 259 |
|
| 260 |
gen_kwargs = dict(
|
| 261 |
inputs=prompt_ids,
|
| 262 |
-
max_new_tokens=request.max_tokens,
|
| 263 |
temperature=0.2,
|
| 264 |
top_p=0.9,
|
| 265 |
do_sample=True,
|
| 266 |
pad_token_id=tokenizer.eos_token_id,
|
| 267 |
-
repetition_penalty=1.1
|
| 268 |
)
|
| 269 |
|
| 270 |
if request.stream:
|
|
@@ -277,13 +259,8 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 277 |
full_text = ""
|
| 278 |
for text in streamer:
|
| 279 |
if text:
|
| 280 |
-
if not is_valid_text(text):
|
| 281 |
-
logger.warning(f"Blocked: {text}")
|
| 282 |
-
break
|
| 283 |
-
|
| 284 |
full_text += text
|
| 285 |
yield f"data: {json.dumps({'choices': [{'delta': {'content': text}}]}, ensure_ascii=False)}\n\n"
|
| 286 |
-
|
| 287 |
save_message("assistant", full_text)
|
| 288 |
yield "data: [DONE]\n\n"
|
| 289 |
|
|
|
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
| 16 |
# --- Configuration ---
|
| 17 |
+
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="10.0.0")
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 45 |
"חיבור": ["Connect", "Wire", "Signal"],
|
| 46 |
"תקלה": ["Error", "Problem", "Fix"],
|
| 47 |
"זרם": ["ACS712", "Sensor"],
|
| 48 |
+
"מתח": ["3.3V", "5V", "VIN", "Power"]
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def init_db():
|
| 52 |
if os.path.exists(DB_FILE): os.remove(DB_FILE)
|
| 53 |
conn = sqlite3.connect(DB_FILE)
|
|
|
|
| 86 |
return list(search_terms)
|
| 87 |
|
| 88 |
def simplify_schematic_text(text: str) -> str:
|
| 89 |
+
"""
|
| 90 |
+
פונקציה קריטית: מנקה את השמות הארוכים והמסובכים ומחליפה אותם בשמות פשוטים בעברית.
|
| 91 |
+
זה מקל מאוד על המודל להבין ולתרגם.
|
| 92 |
+
"""
|
| 93 |
+
# החלפת שמות רכיבים ארוכים בשמות קריאים
|
| 94 |
+
text = text.replace("ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO", "בקר (ESP32)")
|
| 95 |
+
text = text.replace("BOB_19002", "ממיר רמות (Level Shifter)")
|
| 96 |
+
text = text.replace("TLM-SH_board", "לוח ראשי")
|
| 97 |
+
text = text.replace("NEXTION", "מסך")
|
| 98 |
+
text = text.replace("LCD1", "מסך")
|
| 99 |
+
|
| 100 |
+
# פישוט פעלים
|
|
|
|
|
|
|
| 101 |
text = text.replace("connects to", "מחובר אל")
|
| 102 |
text = text.replace("Pin", "פין")
|
| 103 |
+
|
| 104 |
return text
|
| 105 |
|
| 106 |
+
def query_knowledge_base(query: str) -> str:
|
| 107 |
conn = sqlite3.connect(DB_FILE)
|
| 108 |
cursor = conn.cursor()
|
| 109 |
+
|
| 110 |
search_terms = expand_query(query)
|
| 111 |
logger.info(f"🔍 Searching for: {search_terms}")
|
| 112 |
|
|
|
|
| 114 |
seen_ids = set()
|
| 115 |
|
| 116 |
for term in search_terms:
|
|
|
|
| 117 |
cursor.execute("SELECT id, content, topic FROM knowledge WHERE content LIKE ? OR topic LIKE ?", (f'%{term}%', f'%{term}%'))
|
| 118 |
rows = cursor.fetchall()
|
| 119 |
for row_id, content, topic in rows:
|
| 120 |
if row_id not in seen_ids:
|
| 121 |
score = 1
|
| 122 |
|
| 123 |
+
# אם זו שאלה טכנית ויש מידע טכני
|
| 124 |
if "Schematic" in content and any(x in query for x in ["שרטוט", "חיבור", "פין", "לאן", "מחובר", "מסך"]):
|
| 125 |
score += 100
|
| 126 |
elif term.lower() in topic.lower():
|
| 127 |
+
score += 10
|
| 128 |
+
|
| 129 |
+
# חיפוש מספרים מדויק (כמו 18)
|
| 130 |
+
numbers_in_query = [int(s) for s in query.split() if s.isdigit()]
|
| 131 |
+
for num in numbers_in_query:
|
| 132 |
+
if str(num) in content:
|
| 133 |
+
score += 50
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
results.append((score, content))
|
| 136 |
seen_ids.add(row_id)
|
| 137 |
conn.close()
|
|
|
|
| 140 |
|
| 141 |
if results:
|
| 142 |
raw_content = results[0][1]
|
| 143 |
+
|
| 144 |
+
# אם מצאנו שרטוט, ננקה אותו ונחזיר רק שורות רלוונטיות
|
| 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 |
|
| 150 |
for line in lines:
|
| 151 |
+
# סינון שורות רלוונטיות בלבד
|
| 152 |
if "connect" in line.lower() and any(t in line.lower() for t in target_terms):
|
| 153 |
+
relevant_lines.append(line.strip())
|
|
|
|
| 154 |
|
| 155 |
+
# ניקוי הטקסט (החלפת אנגלית בעברית)
|
| 156 |
+
clean_text = simplify_schematic_text("\n".join(relevant_lines))
|
| 157 |
+
return f"להלן החיבורים שנמצאו:\n{clean_text}"
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
return raw_content
|
| 160 |
+
|
| 161 |
+
return ""
|
| 162 |
|
| 163 |
def save_message(role: str, content: str):
|
| 164 |
try:
|
|
|
|
| 175 |
|
| 176 |
class ChatCompletionRequest(BaseModel):
|
| 177 |
messages: List[Message]
|
| 178 |
+
max_tokens: Optional[int] = 512
|
| 179 |
temperature: Optional[float] = 0.2
|
| 180 |
stream: Optional[bool] = False
|
| 181 |
|
|
|
|
| 189 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 190 |
model = AutoModelForCausalLM.from_pretrained(
|
| 191 |
MODEL_ID,
|
| 192 |
+
torch_dtype=torch.float16 if device != "cpu" else torch.float32,
|
| 193 |
device_map=device,
|
| 194 |
low_cpu_mem_usage=True,
|
| 195 |
trust_remote_code=True
|
|
|
|
| 214 |
context = ""
|
| 215 |
if last_user_msg:
|
| 216 |
save_message("user", last_user_msg.content)
|
| 217 |
+
context = query_knowledge_base(last_user_msg.content)
|
| 218 |
|
| 219 |
if context:
|
| 220 |
sys_msg = (
|
| 221 |
+
"תפקידך: טכנאי מומחה לפרויקט 'בית חכם תלם'.\n"
|
| 222 |
"הנחיות:\n"
|
| 223 |
+
"1. דבר בעברית טבעית בלבד.\n"
|
| 224 |
+
"2. המידע שתקבל עבר עיבוד והוא מציג חיבורים (למשל: 'מסך פין RX מחובר אל...').\n"
|
| 225 |
+
"3. **אל תחזור על הטקסט כמו תוכי!** הפוך אותו לרשימה מסודרת או למשפט הסבר.\n"
|
| 226 |
+
"4. דוגמה: 'המסך מחובר לבקר בפיני התקשורת (RX/TX) וכן למתח 5V'.\n"
|
| 227 |
+
f"\nמידע מעובד מהמערכת:\n{context}"
|
| 228 |
)
|
| 229 |
else:
|
| 230 |
+
sys_msg = "אתה עוזר חכם לפרויקט תלם. ענה בעברית. אם אין לך מידע, אמור זאת."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
if request.messages[0].role == "system":
|
| 233 |
request.messages[0].content = sys_msg
|
|
|
|
| 241 |
|
| 242 |
gen_kwargs = dict(
|
| 243 |
inputs=prompt_ids,
|
| 244 |
+
max_new_tokens=request.max_tokens,
|
| 245 |
temperature=0.2,
|
| 246 |
top_p=0.9,
|
| 247 |
do_sample=True,
|
| 248 |
pad_token_id=tokenizer.eos_token_id,
|
| 249 |
+
repetition_penalty=1.1 # מאוזן
|
| 250 |
)
|
| 251 |
|
| 252 |
if request.stream:
|
|
|
|
| 259 |
full_text = ""
|
| 260 |
for text in streamer:
|
| 261 |
if text:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
full_text += text
|
| 263 |
yield f"data: {json.dumps({'choices': [{'delta': {'content': text}}]}, ensure_ascii=False)}\n\n"
|
|
|
|
| 264 |
save_message("assistant", full_text)
|
| 265 |
yield "data: [DONE]\n\n"
|
| 266 |
|