|
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('vector_map.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=2) |
|
context='' |
|
for text, similarity in top_n_results: |
|
context=context+text |
|
jaczat=client.chat.completions.create( |
|
model='gpt-4o-mini', |
|
temperature=0.0, |
|
max_tokens=1024, |
|
messages=[ |
|
{'role': 'system', |
|
'content': 'Jeste艣 asystentk膮 koordynator贸w dost臋pno艣ci i masz na imi臋 Ally. Odpowiadasz rzetelnie i mi艂o na pytania zadawane przez u偶ytkownik贸w. Nie musisz odpowiada膰 na wulgarne i chamskie uwagi.'+context}, |
|
{'role': 'user', |
|
'content': query_text} |
|
] |
|
) |
|
return jaczat.choices[0].message.content |
|
demo=gr.ChatInterface( |
|
fn=szukaj, |
|
theme=gr.themes.Monochrome(), |
|
autofocus=True, |
|
title='Ally - asystentka koordynator贸w dost臋pno艣ci', |
|
description='Ally ch臋tnie odpowie na Twoje pytania i w膮tpliwo艣ci dotycz膮ce dost臋pno艣ci architektonicznej i zada艅 os贸b koordynuj膮cych dost臋pno艣膰 w organizacji. Poniewa偶 Ally jest oparta o model sztucznej inteligencji, to mo偶e si臋 myli膰, chocia偶 na pewnorzadziej, ni偶 cz艂owiek.', |
|
submit_btn='Zapytaj', |
|
clear_btn=None, |
|
retry_btn=None, |
|
undo_btn=None, |
|
show_progress='minimal', |
|
).launch() |