Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -14,7 +14,8 @@ from pydantic import BaseModel
|
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
| 16 |
# --- Configuration ---
|
| 17 |
-
|
|
|
|
| 18 |
PORT = int(os.environ.get("PORT", 8000))
|
| 19 |
HOST = "0.0.0.0"
|
| 20 |
DB_FILE = "chatbot.db"
|
|
@@ -23,7 +24,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="
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
@@ -100,7 +101,6 @@ def simplify_schematic_text(text: str) -> str:
|
|
| 100 |
text = text.replace("ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO", "בקר ESP32")
|
| 101 |
|
| 102 |
# Specific component mappings based on your schematic
|
| 103 |
-
# We map the ID to a readable name but KEEP the ID for reference
|
| 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)
|
|
@@ -167,8 +167,8 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 167 |
relevant_lines.append(f"- {clean_line}")
|
| 168 |
|
| 169 |
if relevant_lines:
|
| 170 |
-
#
|
| 171 |
-
context_str = "רשימת החיבורים שנמצאה:\n" + "\n".join(relevant_lines
|
| 172 |
return context_str, search_terms
|
| 173 |
else:
|
| 174 |
return "נמצא שרטוט, אך לא נמצאו חיבורים ספציפיים לרכיב זה.", search_terms
|
|
@@ -200,13 +200,14 @@ class ChatCompletionRequest(BaseModel):
|
|
| 200 |
async def startup_event():
|
| 201 |
global model, tokenizer, device
|
| 202 |
init_db()
|
| 203 |
-
logger.info(f"Loading model: {MODEL_ID}...")
|
| 204 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 205 |
try:
|
| 206 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
|
|
|
| 207 |
model = AutoModelForCausalLM.from_pretrained(
|
| 208 |
MODEL_ID,
|
| 209 |
-
torch_dtype=torch.float16 if device != "cpu" else
|
| 210 |
device_map=device,
|
| 211 |
low_cpu_mem_usage=True,
|
| 212 |
trust_remote_code=True
|
|
@@ -234,7 +235,6 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 234 |
context, _ = query_knowledge_base(last_user_msg.content)
|
| 235 |
|
| 236 |
if context:
|
| 237 |
-
# הנחיית מערכת מעודכנת לדיוק טכני
|
| 238 |
sys_msg = (
|
| 239 |
"תפקידך: אתה טכנאי מומחה המסביר על חיבורים במערכת 'בית חכם'.\n"
|
| 240 |
"הנחיות:\n"
|
|
@@ -265,7 +265,7 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 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,
|
|
@@ -281,8 +281,8 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 281 |
async def stream_generator():
|
| 282 |
full_text = ""
|
| 283 |
for text in streamer:
|
| 284 |
-
# הגנה בזמן אמת
|
| 285 |
if text:
|
|
|
|
| 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 |
+
# UPGRADE: Switching to 7B model (fits in 16GB RAM easily)
|
| 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 |
logging.basicConfig(level=logging.INFO)
|
| 25 |
logger = logging.getLogger(__name__)
|
| 26 |
|
| 27 |
+
app = FastAPI(title="Hebrew Smart Home Technician (7B)", version="14.0.0")
|
| 28 |
|
| 29 |
app.add_middleware(
|
| 30 |
CORSMiddleware,
|
|
|
|
| 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)
|
|
|
|
| 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:
|
| 174 |
return "נמצא שרטוט, אך לא נמצאו חיבורים ספציפיים לרכיב זה.", search_terms
|
|
|
|
| 200 |
async def startup_event():
|
| 201 |
global model, tokenizer, device
|
| 202 |
init_db()
|
| 203 |
+
logger.info(f"Loading UPGRADED model: {MODEL_ID}...")
|
| 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 if possible, otherwise standard. 7B fits in 16GB easily in float16.
|
| 208 |
model = AutoModelForCausalLM.from_pretrained(
|
| 209 |
MODEL_ID,
|
| 210 |
+
torch_dtype=torch.float16 if device != "cpu" else "auto",
|
| 211 |
device_map=device,
|
| 212 |
low_cpu_mem_usage=True,
|
| 213 |
trust_remote_code=True
|
|
|
|
| 235 |
context, _ = query_knowledge_base(last_user_msg.content)
|
| 236 |
|
| 237 |
if context:
|
|
|
|
| 238 |
sys_msg = (
|
| 239 |
"תפקידך: אתה טכנאי מומחה המסביר על חיבורים במערכת 'בית חכם'.\n"
|
| 240 |
"הנחיות:\n"
|
|
|
|
| 265 |
gen_kwargs = dict(
|
| 266 |
inputs=prompt_ids,
|
| 267 |
max_new_tokens=request.max_tokens,
|
| 268 |
+
temperature=0.3, # Slightly higher for 7B as it's more stable
|
| 269 |
top_p=0.9,
|
| 270 |
do_sample=True,
|
| 271 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
| 281 |
async def stream_generator():
|
| 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
|