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('
') 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()