11import sys
22import os
3+ import sys
4+ import os
35
46sys .path .append ("../" )
57
@@ -68,9 +70,16 @@ async def greet(user_input: UserInput):
6870 if not user_input .text :
6971 raise HTTPException (status_code = 400 , detail = "Text cannot be empty" )
7072
71- results = model .find_answer (user_question = user_input .text , answer_embs = answer_embs , answers_df = data )
73+ results , scores = model .find_answer (user_question = 'query: ' + user_input .text , answer_embs = answer_embs , answers_df = data )
74+
75+ if len (scores ) > 0 :
76+ max_score = max (scores )
77+ if max_score < 0.85 :
78+ return {"message" : "Уточните, пожалуйста, ваш вопрос." }
79+
7280 return {"results" : results }
7381
82+
7483@app .get ("/" , response_class = HTMLResponse )
7584async def read_root ():
7685 return f"""
@@ -228,6 +237,31 @@ async def read_root():
228237 0% {{ transform: rotate(0deg); }}
229238 100% {{ transform: rotate(360deg); }}
230239 }}
240+
241+ /* Добавленные стили для сообщений */
242+ .warning-message {{
243+ padding: 1rem;
244+ margin: 1rem 0;
245+ border-radius: 8px;
246+ background: #fff3e0;
247+ color: #e65100;
248+ border-left: 4px solid #f57c00;
249+ display: flex;
250+ align-items: center;
251+ gap: 0.5rem;
252+ }}
253+
254+ .info-message {{
255+ padding: 1rem;
256+ margin: 1rem 0;
257+ border-radius: 8px;
258+ background: #e3f2fd;
259+ color: #1976d2;
260+ border-left: 4px solid #1976d2;
261+ display: flex;
262+ align-items: center;
263+ gap: 0.5rem;
264+ }}
231265 </style>
232266 </head>
233267 <body>
@@ -257,7 +291,14 @@ async def read_root():
257291 </div>
258292
259293 <div class="loader" id="loader"></div>
260- <div id="response"></div>
294+ <div id="response">
295+ <div class="info-message">
296+ <svg style="width:24px;height:24px" viewBox="0 0 24 24">
297+ <path fill="currentColor" d="M11,9H13V7H11M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M11,17H13V11H11V17Z" />
298+ </svg>
299+ <div>Задайте вопрос в поле выше, и я постараюсь найти ответ</div>
300+ </div>
301+ </div>
261302 </div>
262303
263304 <script>
@@ -280,6 +321,19 @@ async def read_root():
280321 const responseDiv = document.getElementById('response');
281322 const loader = document.getElementById('loader');
282323
324+ // Проверка пустого ввода
325+ if (!userInput.trim()) {{
326+ responseDiv.innerHTML = `
327+ <div class="warning-message">
328+ <svg style="width:24px;height:24px" viewBox="0 0 24 24">
329+ <path fill="currentColor" d="M11 15H13V17H11V15M11 7H13V13H11V7M12 2C6.47 2 2 6.5 2 12A10 10 0 0 0 12 22A10 10 0 0 0 22 12A10 10 0 0 0 12 2M12 20A8 8 0 0 1 4 12A8 8 0 0 1 12 4A8 8 0 0 1 20 12A8 8 0 0 1 12 20Z" />
330+ </svg>
331+ <div>Пожалуйста, введите ваш вопрос</div>
332+ </div>
333+ `;
334+ return;
335+ }}
336+
283337 responseDiv.innerHTML = '';
284338 loader.style.display = 'block';
285339
@@ -293,7 +347,19 @@ async def read_root():
293347 const data = await response.json();
294348 loader.style.display = 'none';
295349
296- if (data.results) {{
350+ // Обработка случая, когда максимальный score < 0.8
351+ if (data.message) {{
352+ responseDiv.innerHTML = `
353+ <div class="warning-message">
354+ <svg style="width:24px;height:24px" viewBox="0 0 24 24">
355+ <path fill="currentColor" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" />
356+ </svg>
357+ <div>${{escapeHtml(data.message)}}</div>
358+ </div>
359+ `;
360+ }}
361+ // Обработка результатов
362+ else if (data.results) {{
297363 data.results.forEach((result, index) => {{
298364 const topicDiv = document.createElement('div');
299365 topicDiv.className = 'topic';
@@ -315,12 +381,17 @@ async def read_root():
315381 responseDiv.appendChild(topicDiv);
316382 responseDiv.appendChild(fullTextDiv);
317383 }});
318- }} else if (data.message) {{
319- responseDiv.textContent = data.message;
320384 }}
321385 }} catch (error) {{
322386 loader.style.display = 'none';
323- responseDiv.textContent = 'Ошибка: ' + error.message;
387+ responseDiv.innerHTML = `
388+ <div class="warning-message">
389+ <svg style="width:24px;height:24px" viewBox="0 0 24 24">
390+ <path fill="currentColor" d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" />
391+ </svg>
392+ <div>Произошла ошибка: ${{escapeHtml(error.message)}}</div>
393+ </div>
394+ `;
324395 }}
325396 }}
326397
@@ -329,7 +400,14 @@ async def read_root():
329400
330401 document.getElementById('clearInput').addEventListener('click', () => {{
331402 document.getElementById('userInput').value = '';
332- document.getElementById('response').innerHTML = '';
403+ document.getElementById('response').innerHTML = `
404+ <div class="info-message">
405+ <svg style="width:24px;height:24px" viewBox="0 0 24 24">
406+ <path fill="currentColor" d="M11,9H13V7H11M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M11,17H13V11H11V17Z" />
407+ </svg>
408+ <div>Задайте вопрос в поле выше, и я постараюсь найти ответ</div>
409+ </div>
410+ `;
333411 }});
334412
335413 // Обработка Enter
0 commit comments