Spaces:
Sleeping
Sleeping
File size: 12,366 Bytes
fcc01f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
🚀 БелУзор v0.1 для Hugging Face Spaces
Запуск:
python3 app.py
Адкрыць:
http://localhost:7860
"""
import gradio as gr
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from src.core.ultimate_engine_v1 import BelarusUltimateEngine
from src.core.llm_interface import LLMInterface
# Ініцыялізацыя
print("🔄 Ініцыялізацыя сістэмы...")
engine = BelarusUltimateEngine()
llm = LLMInterface()
conversation_history = []
def update_llm_settings(provider, model, custom_url, enable_llm):
"""Абнаўленне налад LLM"""
try:
if not enable_llm:
llm.disable()
return "✅ LLM адключаны"
if provider == "Custom URL":
if custom_url:
llm.enable_custom_endpoint(custom_url)
return f"✅ Custom endpoint: {custom_url}"
else:
return "⚠️ Увядзіце URL"
else:
llm.enable(provider.lower(), model)
return f"✅ LLM: {provider}/{model}"
except Exception as e:
return f"❌ Памылка: {e}"
def search_answer(question, provider, model, custom_url, enable_llm):
"""Пошук адказу з наладамі LLM"""
global conversation_history
if not question.strip():
return "❌ Увядзіце пытанне!"
# Абнаўленне налад LLM
if enable_llm:
if provider == "Custom URL":
if custom_url:
llm.enable_custom_endpoint(custom_url)
else:
llm.enable(provider.lower(), model)
# Пошук фактаў
facts = engine.answer(question, use_llm_ranking=enable_llm)
if not facts:
return "❌ На жаль, нічога не знойдзена."
# Калі LLM уключаны
if enable_llm and llm.enabled:
try:
# Даданне кантэксту
context = []
if conversation_history:
context = [f"{msg['role']}: {msg['content']}"
for msg in conversation_history[-6:]]
# Фарміраванне prompt
if context:
full_prompt = f"""Гісторыя размовы:
{chr(10).join(context)}
❓ Апошняе пытанне: {question}
📚 ФАКТЫ З БАЗЫ ВЕДАЎ:
{chr(10).join(f'{i+1}. {fact}' for i, fact in enumerate(facts))}
📝 Дай поўны разгорнуты адказ (3-5 сказаў мінімум) з улікам папярэдняй размовы."""
else:
full_prompt = llm._create_rag_prompt(question, facts)
llm_answer = llm.generate(full_prompt, context=None)
# Фарматаванне адказу
response = f"🤖 **Адказ LLM:**\n\n{llm_answer}\n\n"
response += "**📚 Крыніцы:**\n"
for i, fact in enumerate(facts[:5], 1):
response += f"{i}. {fact}\n"
# Захаванне ў гісторыю
conversation_history.append({
'role': 'user',
'content': question
})
conversation_history.append({
'role': 'assistant',
'content': llm_answer
})
# Абмежаванне гісторыі
if len(conversation_history) > 20:
conversation_history.pop(0)
return response
except Exception as e:
return f"⚠️ Памылка LLM: {e}\n\n📚 Адказ з базы:\n" + "\n".join(facts[:5])
# Без LLM
response = "**📚 Знойдзена фактаў:** " + str(len(facts)) + "\n\n"
for i, fact in enumerate(facts[:10], 1):
response += f"{i}. {fact}\n"
return response
def clear_history():
"""Ачыстка гісторыі"""
global conversation_history
conversation_history = []
return "✅ Гісторыя ачышчана"
def upload_book(file, parse_mode):
"""Загрузка кнігі ў базу"""
if file is None:
return "❌ Абярыце файл!"
try:
# Чытанне файла
with open(file.name, 'r', encoding='utf-8') as f:
book_text = f.read()
book_name = file.name.split('/')[-1].replace('.txt', '')
# Дадаванне кнігі
facts_count = engine.add_book(book_text, book_name, parse_mode.lower())
return f"✅ Кніга '{book_name}' дададзена!\n📊 Створана фактаў: {facts_count}"
except Exception as e:
return f"❌ Памылка: {e}"
def get_llm_status():
"""Атрыманне статусу LLM"""
status = llm.get_status()
if status['enabled']:
return f"✅ {status['provider']}/{status['model']}"
else:
return "❌ Адключаны"
# Інтэрфейс Gradio
with gr.Blocks(
title="БелУзор v0.1",
css="""
.gradio-container {
max-width: 1400px !important;
}
#banner {
text-align: center;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 10px;
margin-bottom: 20px;
}
#banner h1 {
font-size: 2.5em;
margin: 0;
}
#banner p {
font-size: 1.2em;
opacity: 0.9;
}
"""
) as demo:
# Банэр
gr.HTML("""
<div id="banner">
<h1>🤖 БелУзор v0.1</h1>
<p>Інтэлектуальная пошукавая сістэма па гісторыі Беларусі</p>
</div>
""")
with gr.Row():
with gr.Column(scale=3):
# Налады LLM
with gr.Accordion("⚙️ Налады LLM", open=False):
gr.Markdown("**Абярыце LLM provider для разгорнутых адказаў**")
provider_dropdown = gr.Dropdown(
choices=[
"Ollama",
"LM Studio",
"OpenRouter",
"Custom URL"
],
value="Ollama",
label="Provider"
)
model_input = gr.Textbox(
label="Мадэль",
placeholder="llama3.2:3b",
value="llama3.2:3b"
)
custom_url_input = gr.Textbox(
label="Custom URL",
placeholder="http://192.168.0.116:1234/v1",
visible=False
)
enable_llm_check = gr.Checkbox(
label="🤖 Выкарыстоўваць LLM",
value=False,
info="Для разгорнутых адказаў з кантэкстам"
)
llm_status_text = gr.Textbox(
label="Статус LLM",
value="❌ Адключаны",
interactive=False
)
# Кнопка прымянення налад
apply_llm_btn = gr.Button("✅ Ужыць налады LLM", variant="secondary")
# Поле пытання
question_input = gr.Textbox(
label="Пытанне",
placeholder="Увядзіце ваша пытанне па гісторыі Беларусі...",
lines=3,
scale=2
)
submit_btn = gr.Button("🔍 Пошук", variant="primary", scale=1)
# Адказ
output = gr.Textbox(
label="Адказ",
lines=10,
max_lines=20
)
# Кнопкі
with gr.Row():
clear_btn = gr.Button("🗑️ Ачысціць гісторыю", variant="secondary")
status_btn = gr.Button("📊 Статус LLM", variant="secondary")
with gr.Column(scale=1):
gr.Markdown("""
### 📝 Прыклады пытанняў:
- Год заснавання Мінска?
- Хто такі Кастусь Каліноўскі?
- Што такое Статут 1588 года?
- Грунвальдская бітва 1410 года
- Люблінская унія 1569 года
### 📊 Статыстыка:
- **Фактаў:** 100,484
- **Слоў:** 347,517
- **Кніг:** 19
- **Слоўнікаў:** 26 🅱️ Бэта
### ⚙️ Асаблівасці:
- ✅ Кэш лематызацыі (10x хутчэй)
- ✅ Кэш API (100x хутчэй)
- ✅ Кантэкст дыялогу
- ✅ Пошук крыніц у Google
""")
# Загрузка кніг
with gr.Accordion("📚 Дадаць кнігу", open=False):
gr.Markdown("**Загрузіце кнігу ў базу ведаў**")
book_file = gr.File(
label="Абярыце файл (.txt)",
file_types=[".txt"],
type="filepath"
)
parse_mode_radio = gr.Radio(
choices=["Па сэнтенцыях", "Па раздзелах"],
value="Па сэнтенцыях",
label="Рэжым парсінгу"
)
upload_btn = gr.Button("📚 Загрузіць кнігу", variant="primary")
upload_output = gr.Textbox(
label="Вынік",
lines=3,
max_lines=5
)
# Статус LLM
llm_info = gr.Markdown("**LLM:** ❌ Адключаны")
# Апрацоўчыкі
submit_btn.click(
fn=search_answer,
inputs=[
question_input,
provider_dropdown,
model_input,
custom_url_input,
enable_llm_check
],
outputs=output
)
clear_btn.click(
fn=clear_history,
outputs=output
)
# Загрузка кнігі
upload_btn.click(
fn=upload_book,
inputs=[book_file, parse_mode_radio],
outputs=upload_output
)
# Абнаўленне налад LLM
apply_llm_btn.click(
fn=update_llm_settings,
inputs=[
provider_dropdown,
model_input,
custom_url_input,
enable_llm_check
],
outputs=llm_status_text
)
# Паказ/утоенне Custom URL
def toggle_custom_url(provider):
return gr.update(visible=(provider == "Custom URL"))
provider_dropdown.change(
fn=toggle_custom_url,
inputs=provider_dropdown,
outputs=custom_url_input
)
# Статус LLM
status_btn.click(
fn=get_llm_status,
outputs=llm_info
)
# Гарачыя клавішы
question_input.submit(
fn=search_answer,
inputs=[
question_input,
provider_dropdown,
model_input,
custom_url_input,
enable_llm_check
],
outputs=output
)
if __name__ == "__main__":
print("✅ Сістэма гатова!")
print("🌐 Адкрыйце: http://localhost:7860")
print("⚙️ Налады LLM даступныя ў акне '⚙️ Налады LLM'")
demo.launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())
|