CoolFace
Apppublic

robyramos/smart

sourceHugging Faceotherupdated 3y agoView on Hugging Face
0likes
app.py75 linesDownload Raw Back to root
1from glob import glob2from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper3from langchain.chat_models import ChatOpenAI4import gradio as gr5import sys6import os7import zipfile8 9 10 11OPENAI_API_KEY = os.getenv('token')12 13 14 15def construct_index():16    # extrair arquivos do zip17    zip_path = "tema.zip"18    try:19        with zipfile.ZipFile(zip_path, 'r') as zip_ref:20            zip_ref.extractall(".")21    except Exception as e:22        print("Erro ao extrair o arquivo zip:", e)23 24        25    max_input_size = 350026    num_outputs = 51227    max_chunk_overlap = 2028    chunk_size_limit = 60029 30    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)31 32    llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.3, model_name="gpt-3.5-turbo", max_tokens=num_outputs))33 34    documents = SimpleDirectoryReader(".").load_data()35 36    index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)37 38    index.save_to_disk('index.json')39 40    return index41 42 43def chatbot(input_text):44    index = GPTSimpleVectorIndex.load_from_disk('index.json')45 46    # Ler e concatenar os documentos da pasta "docs" como o contexto relevante47    documents = ""48    for file_path in glob(os.path.join(".", "*.{txt,pdf}")):49        with open(file_path, "r") as f:50            documents += f.read() + " "51    contexto = documents.strip()52 53    # Combinar o contexto e a pergunta de entrada54    with open('tema.txt', 'r') as f:55        texto_prefixo = f.readline().strip()56    texto_entrada = f"Dentro do assunto {texto_prefixo} me responda: {input_text}{contexto} se não for {texto_prefixo} não responda"57    print(texto_entrada)58      59    response = index.query(texto_entrada, response_mode="compact")60    return response.response61 62 63description = """64A IA foi treinada com materiais enviados e responde perguntas sobre o tema definido!65"""66 67iface = gr.Interface(fn=chatbot,68                     inputs=gr.components.Textbox(lines=7, label="Como podemos te ajudar?"),69                     outputs="text",70                     description=description,                     71                     title="Demonstração Chat OpenAI")72 73 74index = construct_index()75iface.launch(share=False)