|
import pickle |
|
import numpy as np |
|
from sentence_transformers import SentenceTransformer |
|
from scipy.spatial.distance import cosine |
|
import gradio as gr |
|
from openai import OpenAI |
|
client=OpenAI() |
|
model = SentenceTransformer("quanthome/paraphrase-multilingual-MiniLM-L12-v2") |
|
|
|
def find_similar(text, vector_map, model, top_n=5): |
|
query_embedding = model.encode([text])[0] |
|
similarities = [] |
|
|
|
for key, embedding in vector_map.items(): |
|
similarity = 1 - cosine(query_embedding, embedding) |
|
similarities.append((key, similarity)) |
|
|
|
|
|
similarities = sorted(similarities, key=lambda x: x[1], reverse=True) |
|
|
|
return similarities[:top_n] |
|
|
|
|
|
|
|
with open('praca.pkl', 'rb') as f: |
|
vector_map = pickle.load(f) |
|
|
|
def szukaj(query_text, history): |
|
top_n_results = find_similar(query_text, vector_map, model, top_n=1) |
|
context='' |
|
for text, similarity in top_n_results: |
|
context=context+text |
|
agata=client.chat.completions.create( |
|
model='gpt-4o', |
|
temperature=0.1, |
|
max_tokens=1024, |
|
messages=[ |
|
{'role': 'system', |
|
'content': 'Nazywasz si臋 Agata Gawska i jeste艣 ekspertk膮 do spraw zatrudniania i aktywizacji zawodowej os贸b z niepe艂nosprawno艣ci膮. Odpowiadasz konkretnie na pytania.'+context}, |
|
{'role': 'user', |
|
'content': query_text} |
|
] |
|
) |
|
return agata.choices[0].message.content |
|
demo=gr.ChatInterface( |
|
fn=szukaj, |
|
theme=gr.themes.Glass(font='OpenSans'), |
|
title='Agata', |
|
description='Twoja doradczyni w zatrudnianiu os贸b z niepe艂nosprawno艣ciami.', |
|
submit_btn='Zapytaj', |
|
clear_btn='Wyczy艣膰', |
|
retry_btn=None, |
|
undo_btn=None, |
|
show_progress='minimal', |
|
).launch(show_api=False, inbrowser=True) |