Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper | |
| from langchain import OpenAI | |
| from langchain.chat_models import ChatOpenAI | |
| import sys | |
| import os | |
| directory_path = os.getcwd() | |
| openai_api_key = os.environ.get("OPENAI_API_KEY") | |
| def construct_index(): | |
| # set maximum input size | |
| max_input_size = 4096 | |
| # set number of output tokens | |
| num_outputs = 256 | |
| # set maximum chunk overlap | |
| max_chunk_overlap = 0 | |
| # set chunk size limit | |
| 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, 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 chatbot_response(query_input, chat_history=[]): | |
| index = construct_index() | |
| response = index.query(query_input, response_mode="compact") | |
| chat_history.append(("You:\n", query_input)) | |
| chat_history.append(("Bot:", response.response)) | |
| chat_history_text = "\n\n".join(["{}{}".format(item[0], item[1]) for item in chat_history[-9:-1]]) | |
| response_text = f"{chat_history_text} \n\nBot:{response.response}" | |
| return response_text | |
| chat_history = [] | |
| query_input = gr.Textbox(label="What do you want to ask the bot?") | |
| output_text = gr.Textbox(label="Bot Response and Chat History") | |
| def get_chatbot_response(query_input): | |
| global chat_history | |
| response_text = chatbot_response(query_input, chat_history) | |
| # chat_history = chat_history[-10:-1] | |
| return response_text | |
| interface = gr.Interface( | |
| fn=get_chatbot_response, | |
| inputs=query_input, | |
| outputs=output_text, | |
| title="Chatbot", | |
| description="Ask the bot any question and it will give you a response.", | |
| examples=["What are some popular tourist destinations in Europe?","What are some famous landmarks in Paris?","What can visitors do in Rome, Italy?","What are some must-see sights in Rome?"], | |
| ) | |
| interface.launch() |