custom-chatbot / app.py
fastx's picture
Update app.py
2e83dbc
raw
history blame
2.56 kB
import gradio as gr
import json
import os
from llama_index import SimpleDirectoryReader, GPTListIndex, readers, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import sys
from IPython.display import Markdown, display
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="gpt-3.5-turbo", max_tokens=num_outputs))
prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
'''
import tkinter as tk
from llama_index import GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
from IPython.display import Markdown, display
# Define the GUI
class ChatBotGUI:
def __init__(self, master):
self.master = master
master.title("Chat Bot")
# Create a label and an entry for the question
self.label = tk.Label(master, text="Ask me anything:")
self.label.pack()
self.entry = tk.Entry(master)
self.entry.pack()
# Create a button to submit the question
self.button = tk.Button(master, text="Submit", command=self.submit_question)
self.button.pack()
# Create a text box to display the response
self.textbox = tk.Text(master)
self.textbox.pack()
def submit_question(self):
question = self.entry.get()
response = ask_ai(question)
self.textbox.insert(tk.END, "You: " + question + "\n")
self.textbox.insert(tk.END, "Bot: " + response + "\n\n")
self.entry.delete(0, tk.END)
# Create an instance of the GUI and start the main loop
root = tk.Tk()
chatbot_gui = ChatBotGUI(root)
root.mainloop()
'''
os.environ["OPENAI_API_KEY"] = "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"
construct_index("data")
def ask_ai(question,openai_api_key):
if openai_api_key == "":
os.environ["OPENAI_API_KEY"] = "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"
else:
os.environ["OPENAI_API_KEY"] = openai_api_key
construct_index("data")
index = GPTSimpleVectorIndex.load_from_disk('index.json')
response = index.query(question, response_mode="compact")
return response.response
api_key = gr.inputs.Textbox(label="Paste OPENAI API Key (Or left it blank to use default api)")
iface = gr.Interface(fn=ask_ai, inputs=["text", api_key], outputs="text", title="Chatbot")
iface.launch()