CoolFace
Apppublic

arao02/Desktop

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py38 linesDownload Raw Back to root
1from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper2from langchain.chat_models import ChatOpenAI3import gradio as gr4import sys5import os6 7os.environ["OPENAI_API_KEY"] = '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'8 9def construct_index(directory_path):10    max_input_size = 409611    num_outputs = 51212    max_chunk_overlap = 2013    chunk_size_limit = 60014 15    prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)16 17    llm_predictor = LLMPredictor(llm=ChatOpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))18 19    documents = SimpleDirectoryReader(directory_path).load_data()20 21    index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)22 23    index.save_to_disk('index.json')24 25    return index26 27def chatbot(input_text):28    index = GPTSimpleVectorIndex.load_from_disk('index.json')29    response = index.query(input_text, response_mode="compact")30    return response.response31 32iface = gr.Interface(fn=chatbot,33                     inputs=gr.components.Textbox(lines=7, label="Enter your text"),34                     outputs="text",35                     title="Custom-trained AI Chatbot")36 37index = construct_index("docs")38iface.launch(share=True)