peterpull commited on
Commit
4c6f78a
·
1 Parent(s): e8fecae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -30
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
3
  from langchain import OpenAI
4
  import gradio as gr
@@ -9,36 +8,21 @@ import datetime
9
  os.environ["OPENAI_API_KEY"] = 'SECRET'
10
 
11
 
12
- def construct_index(directory_path):
13
- max_input_size = 4096
14
- num_outputs = 512
15
- max_chunk_overlap = 20
16
- chunk_size_limit = 600
17
-
18
- prompt_helper = PromptHelper(max_input_size, num_outputs, max_chunk_overlap, chunk_size_limit=chunk_size_limit)
19
- 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"
20
-
21
- llm_predictor = LLMPredictor(llm=OpenAI(temperature=0.7, model_name="gpt-3.5-turbo", max_tokens=num_outputs))
22
-
23
- documents = SimpleDirectoryReader(directory_path).load_data()
24
-
25
- index = GPTSimpleVectorIndex(documents, llm_predictor=llm_predictor, prompt_helper=prompt_helper)
26
 
27
- index.save_to_disk('index.json')
28
-
29
- return index
30
 
31
  def chatbot(input_text, mentioned_person='Mediation teacher John Haynes'):
32
- index = GPTSimpleVectorIndex.load_from_disk('index.json')
33
  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...-."
34
  response = index.query(prompt, response_mode="compact")
35
 
36
- # Check if response includes a question mark
37
- if "?" not in response.response:
38
- # If response does not include a question, add one
39
- response.response += "\n\nWhat are your thoughts on this?"
40
 
41
- # Save chat log
42
  current_time = datetime.datetime.now()
43
  current_time_str = current_time.strftime("%Y-%m-%d_%H-%M-%S")
44
  chat_log_filename = f"{current_time_str}.txt"
@@ -50,17 +34,14 @@ def chatbot(input_text, mentioned_person='Mediation teacher John Haynes'):
50
 
51
  return response.response
52
 
53
-
54
  with open("docs/about/descript.txt", "r") as f:
55
  description = f.read()
56
-
57
  iface = gr.Interface(fn=chatbot,
58
  inputs=gr.inputs.Textbox(lines=5, label="Enter your question"),
59
  outputs=gr.outputs.Textbox(label="Chatbot Response"),
60
  title="AI Chatbot trained on J. Haynes mediation material, v0.1",
61
  description=description)
62
-
63
-
64
- index = construct_index("docs")
65
- iface.launch(share=True)
66
 
 
 
 
1
  from gpt_index import SimpleDirectoryReader, GPTListIndex, GPTSimpleVectorIndex, LLMPredictor, PromptHelper
2
  from langchain import OpenAI
3
  import gradio as gr
 
8
  os.environ["OPENAI_API_KEY"] = 'SECRET'
9
 
10
 
11
+ def get_index(index_file_path):
12
+ if os.path.exists(index_file_path):
13
+ return GPTSimpleVectorIndex.load_from_disk(index_file_path)
14
+ else:
15
+ print(f"Error: '{index_file_path}' does not exist.")
16
+ sys.exit()
 
 
 
 
 
 
 
 
17
 
 
 
 
18
 
19
  def chatbot(input_text, mentioned_person='Mediation teacher John Haynes'):
20
+ index = get_index('index.json')
21
  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...-."
22
  response = index.query(prompt, response_mode="compact")
23
 
 
 
 
 
24
 
25
+ # Save chat log
26
  current_time = datetime.datetime.now()
27
  current_time_str = current_time.strftime("%Y-%m-%d_%H-%M-%S")
28
  chat_log_filename = f"{current_time_str}.txt"
 
34
 
35
  return response.response
36
 
37
+
38
  with open("docs/about/descript.txt", "r") as f:
39
  description = f.read()
40
+
41
  iface = gr.Interface(fn=chatbot,
42
  inputs=gr.inputs.Textbox(lines=5, label="Enter your question"),
43
  outputs=gr.outputs.Textbox(label="Chatbot Response"),
44
  title="AI Chatbot trained on J. Haynes mediation material, v0.1",
45
  description=description)
 
 
 
 
46
 
47
+ iface.launch(share=True)