File size: 1,412 Bytes
157cf8c
 
 
8e61694
82f04bb
 
8e61694
 
157cf8c
 
 
 
 
 
0230ec8
157cf8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68e1a2a
 
 
 
 
157cf8c
68e1a2a
 
 
 
157cf8c
 
 
 
0230ec8
157cf8c
0230ec8
 
157cf8c
 
 
 
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
import openai
import faiss
import numpy as np
import os
import gradio as gr

openai.api_key = os.environ['api_top']
text_file_path = 'risale.txt'

# read the text file
with open(text_file_path, 'r', encoding='utf-8') as file:
    text_content = file.read()

# split text into sentences
allText = [sentence.strip() for sentence in text_content.split('<br>') if sentence.strip()]

def encode_open(input):
# you can change model if you want different
    MODEL = "text-embedding-ada-002"
    res = openai.Embedding.create(
       input=input, engine=MODEL
    )
    embeds = [record['embedding'] for record in res['data']]
    import torch
    embeds=torch.FloatTensor(embeds)
    return embeds

encoded_data=encode_open(allText)
d=encoded_data.shape[1]
index = faiss.IndexIDMap(faiss.IndexFlatIP(d))
index.add_with_ids(encoded_data, np.arange(encoded_data.shape[0]))

def search_openai_pdf(query, k=10):
    try:
       query_vector = encode_open([query])
       top_k = index.search(query_vector, k)
    
       return [
        allText[_id] for _id in top_k[1][0]
       ] 
    except Exception as e:
        return ["Hata oluştu", e]
    


iface = gr.Interface(
    fn=search_openai_pdf,
    inputs=gr.Textbox(placeholder="Fihristte aramak için bir şeyler yaz.",label='query'),
    outputs=gr.Textbox(label="Sonuçlar"),
    live=False,
    title="Sözler Fihristi Anlamsal Arama Motoru"
)

iface.launch()