#!git clone https://github.com/sudipmondal1310/Internship.git #!pip install llama-index==0.5.6 #!pip install langchain==0.0.148 from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper, ServiceContext from langchain import OpenAI import sys import os from IPython.display import Markdown, display import time import random import gradio as gr def construct_index(directory_path): # set maximum input size max_input_size = 4096 # set number of output tokens num_outputs = 2000 # set maximum chunk overlap max_chunk_overlap = 20 # set chunk size limit chunk_size_limit = 600 # define prompt helper prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit) # define LLM llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs)) documents = SimpleDirectoryReader(directory_path).load_data() service_context = ServiceContext.from_defaults(llm_predictor=llm_predictor, prompt_helper=prompt_helper) index = GPTSimpleVectorIndex.from_documents(documents, service_context=service_context) index.save_to_disk('index.json') return index def ask_ai_new(query): index = GPTSimpleVectorIndex.load_from_disk('index.json') while True: #query = input("What do you want to ask? ") response = index.query(query) response = Markdown(f"{response.response}") print(response) return response.data os.environ["OPENAI_API_KEY"]="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" construct_index("Data") #pip install gradio with gr.Blocks() as demo: chatbot = gr.Chatbot() query = gr.inputs.Textbox(label="Enter your message here") clear = gr.Button("Clear") def user(user_message, history): return "", history + [[user_message, None]] def bot(history): bot_message = ask_ai_new(history[-1][0]) print(bot_message) history[-1][1] = "" for character in bot_message: history[-1][1] += character time.sleep(0.05) yield history query.submit(user, [query, chatbot], [query, chatbot], queue=False).then( bot, chatbot, chatbot ) clear.click(lambda: None, None, chatbot, queue=False) demo.queue() demo.launch()