Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -14,8 +14,11 @@ from pydantic import BaseModel
|
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
| 16 |
# --- Configuration ---
|
| 17 |
-
#
|
| 18 |
-
MODEL_ID = "Qwen/Qwen2.5-7B-Instruct"
|
|
|
|
|
|
|
|
|
|
| 19 |
PORT = int(os.environ.get("PORT", 8000))
|
| 20 |
HOST = "0.0.0.0"
|
| 21 |
DB_FILE = "chatbot.db"
|
|
@@ -24,7 +27,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,
|
|
@@ -97,34 +100,22 @@ def expand_query(query: str) -> List[str]:
|
|
| 97 |
return list(search_terms)
|
| 98 |
|
| 99 |
def simplify_schematic_text(text: str) -> str:
|
| 100 |
-
# 1. ניקוי שמות רכיבים טכניים מה-DB החדש
|
| 101 |
text = text.replace("ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO", "בקר ESP32")
|
| 102 |
-
|
| 103 |
-
# Specific component mappings based on your schematic
|
| 104 |
text = re.sub(r"U2\b", "ESP32 (U2)", text)
|
| 105 |
text = re.sub(r"U4\b", "Level Shifter (U4)", text)
|
| 106 |
text = re.sub(r"U3\b", "Main Board (U3)", text)
|
| 107 |
text = re.sub(r"LCD1\b", "Nextion Screen (LCD1)", text)
|
| 108 |
-
|
| 109 |
-
# Clean up the raw database strings if they still exist
|
| 110 |
text = text.replace("LevelConvertor (BOB_19002)", "Level Shifter (U4)")
|
| 111 |
text = text.replace("BOB_19002", "")
|
| 112 |
text = text.replace("StudentBoard (0201080606)", "Main Board (U3)")
|
| 113 |
text = text.replace("0201080606", "")
|
| 114 |
text = text.replace("040709_0804060105", "")
|
| 115 |
-
|
| 116 |
-
# 2. Clean Generic names
|
| 117 |
text = text.replace("(NEXTION)", "")
|
| 118 |
text = text.replace("HMI", "")
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
text = text.replace("()", "") # Remove empty brackets
|
| 122 |
-
text = text.replace(" ", " ") # Remove double spaces
|
| 123 |
-
|
| 124 |
-
# 4. Translate verbs for the Hebrew sentence structure
|
| 125 |
text = text.replace("connects to", "מחובר אל")
|
| 126 |
text = text.replace("Pin", "פין")
|
| 127 |
-
|
| 128 |
return text
|
| 129 |
|
| 130 |
def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
@@ -146,7 +137,6 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 146 |
score += 100
|
| 147 |
elif term.lower() in topic.lower():
|
| 148 |
score += 10
|
| 149 |
-
|
| 150 |
results.append((score, content))
|
| 151 |
seen_ids.add(row_id)
|
| 152 |
conn.close()
|
|
@@ -155,7 +145,6 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 155 |
|
| 156 |
if results:
|
| 157 |
raw_content = results[0][1]
|
| 158 |
-
|
| 159 |
if "Schematic" in raw_content:
|
| 160 |
lines = raw_content.split('\n')
|
| 161 |
relevant_lines = []
|
|
@@ -167,7 +156,6 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 167 |
relevant_lines.append(f"- {clean_line}")
|
| 168 |
|
| 169 |
if relevant_lines:
|
| 170 |
-
# Limit to relevant lines only
|
| 171 |
context_str = "רשימת החיבורים שנמצאה:\n" + "\n".join(relevant_lines)
|
| 172 |
return context_str, search_terms
|
| 173 |
else:
|
|
@@ -192,7 +180,7 @@ class Message(BaseModel):
|
|
| 192 |
|
| 193 |
class ChatCompletionRequest(BaseModel):
|
| 194 |
messages: List[Message]
|
| 195 |
-
max_tokens: Optional[int] =
|
| 196 |
temperature: Optional[float] = 0.2
|
| 197 |
stream: Optional[bool] = False
|
| 198 |
|
|
@@ -200,11 +188,11 @@ class ChatCompletionRequest(BaseModel):
|
|
| 200 |
async def startup_event():
|
| 201 |
global model, tokenizer, device
|
| 202 |
init_db()
|
| 203 |
-
logger.info(f"Loading
|
| 204 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 205 |
try:
|
| 206 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 207 |
-
# Using float16
|
| 208 |
model = AutoModelForCausalLM.from_pretrained(
|
| 209 |
MODEL_ID,
|
| 210 |
torch_dtype=torch.float16 if device != "cpu" else "auto",
|
|
@@ -239,17 +227,15 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 239 |
"תפקידך: אתה טכנאי מומחה המסביר על חיבורים במערכת 'בית חכם'.\n"
|
| 240 |
"הנחיות:\n"
|
| 241 |
"1. דבר אך ורק בשפה העברית.\n"
|
| 242 |
-
"2. השתמש במידע המצורף כדי לענות.
|
| 243 |
-
"3.
|
| 244 |
-
"
|
| 245 |
-
"5. אל תמציא מידע. אם חסר פין, אל תנחש אותו.\n"
|
| 246 |
-
f"\nמידע טכני מהשרטוט:\n{context}"
|
| 247 |
)
|
| 248 |
else:
|
| 249 |
sys_msg = (
|
| 250 |
"You are a Hebrew-speaking assistant. "
|
| 251 |
"If the user says 'Shalom', reply with 'שלום! איך אוכל לעזור?'. "
|
| 252 |
-
"
|
| 253 |
)
|
| 254 |
|
| 255 |
if request.messages[0].role == "system":
|
|
@@ -264,8 +250,8 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 264 |
|
| 265 |
gen_kwargs = dict(
|
| 266 |
inputs=prompt_ids,
|
| 267 |
-
max_new_tokens=request.max_tokens,
|
| 268 |
-
temperature=0.
|
| 269 |
top_p=0.9,
|
| 270 |
do_sample=True,
|
| 271 |
pad_token_id=tokenizer.eos_token_id,
|
|
@@ -282,7 +268,6 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 282 |
full_text = ""
|
| 283 |
for text in streamer:
|
| 284 |
if text:
|
| 285 |
-
# Still keep the guard just in case, though 7B needs it less
|
| 286 |
if not is_valid_text(text):
|
| 287 |
logger.warning(f"Blocked: {text}")
|
| 288 |
break
|
|
|
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
| 16 |
# --- Configuration ---
|
| 17 |
+
# OPTION 1: Stick with 7B for smarts, but limit output length
|
| 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 |
logging.basicConfig(level=logging.INFO)
|
| 28 |
logger = logging.getLogger(__name__)
|
| 29 |
|
| 30 |
+
app = FastAPI(title="Hebrew Smart Home Technician (Optimized)", version="14.1.0")
|
| 31 |
|
| 32 |
app.add_middleware(
|
| 33 |
CORSMiddleware,
|
|
|
|
| 100 |
return list(search_terms)
|
| 101 |
|
| 102 |
def simplify_schematic_text(text: str) -> str:
|
|
|
|
| 103 |
text = text.replace("ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO", "בקר ESP32")
|
|
|
|
|
|
|
| 104 |
text = re.sub(r"U2\b", "ESP32 (U2)", text)
|
| 105 |
text = re.sub(r"U4\b", "Level Shifter (U4)", text)
|
| 106 |
text = re.sub(r"U3\b", "Main Board (U3)", text)
|
| 107 |
text = re.sub(r"LCD1\b", "Nextion Screen (LCD1)", text)
|
|
|
|
|
|
|
| 108 |
text = text.replace("LevelConvertor (BOB_19002)", "Level Shifter (U4)")
|
| 109 |
text = text.replace("BOB_19002", "")
|
| 110 |
text = text.replace("StudentBoard (0201080606)", "Main Board (U3)")
|
| 111 |
text = text.replace("0201080606", "")
|
| 112 |
text = text.replace("040709_0804060105", "")
|
|
|
|
|
|
|
| 113 |
text = text.replace("(NEXTION)", "")
|
| 114 |
text = text.replace("HMI", "")
|
| 115 |
+
text = text.replace("()", "")
|
| 116 |
+
text = text.replace(" ", " ")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
text = text.replace("connects to", "מחובר אל")
|
| 118 |
text = text.replace("Pin", "פין")
|
|
|
|
| 119 |
return text
|
| 120 |
|
| 121 |
def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
|
|
| 137 |
score += 100
|
| 138 |
elif term.lower() in topic.lower():
|
| 139 |
score += 10
|
|
|
|
| 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 |
if "Schematic" in raw_content:
|
| 149 |
lines = raw_content.split('\n')
|
| 150 |
relevant_lines = []
|
|
|
|
| 156 |
relevant_lines.append(f"- {clean_line}")
|
| 157 |
|
| 158 |
if relevant_lines:
|
|
|
|
| 159 |
context_str = "רשימת החיבורים שנמצאה:\n" + "\n".join(relevant_lines)
|
| 160 |
return context_str, search_terms
|
| 161 |
else:
|
|
|
|
| 180 |
|
| 181 |
class ChatCompletionRequest(BaseModel):
|
| 182 |
messages: List[Message]
|
| 183 |
+
max_tokens: Optional[int] = 256 # Reduced for speed!
|
| 184 |
temperature: Optional[float] = 0.2
|
| 185 |
stream: Optional[bool] = False
|
| 186 |
|
|
|
|
| 188 |
async def startup_event():
|
| 189 |
global model, tokenizer, device
|
| 190 |
init_db()
|
| 191 |
+
logger.info(f"Loading model: {MODEL_ID}...")
|
| 192 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 193 |
try:
|
| 194 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 195 |
+
# Using float16 for speed on compatible hardware, or auto
|
| 196 |
model = AutoModelForCausalLM.from_pretrained(
|
| 197 |
MODEL_ID,
|
| 198 |
torch_dtype=torch.float16 if device != "cpu" else "auto",
|
|
|
|
| 227 |
"תפקידך: אתה טכנאי מומחה המסביר על חיבורים במערכת 'בית חכם'.\n"
|
| 228 |
"הנחיות:\n"
|
| 229 |
"1. דבר אך ורק בשפה העברית.\n"
|
| 230 |
+
"2. השתמש במידע המצורף כדי לענות.\n"
|
| 231 |
+
"3. קצר ולעניין! כתוב משפט אחד או שניים בלבד.\n"
|
| 232 |
+
f"\nמידע טכני:\n{context}"
|
|
|
|
|
|
|
| 233 |
)
|
| 234 |
else:
|
| 235 |
sys_msg = (
|
| 236 |
"You are a Hebrew-speaking assistant. "
|
| 237 |
"If the user says 'Shalom', reply with 'שלום! איך אוכל לעזור?'. "
|
| 238 |
+
"Keep answers short."
|
| 239 |
)
|
| 240 |
|
| 241 |
if request.messages[0].role == "system":
|
|
|
|
| 250 |
|
| 251 |
gen_kwargs = dict(
|
| 252 |
inputs=prompt_ids,
|
| 253 |
+
max_new_tokens=request.max_tokens, # Using the reduced limit
|
| 254 |
+
temperature=0.2,
|
| 255 |
top_p=0.9,
|
| 256 |
do_sample=True,
|
| 257 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
| 268 |
full_text = ""
|
| 269 |
for text in streamer:
|
| 270 |
if text:
|
|
|
|
| 271 |
if not is_valid_text(text):
|
| 272 |
logger.warning(f"Blocked: {text}")
|
| 273 |
break
|