AhmedEwis's picture
Update app.py
21a3057
raw
history blame
No virus
2.9 kB
# -*- coding: utf-8 -*-
"""Copy of Copy of Chatbot with custom knowledge base
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1VSXUmag_76fzebs16YhW_as4mdhHNdkx
"""
#pip install llama-index
#pip install langchain
#pip install gradio
#pip install pandas
#pip install openpyxl
import pandas as pd
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import sys
import os
from IPython.display import Markdown, display
import pandas as pd
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
from IPython.display import Markdown, display
import gradio as gr
import gradio
import pandas as pd
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import sys
import os
from IPython.display import Markdown, display
import pandas as pd
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
from IPython.display import Markdown, display
import streamlit as st
import pickle
df = pd.read_excel('Shegardi_dataset.xlsx',sheet_name = 'dataset')
os.environ['OPENAI_API_KEY'] = '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'
def construct_index(directory_path):
max_input_size = 4096
num_outputs = 2000
max_chunk_overlap = 20
chunk_size_limit = 600
llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.5, model_name="text-davinci-003", max_tokens=num_outputs))
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
documents = SimpleDirectoryReader(directory_path).load_data()
index = GPTSimpleVectorIndex(
documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper
)
index.save_to_disk('index.json')
with open('llm_predictor.pkl', 'wb') as f:
pickle.dump(llm_predictor, f)
return index
def ask_ai(query):
with open('llm_predictor.pkl', 'rb') as f:
llm_predictor = pickle.load(f)
index = GPTSimpleVectorIndex.load_from_disk('index.json', llm_predictor=llm_predictor)
response = index.query(query, response_mode="compact")
return response.response
iface = gr.Interface(fn=ask_ai, inputs="text", outputs="text", title="The following is a conversation with a human called Shegardi. Shegardi is helpful, precise, truthful, and very friendly.  Also, Shegardi is an employee of Warba Bank, located in Kuwait. Shegardi will only use the information provided to him. ",
description="Enter a question and get an answer from Shegardi.")
iface.launch(share=True)