Peter P commited on
Commit
eb9bbc9
·
1 Parent(s): 4d887b0

Add application file

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
2
+ from langchain import OpenAI
3
+ import gradio as gr
4
+ import sys
5
+ import os
6
+ import datetime
7
+
8
+ os.environ["OPENAI_API_KEY"] = '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'
9
+ mentioned_person = 'Mediation teacher John Haynes:'
10
+
11
+ def construct_index(directory_path):
12
+ max_input_size = 4096
13
+ num_outputs = 512
14
+ max_chunk_overlap = 20
15
+ chunk_size_limit = 600
16
+
17
+ prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
18
+ prompt = "You are John Haynes, an experienced mediator who lived from 1923 to 1999. You are sometimes called the grandfather of mediation practice because you helped train many mediators during your life. In addition to providing advice on mediation practice you will also finish your answers by asking deep, philosophical and insightful questions based on the topic presented. \n\n"
19
+
20
+ llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
21
+
22
+ documents = SimpleDirectoryReader(directory_path).load_data()
23
+
24
+ index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
25
+
26
+ index.save_to_disk('index.json')
27
+
28
+ return index
29
+
30
+ def chatbot(input_text, mentioned_person):
31
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
32
+ 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...-."
33
+ response = index.query(prompt, response_mode="compact")
34
+
35
+ # Check if response includes a question mark
36
+ if "?" not in response.response:
37
+ # If response does not include a question, add one
38
+ response.response += "\n\nWhat are your thoughts on this?"
39
+
40
+ # Save chat log
41
+ current_time = datetime.datetime.now()
42
+ current_time_str = current_time.strftime("%Y-%m-%d_%H-%M-%S")
43
+ chat_log_filename = f"{current_time_str}.txt"
44
+ chat_log_filepath = os.path.join('docs/chathistory', chat_log_filename)
45
+ with open(chat_log_filepath, "w") as f:
46
+ f.write(f"Chat started at {current_time_str}\n\n")
47
+ f.write(f"User: {input_text}\n")
48
+ f.write(f"Chatbot: {response.response}\n\n")
49
+
50
+ return response.response
51
+
52
+
53
+ with open("docs/about/descript.txt", "r") as f:
54
+ description = f.read()
55
+
56
+ iface = gr.Interface(fn=chatbot,
57
+ inputs=gr.inputs.Textbox(lines=5, label="Enter your question"),
58
+ outputs=gr.outputs.Textbox(label="Chatbot Response"),
59
+ title="AI Chatbot trained on J. Haynes mediation material, v0.1",
60
+ description=description)
61
+
62
+
63
+ index = construct_index("docs")
64
+ iface.launch(share=True)
65
+