brunhild217 commited on
Commit
37f3329
·
1 Parent(s): b721670

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -7
app.py CHANGED
@@ -4,9 +4,7 @@ import pandas as pd
4
  from functools import partial
5
  from ai_classroom_suite.UIBaseComponents import *
6
 
7
- # history is a list of list
8
- # [[user_input_str, bot_response_str], ...]
9
-
10
  class EchoingTutor(SlightlyDelusionalTutor):
11
  def add_user_message(self, user_message):
12
  self.conversation_memory.append([user_message, None])
@@ -36,6 +34,33 @@ def get_tutor_reply(user_message, chat_tutor):
36
  def get_conversation_history(chat_tutor):
37
  return chat_tutor.conversation_memory, chat_tutor
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  ### Instructor Interface Helper Functions ###
41
  def get_instructor_prompt(fileobj):
@@ -57,6 +82,7 @@ with gr.Blocks() as demo:
57
 
58
  # Student interface
59
  with gr.Tab("For Students"):
 
60
  # Chatbot interface
61
  gr.Markdown("""
62
  ## Chat with the Model
@@ -80,6 +106,7 @@ with gr.Blocks() as demo:
80
  chat_history = gr.JSON(label = "conversation history")
81
  test_btn.click(get_conversation_history, inputs=[study_tutor], outputs=[chat_history, study_tutor])
82
 
 
83
  with gr.Blocks():
84
  gr.Markdown("""
85
  ## Export Your Chat History
@@ -101,7 +128,6 @@ with gr.Blocks() as demo:
101
  # API Authentication functionality
102
  # Instead of ask students to provide key, the key is now provided by the instructor
103
  with gr.Box():
104
-
105
  gr.Markdown("### OpenAI API Key ")
106
  gr.HTML("""<span>Embed your OpenAI API key below; if you haven't created one already, visit
107
  <a href="https://platform.openai.com/account/api-keys">platform.openai.com/account/api-keys</a>
@@ -121,6 +147,7 @@ with gr.Blocks() as demo:
121
  # embed_key(api_input, study_tutor)
122
 
123
  # Upload secret prompt functionality
 
124
  with gr.Blocks():
125
  # testing purpose, change visible to False at deployment
126
  test_secret = gr.Textbox(label="Current secret prompt", value=os.environ.get("SECRET_PROMPT"), visible=True)
@@ -128,13 +155,20 @@ with gr.Blocks() as demo:
128
  file_input = gr.File(label="Load a .txt or .py file",
129
  file_types=['.py', '.txt'], type="file",
130
  elem_classes="short-height")
 
 
131
  instructor_prompt = gr.Textbox(label="Verify your prompt content", visible=True)
132
  file_input.upload(fn=get_instructor_prompt, inputs=file_input, outputs=instructor_prompt)
133
 
134
- # Set the secret prompt in this session
135
  prompt_submit_btn = gr.Button("Submit")
136
- prompt_submit_btn.click(fn=embed_prompt, inputs=instructor_prompt, outputs=test_secret)
137
-
 
 
 
 
 
138
  # TODO: may need a way to set the secret prompt permanently in settings/secret
139
 
140
  demo.queue().launch(server_name='0.0.0.0', server_port=7860)
 
4
  from functools import partial
5
  from ai_classroom_suite.UIBaseComponents import *
6
 
7
+ # TODO:
 
 
8
  class EchoingTutor(SlightlyDelusionalTutor):
9
  def add_user_message(self, user_message):
10
  self.conversation_memory.append([user_message, None])
 
34
  def get_conversation_history(chat_tutor):
35
  return chat_tutor.conversation_memory, chat_tutor
36
 
37
+ def create_prompt_store(chat_tutor, vs_button, upload_files, openai_auth):
38
+ text_segs = []
39
+ upload_segs = []
40
+
41
+ if upload_files:
42
+ print(upload_files)
43
+ upload_fnames = [f.name for f in upload_files]
44
+ upload_segs = get_document_segments(upload_fnames, 'file', chunk_size=700, chunk_overlap=100)
45
+
46
+ # get the full list of everything
47
+ all_segs = text_segs + upload_segs
48
+ print(all_segs)
49
+
50
+ # create the vector store and update tutor
51
+ vs_db, vs_retriever = create_local_vector_store(all_segs, search_kwargs={"k": 2})
52
+ chat_tutor.vector_store = vs_db
53
+ chat_tutor.vs_retriever = vs_retriever
54
+
55
+ # create the tutor chain
56
+ if not chat_tutor.api_key_valid or not chat_tutor.openai_auth:
57
+ chat_tutor = embed_key(openai_auth, chat_tutor)
58
+ qa_chain = create_tutor_mdl_chain(kind="retrieval_qa", mdl=chat_tutor.chat_llm, retriever = chat_tutor.vs_retriever, return_source_documents=True)
59
+ chat_tutor.tutor_chain = qa_chain
60
+
61
+ # return the store
62
+ return chat_tutor, gr.update(interactive=True, value='Tutor Initialized!')
63
+
64
 
65
  ### Instructor Interface Helper Functions ###
66
  def get_instructor_prompt(fileobj):
 
82
 
83
  # Student interface
84
  with gr.Tab("For Students"):
85
+
86
  # Chatbot interface
87
  gr.Markdown("""
88
  ## Chat with the Model
 
106
  chat_history = gr.JSON(label = "conversation history")
107
  test_btn.click(get_conversation_history, inputs=[study_tutor], outputs=[chat_history, study_tutor])
108
 
109
+ # Download conversation history file
110
  with gr.Blocks():
111
  gr.Markdown("""
112
  ## Export Your Chat History
 
128
  # API Authentication functionality
129
  # Instead of ask students to provide key, the key is now provided by the instructor
130
  with gr.Box():
 
131
  gr.Markdown("### OpenAI API Key ")
132
  gr.HTML("""<span>Embed your OpenAI API key below; if you haven't created one already, visit
133
  <a href="https://platform.openai.com/account/api-keys">platform.openai.com/account/api-keys</a>
 
147
  # embed_key(api_input, study_tutor)
148
 
149
  # Upload secret prompt functionality
150
+ # The instructor will provide a secret prompt/persona to the tutor
151
  with gr.Blocks():
152
  # testing purpose, change visible to False at deployment
153
  test_secret = gr.Textbox(label="Current secret prompt", value=os.environ.get("SECRET_PROMPT"), visible=True)
 
155
  file_input = gr.File(label="Load a .txt or .py file",
156
  file_types=['.py', '.txt'], type="file",
157
  elem_classes="short-height")
158
+
159
+ # Verify prompt content
160
  instructor_prompt = gr.Textbox(label="Verify your prompt content", visible=True)
161
  file_input.upload(fn=get_instructor_prompt, inputs=file_input, outputs=instructor_prompt)
162
 
163
+ # Set the secret prompt in this session and embed it to the study tutor
164
  prompt_submit_btn = gr.Button("Submit")
165
+ prompt_submit_btn.click(
166
+ fn=embed_prompt, inputs=instructor_prompt, outputs=test_secret
167
+ ).then(
168
+ fn=create_prompt_store,
169
+ inputs=[study_tutor, prompt_submit_btn, file_input, api_input],
170
+ outputs=[study_tutor, prompt_submit_btn]
171
+ )
172
  # TODO: may need a way to set the secret prompt permanently in settings/secret
173
 
174
  demo.queue().launch(server_name='0.0.0.0', server_port=7860)