Spaces:
BHO
/
Runtime error

File size: 4,071 Bytes
7fbc363
 
 
 
 
 
 
 
 
 
a50b0b8
 
 
 
 
7fbc363
 
 
 
d8b6e07
47d9c0b
7fbc363
 
 
000e3c2
 
 
 
 
 
 
 
 
9f679b8
000e3c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fbc363
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
000e3c2
 
 
 
f3cb278
 
 
a9df1a3
000e3c2
7fbc363
000e3c2
7fbc363
 
 
 
 
 
 
 
8f8811d
 
8c6917a
5bd0ed8
000e3c2
8f8811d
 
f3cb278
5bd0ed8
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import gradio as gr
import os 
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.document_loaders import PyPDFLoader
from langchain.document_loaders import DirectoryLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma

from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext
#from langchain import OpenAI
#import gradio as gr
import sys


# Set the path of your new directory
dir_path = "./docs"

os.environ["OPENAI_API_KEY"] 

# Create the directory using the os module
os.makedirs(dir_path, exist_ok=True)

def construct_index(directory_path):
    max_input_size = 4096
    num_outputs = 512
    max_chunk_overlap = 20
    chunk_size_limit = 600

    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)

    #llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.4, model_name="text-davinci-003", max_tokens=num_outputs))
    llm_predictor = LLMPredictor(llm=OpenAI(temperature=0, model_name="gpt-3.5-turbo", max_tokens=num_outputs))


    service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor,  prompt_helper=prompt_helper)

    documents = SimpleDirectoryReader(directory_path).load_data()

    index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context) #, llm_predictor=llm_predictor, prompt_helper=prompt_helper)

    index.save_to_disk('index.json')

    return index

def chatbot(input_text):
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    response = index.query(input_text, response_mode="compact")
    return response.response

def qa_system(pdf_file, openai_key, prompt, chain_type, k):
    os.environ["OPENAI_API_KEY"] = openai_key
    
    # load document
    # loader = PyPDFLoader(pdf_file.name)
    loader = DirectoryLoader(dir_path, glob="**/*.pdf") #, loader_cls=PDFLoader)
    documents = loader.load()
    
    # split the documents into chunks
    text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
    texts = text_splitter.split_documents(documents)
    
    # select which embeddings we want to use
    embeddings = OpenAIEmbeddings()
    
    # create the vectorestore to use as the index
    db = Chroma.from_documents(texts, embeddings)
    
    # expose this index in a retriever interface
    retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
    
    # create a chain to answer questions 
    qa = RetrievalQA.from_chain_type(
        llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)
    
    # get the result
    result = qa({"query": prompt})
    return result['result'], [doc.page_content for doc in result["source_documents"]]


#index = construct_index("docs")
index = construct_index(dir_path)

# Describe principles
# rephrase the questions example
# Examples
# examples_questions = gr.Examples(["What is Sofidy to Tikehau?", "What is TSO2 ?"])

# define the Gradio interface
# input_file = gr.inputs.File(label="PDF File")
openai_key = gr.inputs.Textbox(label="OpenAI API Key", type="password")
prompt = gr.inputs.Textbox(label="Question Prompt")
chain_type = gr.inputs.Radio(['stuff', 'map_reduce', "refine", "map_rerank"], label="Chain Type")
k = gr.inputs.Slider(minimum=1, maximum=5, default=1, label="Number of Relevant Chunks")

output_text = gr.outputs.Textbox(label="Answer")
output_docs = gr.outputs.Textbox(label="Relevant Source Text")

#gr.Interface(fn=chatbot, 
 #            inputs=[openai_key, prompt, chain_type, k], outputs=[output_text, output_docs], 
 #            title="TikehauGPT Question Answering with PDF File and OpenAI", 
 #            description="Tikehau URDs.").launch(debug = True)

gr.Interface(fn=chatbot, 
             inputs= prompt, outputs="text", 
             title="TKO GPT for URDs - experimental", 
             description="Tikehau URDs.").launch(debug = True)