Spaces:
Runtime error

File size: 2,049 Bytes
30abcc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain.chat_models import ChatOpenAI
import gradio as gr
import sys
import os

os.environ["OPENAI_API_KEY"] = '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'

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=ChatOpenAI(model="text-davinci-003",temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))

    documents = SimpleDirectoryReader(directory_path).load_data()

    index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)

    index.save_to_disk('index.json')

    return index

def chat(input_text,history):
    index = GPTSimpleVectorIndex.load_from_disk('index.json')
    history = history or []
    s = list(sum(history, ()))
    s.append(input_text)
    inp = ' '.join(s)
    output = index.query(input_text,response_mode="compact")
    history.append((input_text, str(output.response)))
    return history,history


block = gr.Blocks()

with block:
    gr.HTML('''
    <div style="display: flex; align-items: center; justify-content: center;">
      <h1 style="margin: 0; padding: 0;">SufiBot</h1>
      <img src="https://www.cloudsufi.com/wp-content/uploads/2020/09/Cloudsufi-Logos_Colour-Dots@4x-805x1024.png"
           width="40" style="margin-left: 10px;">
    </div>
    ''')
    chatbot = gr.Chatbot()
    message = gr.Textbox(placeholder='Send a message.')
    state = gr.State()
    with gr.Row():
        submit = gr.Button("SEND")
        clear = gr.Button("Restart")
        theme = "darkhuggingface"
        submit.click(chat, inputs=[message, state], outputs=[chatbot, state])
        if clear.click(lambda: None, None, chatbot, queue=False)==True:
            history=[]


block.launch(share=False)