MediatorBot / app.py
peterpull's picture
Update app.py
4c6f78a
raw
history blame
1.78 kB
from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
from langchain import OpenAI
import gradio as gr
import sys
import os
import datetime
os.environ["OPENAI_API_KEY"] = 'SECRET'
def get_index(index_file_path):
if os.path.exists(index_file_path):
return GPTSimpleVectorIndex.load_from_disk(index_file_path)
else:
print(f"Error: '{index_file_path}' does not exist.")
sys.exit()
def chatbot(input_text, mentioned_person='Mediation teacher John Haynes'):
index = get_index('index.json')
prompt = f"{mentioned_person}: {input_text}\n\n At the end of your answer, if you think appropriate, please ask a provocative question. Start it with a polite phrase such as - I wonder what you think...-."
response = index.query(prompt, response_mode="compact")
# Save chat log
current_time = datetime.datetime.now()
current_time_str = current_time.strftime("%Y-%m-%d_%H-%M-%S")
chat_log_filename = f"{current_time_str}.txt"
chat_log_filepath = os.path.join('docs/chathistory', chat_log_filename)
with open(chat_log_filepath, "w") as f:
f.write(f"Chat started at {current_time_str}\n\n")
f.write(f"User: {input_text}\n")
f.write(f"Chatbot: {response.response}\n\n")
return response.response
with open("docs/about/descript.txt", "r") as f:
description = f.read()
iface = gr.Interface(fn=chatbot,
inputs=gr.inputs.Textbox(lines=5, label="Enter your question"),
outputs=gr.outputs.Textbox(label="Chatbot Response"),
title="AI Chatbot trained on J. Haynes mediation material, v0.1",
description=description)
iface.launch(share=True)