mod
This commit is contained in:
parent
411b516955
commit
78cfc42316
|
|
@ -12,19 +12,15 @@ from sentence_transformers import SentenceTransformer
|
|||
# === KONFIGURACJA ===
|
||||
model_name = "hse.ably.do:latest" # Nazwa modelu Ollama
|
||||
faiss_index_path = "faiss_index.idx" # Plik indeksu FAISS
|
||||
kodeks_file = "./docs/kodekspracy.txt" # Plik z treścią kodeksu pracy
|
||||
kodeks_file = "/home/ably.do/docs/kodekspracy.txt" # Plik z treścią kodeksu pracy
|
||||
embedding_model = SentenceTransformer("all-MiniLM-L6-v2") # Model do embedowania tekstu
|
||||
|
||||
# === KROK 1: WCZYTYWANIE KODEKSU PRACY ===
|
||||
def load_kodeks(filepath):
|
||||
with open(filepath, 'r', encoding='utf-8') as file:
|
||||
with open(filepath, "r", encoding="utf-8") as file:
|
||||
content = file.read()
|
||||
articles = content.split('\n\n')
|
||||
documents = []
|
||||
for article in articles:
|
||||
if article.strip().startswith('Art.'):
|
||||
documents.append(article.strip())
|
||||
return documents
|
||||
articles = content.split("\n\n") # Dzielimy na sekcje
|
||||
return [article.strip() for article in articles if article.strip().startswith("Art.")]
|
||||
|
||||
# === KROK 2: TWORZENIE INDEKSU FAISS ===
|
||||
def create_faiss_index(sections):
|
||||
|
|
@ -36,30 +32,29 @@ def create_faiss_index(sections):
|
|||
|
||||
# === KROK 3: WYSZUKIWANIE NAJBLIŻSZEGO FRAGMENTU ===
|
||||
def search_faiss(query, index, sections):
|
||||
query_vector = embedding_model.encode([query], convert_to_numpy=True) # Wektor zapytania
|
||||
_, idx = index.search(query_vector, 1) # Szukamy 1 najbliższego sąsiada
|
||||
return sections[idx[0][0]] # Zwracamy najbardziej pasujący fragment kodeksu
|
||||
query_vector = embedding_model.encode([query], convert_to_numpy=True)
|
||||
_, idx = index.search(query_vector, 1)
|
||||
return sections[idx[0][0]] if idx[0][0] < len(sections) else "Nie znaleziono pasującego artykułu."
|
||||
|
||||
# === KROK 4: GENEROWANIE ODPOWIEDZI Z OLLAMA ===
|
||||
def generate_response(user_query):
|
||||
if not os.path.exists(faiss_index_path):
|
||||
return "Błąd: Indeks FAISS nie istnieje. Uruchom aplikację z opcją --rebuild-index, aby go wygenerować."
|
||||
return "Błąd: Indeks FAISS nie istnieje. Uruchom aplikację z opcją --rebuild-index."
|
||||
|
||||
try:
|
||||
index = faiss.read_index(faiss_index_path) # Wczytanie indeksu FAISS
|
||||
index = faiss.read_index(faiss_index_path)
|
||||
except Exception as e:
|
||||
return f"Błąd: Nie można załadować indeksu FAISS. Spróbuj odbudować go za pomocą --rebuild-index. Szczegóły: {str(e)}"
|
||||
return f"Błąd ładowania FAISS: {str(e)}"
|
||||
|
||||
sections = load_kodeks(kodeks_file) # Wczytanie kodeksu
|
||||
best_match = search_faiss(user_query, index, sections) # Znalezienie najbliższego fragmentu
|
||||
sections = load_kodeks(kodeks_file)
|
||||
best_match = search_faiss(user_query, index, sections)
|
||||
|
||||
# Kontekst dla Ollama
|
||||
prompt = f"Odpowiedz na pytanie na podstawie następującego tekstu:\n\n{best_match}\n\nPytanie: {user_query}"
|
||||
|
||||
response = ollama.chat(model=model_name, messages=[{"role": "user", "content": prompt}])
|
||||
return response.get("message", "Błąd: Nie udało się wygenerować odpowiedzi.")
|
||||
|
||||
# === KROK 5: URUCHOMIENIE INTERFEJSU WEBOWEGO ===
|
||||
return response.get("message", response.get("content", "Błąd: Nie udało się wygenerować odpowiedzi."))
|
||||
|
||||
# === KROK 5: INTERFEJS WEBOWY ===
|
||||
iface = gr.Interface(
|
||||
fn=generate_response,
|
||||
inputs=gr.Textbox(label="Zadaj pytanie o kodeks pracy"),
|
||||
|
|
@ -70,7 +65,7 @@ iface = gr.Interface(
|
|||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--rebuild-index", action="store_true", help="Wymuś odbudowanie indeksu FAISS")
|
||||
parser.add_argument("--rebuild-index", action="store_true", help="Odbudowanie indeksu FAISS")
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.rebuild_index or not os.path.exists(faiss_index_path):
|
||||
|
|
|
|||
Loading…
Reference in New Issue