Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from datasets import load_dataset, load_from_disk, Dataset
|
3 |
+
from transformers import AutoTokenizer, AutoModel
|
4 |
+
import torch
|
5 |
+
import pandas as pd
|
6 |
+
import base64
|
7 |
+
|
8 |
+
styleBase64 = 'PHN0eWxlPi8qIFRleHQgU3R5bGVzICovCmhyIHsgY29sb3I6ICMwMDAwMDB9CmJvZHksIHRhYmxlLCB0ciwgdGggLyogTm9ybWFsICovCnsKIGZvbnQtc2l6ZTogMTJwdDsKIGZvbnQtZmFtaWx5OiAnU2Vnb2UgVUknOwogZm9udC1zdHlsZTogbm9ybWFsOwogZm9udC13ZWlnaHQ6IG5vcm1hbDsKIGNvbG9yOiAjMDAwMDAwOwogdGV4dC1kZWNvcmF0aW9uOiBub25lOwp9CnNwYW4uZl9ibGF6b3IgLyogYmxhem9yICovCnsKIGZvbnQtc2l6ZTogMTBwdDsKIGZvbnQtZmFtaWx5OiAnQ291cmllciBOZXcnOwogY29sb3I6ICM4MDAwODA7Cn0Kc3Bhbi5mX2NibHVlIC8qIGNibHVlICovCnsKIGZvbnQtc2l6ZTogMTBwdDsKIGZvbnQtZmFtaWx5OiAnQ291cmllciBOZXcnOwogY29sb3I6ICMwMDAwZmY7Cn0Kc3Bhbi5mX2NjbGFzcyAvKiBjY2xhc3MgKi8KewogZm9udC1zaXplOiAxMHB0OwogZm9udC1mYW1pbHk6ICdDb3VyaWVyIE5ldyc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GVyOjFweCBzb2xpZCAjRkYwMDAwOwogIGJvcmRlci1yYWRpdXM6IDVweDsKICBib3gtc2hhZG93OiA1cHggNXB4IDVweCAjODg4Owp9CgojaG1saWdodGJveCAvKiBpbWFnZSBsaWdodGJveCAqLwp7CiAgYm9yZGVyOiBub25lOwogIHBhZGRpbmc6IDhweDsKICBiYWNrZ3JvdW5kOiAjRkZGOwogIGZvbnQtc2l6ZTogMTEwJTsKICBmb250LXdlaWdodDogYm9sZDsKICBjb2xvcjogIzc3NzsKICBib3gtc2hhZG93OiA1cHggNXB4IDVweCAjODg4Owp9CgphLmRyb3Bkb3duLXRvZ2dsZSAvKiBkcm9wZG93biB0b2dnbGUgY2FwdGlvbiAqLwp7CiAgY3Vyc29yOiBwb2ludGVyOwp9CmEuaW5saW5lLXRvZ2dsZSAvKiBpbmxpbmUgdG9nZ2xlIGNhcHRpb24gKi8KewogIGN1cnNvcjogcG9pbnRlcjsKfQppbWcuZHJvcGRvd24tdG9nZ2xlLWljb24gLyogdG9nZ2xlIGljb24gKi8KewogIGN1cnNvcjogcG9pbnRlcjsKfQo8L3N0eWxlPg=='
|
9 |
+
style = base64.b64decode(styleBase64).decode("ascii")
|
10 |
+
|
11 |
+
model_ckpt = "nomic-ai/nomic-embed-text-v1.5"
|
12 |
+
|
13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
|
14 |
+
model = AutoModel.from_pretrained(model_ckpt, trust_remote_code=True)
|
15 |
+
|
16 |
+
device = torch.device("cpu")
|
17 |
+
model.to(device)
|
18 |
+
|
19 |
+
|
20 |
+
def cls_pooling(model_output):
|
21 |
+
return model_output.last_hidden_state[:, 0]
|
22 |
+
|
23 |
+
def get_embeddings(text_list):
|
24 |
+
encoded_input = tokenizer(
|
25 |
+
text_list, padding=True, truncation=True, return_tensors="pt"
|
26 |
+
)
|
27 |
+
encoded_input = {k: v.to(device) for k, v in encoded_input.items()}
|
28 |
+
model_output = model(**encoded_input)
|
29 |
+
return cls_pooling(model_output)
|
30 |
+
|
31 |
+
|
32 |
+
embeddings_dataset = load_dataset("Vadim212/doctest1")["train"]
|
33 |
+
embeddings_dataset.add_faiss_index(column="embeddings")
|
34 |
+
|
35 |
+
def find(question):
|
36 |
+
|
37 |
+
question_embedding = get_embeddings([question]).cpu().detach().numpy()
|
38 |
+
|
39 |
+
scores, samples = embeddings_dataset.get_nearest_examples(
|
40 |
+
"embeddings", question_embedding, k=5
|
41 |
+
)
|
42 |
+
|
43 |
+
samples_df = pd.DataFrame.from_dict(samples)
|
44 |
+
samples_df["scores"] = scores
|
45 |
+
samples_df.sort_values("scores", ascending=True, inplace=True)
|
46 |
+
|
47 |
+
result = [(style + f"<font size='20'><b>Answer {i}:</b></font>" + row.url + "<hr>") for i, row in samples_df.iterrows()]
|
48 |
+
return result
|
49 |
+
|
50 |
+
|
51 |
+
demo = gr.Blocks()
|
52 |
+
|
53 |
+
with demo:
|
54 |
+
inp = gr.Textbox(placeholder="Enter prompt", label= "Prompt like: 'how to export to PDF?', 'What is Stimulsoft Reports?', 'What is Dashboards?', 'how to render report?', 'List of exports'" )
|
55 |
+
find_btn = gr.Button("Find")
|
56 |
+
big_block = [gr.HTML("") for i in range(5)]
|
57 |
+
|
58 |
+
find_btn.click(find,
|
59 |
+
inputs=inp,
|
60 |
+
outputs=big_block)
|
61 |
+
|
62 |
+
demo.launch()
|