Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
text_file_path = 'risale3.txt'
|
| 6 |
+
|
| 7 |
+
# read the text file
|
| 8 |
+
with open(text_file_path, 'r', encoding='utf-8') as file:
|
| 9 |
+
text_content = file.read()
|
| 10 |
+
|
| 11 |
+
# split text into sentences
|
| 12 |
+
allText = [sentence.strip() for sentence in text_content.split('<br>') if sentence.strip()]
|
| 13 |
+
|
| 14 |
+
def encode_open(input):
|
| 15 |
+
# you can change model if you want different
|
| 16 |
+
MODEL = "text-embedding-ada-002"
|
| 17 |
+
res = openai.Embedding.create(
|
| 18 |
+
input=input, engine=MODEL
|
| 19 |
+
)
|
| 20 |
+
embeds = [record['embedding'] for record in res['data']]
|
| 21 |
+
import torch
|
| 22 |
+
embeds=torch.FloatTensor(embeds)
|
| 23 |
+
return embeds
|
| 24 |
+
|
| 25 |
+
encoded_data=encode_open(allText)
|
| 26 |
+
d=encoded_data.shape[1]
|
| 27 |
+
index = faiss.IndexIDMap(faiss.IndexFlatIP(d))
|
| 28 |
+
index.add_with_ids(encoded_data, np.arange(encoded_data.shape[0]))
|
| 29 |
+
|
| 30 |
+
def search_openai_pdf(query, k=10):
|
| 31 |
+
query_vector = encode_open([query])
|
| 32 |
+
top_k = index.search(query_vector, k)
|
| 33 |
+
print(top_k[1])
|
| 34 |
+
return [
|
| 35 |
+
allText[_id] for _id in top_k[1][0]
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
search_openai_pdf("ibadet neden etmeliyim")
|
| 39 |
+
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=search_openai_pdf,
|
| 42 |
+
inputs=gr.Textbox(text="ibadet neden etmeliyim", label="query"),
|
| 43 |
+
outputs=gr.Textbox(label="Sonuçlar"),
|
| 44 |
+
live=True
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
iface.launch()
|
| 48 |
+
|