Pierwsze wersje plików.
Browse files- app.py +55 -0
- praca.pkl +3 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import numpy as np
|
| 3 |
+
from sentence_transformers import SentenceTransformer
|
| 4 |
+
from scipy.spatial.distance import cosine
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
client=OpenAI()
|
| 8 |
+
model = SentenceTransformer("quanthome/paraphrase-multilingual-MiniLM-L12-v2")
|
| 9 |
+
# Funkcja do znajdowania najbardziej podobnych tekstów
|
| 10 |
+
def find_similar(text, vector_map, model, top_n=5):
|
| 11 |
+
query_embedding = model.encode([text])[0]
|
| 12 |
+
similarities = []
|
| 13 |
+
|
| 14 |
+
for key, embedding in vector_map.items():
|
| 15 |
+
similarity = 1 - cosine(query_embedding, embedding) # 1 - cosine distance gives similarity
|
| 16 |
+
similarities.append((key, similarity))
|
| 17 |
+
|
| 18 |
+
# Sortowanie po podobieństwie malejąco
|
| 19 |
+
similarities = sorted(similarities, key=lambda x: x[1], reverse=True)
|
| 20 |
+
|
| 21 |
+
return similarities[:top_n]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Odczytanie słownika z pliku
|
| 25 |
+
with open('praca.pkl', 'rb') as f:
|
| 26 |
+
vector_map = pickle.load(f)
|
| 27 |
+
|
| 28 |
+
def szukaj(query_text, history):
|
| 29 |
+
top_n_results = find_similar(query_text, vector_map, model, top_n=1)
|
| 30 |
+
context=''
|
| 31 |
+
for text, similarity in top_n_results:
|
| 32 |
+
context=context+text
|
| 33 |
+
agata=client.chat.completions.create(
|
| 34 |
+
model='gpt-4o',
|
| 35 |
+
temperature=0.1,
|
| 36 |
+
max_tokens=1024,
|
| 37 |
+
messages=[
|
| 38 |
+
{'role': 'system',
|
| 39 |
+
'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},
|
| 40 |
+
{'role': 'user',
|
| 41 |
+
'content': query_text}
|
| 42 |
+
]
|
| 43 |
+
)
|
| 44 |
+
return agata.choices[0].message.content
|
| 45 |
+
demo=gr.ChatInterface(
|
| 46 |
+
fn=szukaj,
|
| 47 |
+
theme=gr.themes.Glass(font='OpenSans'),
|
| 48 |
+
title='Agata',
|
| 49 |
+
description='Twoja doradczyni w zatrudnianiu osób z niepełnosprawnościami.',
|
| 50 |
+
submit_btn='Zapytaj',
|
| 51 |
+
clear_btn='Wyczyść',
|
| 52 |
+
retry_btn=None,
|
| 53 |
+
undo_btn=None,
|
| 54 |
+
show_progress='minimal',
|
| 55 |
+
).launch(show_api=False, inbrowser=True)
|
praca.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:679412f31e5d37910fe973e45c965b88b804b5c047edce82a441307c14e1a75e
|
| 3 |
+
size 166608
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
sentence-transformers
|
| 2 |
+
openai
|
| 3 |
+
scipy
|