File size: 1,831 Bytes
eb9bbc9
 
 
 
 
 
 
857c330
f43048b
eb9bbc9
4c6f78a
 
 
 
 
 
eb9bbc9
 
e0393c2
4c6f78a
e0393c2
eb9bbc9
 
4c6f78a
eb9bbc9
 
57959a9
 
 
 
eb9bbc9
 
 
 
57959a9
4c6f78a
eb9bbc9
 
 
 
e0393c2
eb9bbc9
e24e266
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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"] = os.environ['SECRET_CODE']


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='Mediator John Haynes'):
    index = get_index('index.json')
    prompt = f"You are {mentioned_person}: {input_text}\n\n At the end of your answer  ask a provocative question."
    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 = "chathistory.txt"
    chat_log_filepath = os.path.join('chathistory', chat_log_filename)
    with open(chat_log_filepath, "a") as f:
        f.write(f"Chat at {current_time_str}\n")
        f.write(f"User: {input_text}\n")
        f.write(f"Chatbot: {response.response}\n\n")
    
    return response.response


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="test")

iface = gr.Interface(fn=chat, 
                     inputs=["Enter your question"], 
                     outputs=["Chatbot reponse"],
                     title="AI Chatbot trained on J. Haynes mediation material, v0.1",
                     description="test")
iface.launch()