Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,69 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import shutil
|
| 4 |
from llama_index.llms.openrouter import OpenRouter
|
| 5 |
from llama_index.core.llms import ChatMessage
|
| 6 |
-
from llama_index.readers.file import PDFReader
|
| 7 |
-
from llama_index.vector_stores.faiss import FaissVectorStore
|
| 8 |
-
from llama_index.core.ingestion import IngestionPipeline
|
| 9 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 10 |
-
import
|
| 11 |
-
from llama_index.core import Settings
|
| 12 |
-
from llama_index.core.readers import SimpleDirectoryReader
|
| 13 |
-
from llama_index.core import VectorStoreIndex
|
| 14 |
-
from llama_index.core import StorageContext, load_index_from_storage
|
| 15 |
|
| 16 |
import nest_asyncio
|
| 17 |
nest_asyncio.apply()
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
storage_context = StorageContext.from_defaults(persist_dir="./storage")
|
| 32 |
index = load_index_from_storage(storage_context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
query_engine = make_query_engine('intfloat/multilingual-e5-large-instruct', 1024)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
gr.Markdown("# Информационная система для формирования адаптационных мероприятий к климатическим рискам")
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
answer_output = gr.Textbox(label="Ответ", lines=10, interactive=False)
|
| 45 |
-
send_button = gr.Button("
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from llama_index.llms.openrouter import OpenRouter
|
| 4 |
from llama_index.core.llms import ChatMessage
|
|
|
|
|
|
|
|
|
|
| 5 |
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
| 6 |
+
from llama_index.core import Settings, StorageContext, load_index_from_storage
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
import nest_asyncio
|
| 9 |
nest_asyncio.apply()
|
| 10 |
|
| 11 |
+
# === Глобальная инициализация ===
|
| 12 |
+
embed_model = HuggingFaceEmbedding(model_name='intfloat/multilingual-e5-large-instruct')
|
| 13 |
+
Settings.embed_model = embed_model
|
| 14 |
+
Settings.llm = OpenRouter(
|
| 15 |
+
api_key="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",
|
| 16 |
+
model="deepseek/deepseek-chat-v3.1:free",
|
| 17 |
+
max_tokens=10000,
|
| 18 |
+
context_window=20000,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# === Функция получения ответа ===
|
| 22 |
+
def get_facts(system_prompt: str, user_question: str) -> str:
|
| 23 |
+
# 1. Загружаем индекс и делаем retrieval
|
| 24 |
storage_context = StorageContext.from_defaults(persist_dir="./storage")
|
| 25 |
index = load_index_from_storage(storage_context)
|
| 26 |
+
retriever = index.as_retriever(similarity_top_k=4)
|
| 27 |
+
nodes = retriever.retrieve(user_question) # ← только по пользовательскому вопросу!
|
| 28 |
+
|
| 29 |
+
# 2. Формируем контекст из найденных документов
|
| 30 |
+
context = "\n\n".join([node.get_content() for node in nodes])
|
| 31 |
|
| 32 |
+
# 3. Подставляем контекст ВНУТРЬ системного промпта
|
| 33 |
+
full_system_prompt = system_prompt + f"Контекст: {context}"
|
|
|
|
| 34 |
|
| 35 |
+
# 4. Отправляем в LLM
|
| 36 |
+
messages = [
|
| 37 |
+
ChatMessage(role="system", content=full_system_prompt),
|
| 38 |
+
ChatMessage(role="user", content=user_question)
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
response = Settings.llm.chat(messages)
|
| 42 |
+
return response.message.content
|
| 43 |
|
| 44 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 45 |
gr.Markdown("# Информационная система для формирования адаптационных мероприятий к климатическим рискам")
|
| 46 |
+
system_input = gr.Textbox(
|
| 47 |
+
label="Системный промпт (можно редактировать)",
|
| 48 |
+
lines=8,
|
| 49 |
+
max_lines = 8,
|
| 50 |
+
placeholder="Введите системный промпт"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
user_input = gr.Textbox(
|
| 54 |
+
label="Ваш запрос",
|
| 55 |
+
lines=5,
|
| 56 |
+
max_lines = 5,
|
| 57 |
+
placeholder="Введите свой вопрос..."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
answer_output = gr.Textbox(label="Ответ", lines=10, interactive=False)
|
| 61 |
+
send_button = gr.Button("Получить ответ")
|
| 62 |
+
|
| 63 |
+
send_button.click(
|
| 64 |
+
fn=get_facts,
|
| 65 |
+
inputs=[system_input, user_input],
|
| 66 |
+
outputs=answer_output
|
| 67 |
+
)
|
| 68 |
|
| 69 |
demo.launch()
|