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="9.
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
@@ -137,7 +137,6 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 137 |
if row_id not in seen_ids:
|
| 138 |
score = 1
|
| 139 |
|
| 140 |
-
# 1. Check if it is schematic context
|
| 141 |
is_schematic = "Schematic" in content or "Connections" in content
|
| 142 |
is_tech_query = any(x in query for x in ["שרטוט", "חיבור", "פין", "לאן", "מחובר"])
|
| 143 |
|
|
@@ -146,7 +145,6 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 146 |
elif term.lower() in topic.lower():
|
| 147 |
score += 10
|
| 148 |
|
| 149 |
-
# 2. Check for number matches (Moved outside IF/ELIF chain to avoid SyntaxError)
|
| 150 |
numbers_in_query = [int(s) for s in query.split() if s.isdigit()]
|
| 151 |
for num in numbers_in_query:
|
| 152 |
if str(num) in content:
|
|
@@ -161,7 +159,6 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 161 |
|
| 162 |
if results:
|
| 163 |
best_content = results[0][1]
|
| 164 |
-
# Apply Smart Filter for Schematics
|
| 165 |
if "Schematic" in best_content:
|
| 166 |
filtered = filter_schematic_context(best_content, search_terms)
|
| 167 |
logger.info(f"📉 Filtered context from {len(best_content)} to {len(filtered)} chars")
|
|
@@ -186,7 +183,7 @@ class Message(BaseModel):
|
|
| 186 |
class ChatCompletionRequest(BaseModel):
|
| 187 |
messages: List[Message]
|
| 188 |
max_tokens: Optional[int] = 512
|
| 189 |
-
temperature: Optional[float] = 0.
|
| 190 |
stream: Optional[bool] = False
|
| 191 |
|
| 192 |
@app.on_event("startup")
|
|
@@ -230,14 +227,14 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 230 |
sys_msg = (
|
| 231 |
"תפקידך: טכנאי מומחה לפרויקט 'בית חכם תלם'.\n"
|
| 232 |
"הנחיות:\n"
|
| 233 |
-
"1. ענה אך ורק בעברית. תרגם
|
| 234 |
-
"2.
|
| 235 |
-
"3. אל תמציא מידע.
|
| 236 |
-
"4.
|
| 237 |
-
f"\nמידע
|
| 238 |
)
|
| 239 |
else:
|
| 240 |
-
sys_msg = "אתה עוזר חכם לפרויקט תלם. ענה בעברית.
|
| 241 |
|
| 242 |
if request.messages[0].role == "system":
|
| 243 |
request.messages[0].content = sys_msg
|
|
@@ -252,11 +249,11 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 252 |
gen_kwargs = dict(
|
| 253 |
inputs=prompt_ids,
|
| 254 |
max_new_tokens=request.max_tokens,
|
| 255 |
-
temperature=0.
|
| 256 |
top_p=0.9,
|
| 257 |
do_sample=True,
|
| 258 |
pad_token_id=tokenizer.eos_token_id,
|
| 259 |
-
repetition_penalty=1.
|
| 260 |
)
|
| 261 |
|
| 262 |
if request.stream:
|
|
|
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
+
app = FastAPI(title="Hebrew Smart Home Technician", version="9.1.0")
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 137 |
if row_id not in seen_ids:
|
| 138 |
score = 1
|
| 139 |
|
|
|
|
| 140 |
is_schematic = "Schematic" in content or "Connections" in content
|
| 141 |
is_tech_query = any(x in query for x in ["שרטוט", "חיבור", "פין", "לאן", "מחובר"])
|
| 142 |
|
|
|
|
| 145 |
elif term.lower() in topic.lower():
|
| 146 |
score += 10
|
| 147 |
|
|
|
|
| 148 |
numbers_in_query = [int(s) for s in query.split() if s.isdigit()]
|
| 149 |
for num in numbers_in_query:
|
| 150 |
if str(num) in content:
|
|
|
|
| 159 |
|
| 160 |
if results:
|
| 161 |
best_content = results[0][1]
|
|
|
|
| 162 |
if "Schematic" in best_content:
|
| 163 |
filtered = filter_schematic_context(best_content, search_terms)
|
| 164 |
logger.info(f"📉 Filtered context from {len(best_content)} to {len(filtered)} chars")
|
|
|
|
| 183 |
class ChatCompletionRequest(BaseModel):
|
| 184 |
messages: List[Message]
|
| 185 |
max_tokens: Optional[int] = 512
|
| 186 |
+
temperature: Optional[float] = 0.3
|
| 187 |
stream: Optional[bool] = False
|
| 188 |
|
| 189 |
@app.on_event("startup")
|
|
|
|
| 227 |
sys_msg = (
|
| 228 |
"תפקידך: טכנאי מומחה לפרויקט 'בית חכם תלם'.\n"
|
| 229 |
"הנחיות:\n"
|
| 230 |
+
"1. ענה אך ורק בעברית. תרגם מונחים טכניים (Connects -> מחובר).\n"
|
| 231 |
+
"2. אם נשאלת על חיבור רכיב, סכם את החיבורים במשפט אחד או שניים. אל תחזור על עצמך.\n"
|
| 232 |
+
"3. אל תמציא מידע. השתמש רק במידע המצורף.\n"
|
| 233 |
+
"4. אם מופיע מספר חיבורים, ציין את החשובים (RX, TX, 5V, GND).\n"
|
| 234 |
+
f"\nמידע טכני (Context):\n{context}"
|
| 235 |
)
|
| 236 |
else:
|
| 237 |
+
sys_msg = "אתה עוזר חכם לפרויקט תלם. ענה בעברית. אם אין מידע, ציין זאת."
|
| 238 |
|
| 239 |
if request.messages[0].role == "system":
|
| 240 |
request.messages[0].content = sys_msg
|
|
|
|
| 249 |
gen_kwargs = dict(
|
| 250 |
inputs=prompt_ids,
|
| 251 |
max_new_tokens=request.max_tokens,
|
| 252 |
+
temperature=0.3,
|
| 253 |
top_p=0.9,
|
| 254 |
do_sample=True,
|
| 255 |
pad_token_id=tokenizer.eos_token_id,
|
| 256 |
+
repetition_penalty=1.1
|
| 257 |
)
|
| 258 |
|
| 259 |
if request.stream:
|