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.0.
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
@@ -98,31 +98,25 @@ def expand_query(query: str) -> List[str]:
|
|
| 98 |
def filter_schematic_context(full_context: str, query_terms: List[str]) -> str:
|
| 99 |
"""
|
| 100 |
מסנן את השרטוט: משאיר רק שורות שרלוונטיות לשאלה.
|
| 101 |
-
זה מונע מהמודל להתבלבל מעודף מידע.
|
| 102 |
"""
|
| 103 |
lines = full_context.split('\n')
|
| 104 |
relevant_lines = []
|
| 105 |
|
| 106 |
-
# מילים שחייבות להופיע תמיד אם זה שרטוט (כותרות וכו')
|
| 107 |
header_terms = ["Schematic Connections", "connects to"]
|
| 108 |
-
|
| 109 |
-
# זיהוי אם המשתמש שאל על רכיב ספציפי
|
| 110 |
-
target_terms = [t.lower() for t in query_terms if len(t) > 1] # מונחים מהשאלה
|
| 111 |
|
| 112 |
for line in lines:
|
| 113 |
line_lower = line.lower()
|
| 114 |
|
| 115 |
-
# תמיד לשמור כותרות
|
| 116 |
if any(h.lower() in line_lower for h in header_terms) and len(line) < 50:
|
| 117 |
relevant_lines.append(line)
|
| 118 |
continue
|
| 119 |
|
| 120 |
-
# אם השורה מכילה אחד ממונחי החיפוש (למשל "LCD1" או "18")
|
| 121 |
if any(t in line_lower for t in target_terms):
|
| 122 |
relevant_lines.append(line)
|
| 123 |
|
| 124 |
if not relevant_lines:
|
| 125 |
-
return full_context
|
| 126 |
|
| 127 |
return "\n".join(relevant_lines)
|
| 128 |
|
|
@@ -143,19 +137,20 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 143 |
if row_id not in seen_ids:
|
| 144 |
score = 1
|
| 145 |
|
|
|
|
| 146 |
is_schematic = "Schematic" in content or "Connections" in content
|
| 147 |
is_tech_query = any(x in query for x in ["שרטוט", "חיבור", "פין", "לאן", "מחובר"])
|
| 148 |
|
| 149 |
if is_schematic and is_tech_query:
|
| 150 |
score += 100
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
| 152 |
numbers_in_query = [int(s) for s in query.split() if s.isdigit()]
|
| 153 |
for num in numbers_in_query:
|
| 154 |
if str(num) in content:
|
| 155 |
score += 50
|
| 156 |
-
|
| 157 |
-
elif term.lower() in topic.lower():
|
| 158 |
-
score += 10
|
| 159 |
|
| 160 |
results.append((score, content))
|
| 161 |
seen_ids.add(row_id)
|
|
@@ -166,7 +161,7 @@ def query_knowledge_base(query: str) -> Tuple[str, List[str]]:
|
|
| 166 |
|
| 167 |
if results:
|
| 168 |
best_content = results[0][1]
|
| 169 |
-
#
|
| 170 |
if "Schematic" in best_content:
|
| 171 |
filtered = filter_schematic_context(best_content, search_terms)
|
| 172 |
logger.info(f"📉 Filtered context from {len(best_content)} to {len(filtered)} chars")
|
|
@@ -229,7 +224,6 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 229 |
context = ""
|
| 230 |
if last_user_msg:
|
| 231 |
save_message("user", last_user_msg.content)
|
| 232 |
-
# קבלת הקשר מסונן ונקי
|
| 233 |
context, _ = query_knowledge_base(last_user_msg.content)
|
| 234 |
|
| 235 |
if context:
|
|
@@ -262,7 +256,7 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 262 |
top_p=0.9,
|
| 263 |
do_sample=True,
|
| 264 |
pad_token_id=tokenizer.eos_token_id,
|
| 265 |
-
repetition_penalty=1.0
|
| 266 |
)
|
| 267 |
|
| 268 |
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.0.1")
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 98 |
def filter_schematic_context(full_context: str, query_terms: List[str]) -> str:
|
| 99 |
"""
|
| 100 |
מסנן את השרטוט: משאיר רק שורות שרלוונטיות לשאלה.
|
|
|
|
| 101 |
"""
|
| 102 |
lines = full_context.split('\n')
|
| 103 |
relevant_lines = []
|
| 104 |
|
|
|
|
| 105 |
header_terms = ["Schematic Connections", "connects to"]
|
| 106 |
+
target_terms = [t.lower() for t in query_terms if len(t) > 1]
|
|
|
|
|
|
|
| 107 |
|
| 108 |
for line in lines:
|
| 109 |
line_lower = line.lower()
|
| 110 |
|
|
|
|
| 111 |
if any(h.lower() in line_lower for h in header_terms) and len(line) < 50:
|
| 112 |
relevant_lines.append(line)
|
| 113 |
continue
|
| 114 |
|
|
|
|
| 115 |
if any(t in line_lower for t in target_terms):
|
| 116 |
relevant_lines.append(line)
|
| 117 |
|
| 118 |
if not relevant_lines:
|
| 119 |
+
return full_context
|
| 120 |
|
| 121 |
return "\n".join(relevant_lines)
|
| 122 |
|
|
|
|
| 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 |
|
| 144 |
if is_schematic and is_tech_query:
|
| 145 |
score += 100
|
| 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:
|
| 153 |
score += 50
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
results.append((score, content))
|
| 156 |
seen_ids.add(row_id)
|
|
|
|
| 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")
|
|
|
|
| 224 |
context = ""
|
| 225 |
if last_user_msg:
|
| 226 |
save_message("user", last_user_msg.content)
|
|
|
|
| 227 |
context, _ = query_knowledge_base(last_user_msg.content)
|
| 228 |
|
| 229 |
if context:
|
|
|
|
| 256 |
top_p=0.9,
|
| 257 |
do_sample=True,
|
| 258 |
pad_token_id=tokenizer.eos_token_id,
|
| 259 |
+
repetition_penalty=1.0
|
| 260 |
)
|
| 261 |
|
| 262 |
if request.stream:
|