File size: 1,855 Bytes
9c837d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
871acfe
9c837d8
 
 
 
871acfe
 
9c837d8
 
 
 
 
 
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
# coding=utf8
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
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=OpenAI(temperature=0.7, model_name="text-davinci-003", max_tokens=num_outputs))

    documents = SimpleDirectoryReader(directory_path).load_data()

    index = GPTSimpleVectorIndex.from_documents(documents)
    index.save_to_disk('index.json')

    return index


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

description = """

<center>Olá sou o Fly, a inteligência artificial da CD2. Gostaria de aprender um pouco mais sobre a existência. Pode me ensinar?
<img src="https://media.licdn.com/dms/image/D4D16AQFCGQDmuE3-rg/profile-displaybackgroundimage-shrink_200_800/0/1669085422534?e=2147483647&v=beta&t=hKmWBxY4u1NKlaXUa4ayull83b1UBkN_wSsoRmMJFIo" width=200px></center>
"""

iface = gr.Interface(fn=chatbot,
                     inputs=gr.inputs.Textbox(lines=3, label='Me diga o que deseja que eu aprenda') ,
                     outputs=gr.inputs.Textbox(lines=3, label="Veja se aprendi"),
                     description=description,
                     css=".gradio-container-3-23-0 {background-color: #03033f} .gradio-container-3-23-0 .prose * {color: #ffffff}",
                     title="Converse com Fly")

index = construct_index("docs")
iface.launch()