File size: 1,968 Bytes
8e0cbbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e3d55a
d84527f
8e0cbbd
 
 
 
e744165
13086e6
9d3ab5d
8e0cbbd
 
4dd20d9
8e0cbbd
5e3d55a
8e0cbbd
 
 
5e3d55a
8e0cbbd
6f4b8b6
a6c561d
d84527f
5e3d55a
8e0cbbd
5e3d55a
 
 
8e0cbbd
63e9a36
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
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")
# Funkcja do znajdowania najbardziej podobnych tekstów
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)  # 1 - cosine distance gives similarity
        similarities.append((key, similarity))

    # Sortowanie po podobieństwie malejąco
    similarities = sorted(similarities, key=lambda x: x[1], reverse=True)
    
    return similarities[:top_n]


# Odczytanie słownika z pliku
with open('vector_map.pkl', 'rb') as f:
    vector_map = pickle.load(f)

# Przykładowe wyszukiwanie
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': 'Nazywasz się Jacek Zadrożny i jesteś ekspertem cyfrowej dostępności. Odpowiadasz zwięźle na pytania. Przed wysłaniem odpowiedzi sprawdzasz jej poprawność.'+context},
            {'role': 'user',
             'content': query_text}
        ]
    )
    return jaczat.choices[0].message.content
demo=gr.ChatInterface(
    fn=szukaj,
    theme=gr.themes.Monochrome(),
autofocus=True,
    title='Jacek',
    description='Tu możesz zapytać o wszystko dotyczące cyfrowej dostępności, w tym przede wszystkim WCAG.',
    submit_btn='Zapytaj',
    clear_btn=None,
    retry_btn=None,
    undo_btn=None,
    show_progress='minimal',
).launch()