Spaces:
Build error
Build error
Commit
Β·
b721670
1
Parent(s):
4f9dc3a
Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +23,6 @@ class EchoingTutor(SlightlyDelusionalTutor):
|
|
23 |
self.conversation_memory = []
|
24 |
self.flattened_conversation = ''
|
25 |
|
26 |
-
|
27 |
### Chatbot Functions ###
|
28 |
def add_user_message(user_message, chat_tutor):
|
29 |
"""Display user message and update chat history to include it."""
|
@@ -37,6 +36,8 @@ def get_tutor_reply(user_message, chat_tutor):
|
|
37 |
def get_conversation_history(chat_tutor):
|
38 |
return chat_tutor.conversation_memory, chat_tutor
|
39 |
|
|
|
|
|
40 |
def get_instructor_prompt(fileobj):
|
41 |
file_path = fileobj.name
|
42 |
f = open(file_path, "r")
|
@@ -47,14 +48,14 @@ def embed_prompt(instructor_prompt):
|
|
47 |
os.environ["SECRET_PROMPT"] = instructor_prompt
|
48 |
return os.environ.get("SECRET_PROMPT")
|
49 |
|
|
|
|
|
50 |
with gr.Blocks() as demo:
|
|
|
51 |
#initialize tutor (with state)
|
52 |
study_tutor = gr.State(EchoingTutor())
|
53 |
|
54 |
-
#
|
55 |
-
api_input = os.environ.get("OPENAI_API_KEY")
|
56 |
-
embed_key(api_input, study_tutor)
|
57 |
-
|
58 |
with gr.Tab("For Students"):
|
59 |
# Chatbot interface
|
60 |
gr.Markdown("""
|
@@ -94,8 +95,32 @@ with gr.Blocks() as demo:
|
|
94 |
export_dialogue_button_json.click(save_json, study_tutor, file_download, show_progress=True)
|
95 |
export_dialogue_button_txt.click(save_txt, study_tutor, file_download, show_progress=True)
|
96 |
export_dialogue_button_csv.click(save_csv, study_tutor, file_download, show_progress=True)
|
97 |
-
|
|
|
98 |
with gr.Tab("Instructor Only"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
with gr.Blocks():
|
100 |
# testing purpose, change visible to False at deployment
|
101 |
test_secret = gr.Textbox(label="Current secret prompt", value=os.environ.get("SECRET_PROMPT"), visible=True)
|
@@ -105,8 +130,11 @@ with gr.Blocks() as demo:
|
|
105 |
elem_classes="short-height")
|
106 |
instructor_prompt = gr.Textbox(label="Verify your prompt content", visible=True)
|
107 |
file_input.upload(fn=get_instructor_prompt, inputs=file_input, outputs=instructor_prompt)
|
108 |
-
|
|
|
109 |
prompt_submit_btn = gr.Button("Submit")
|
110 |
prompt_submit_btn.click(fn=embed_prompt, inputs=instructor_prompt, outputs=test_secret)
|
|
|
|
|
111 |
|
112 |
demo.queue().launch(server_name='0.0.0.0', server_port=7860)
|
|
|
23 |
self.conversation_memory = []
|
24 |
self.flattened_conversation = ''
|
25 |
|
|
|
26 |
### Chatbot Functions ###
|
27 |
def add_user_message(user_message, chat_tutor):
|
28 |
"""Display user message and update chat history to include it."""
|
|
|
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):
|
42 |
file_path = fileobj.name
|
43 |
f = open(file_path, "r")
|
|
|
48 |
os.environ["SECRET_PROMPT"] = instructor_prompt
|
49 |
return os.environ.get("SECRET_PROMPT")
|
50 |
|
51 |
+
|
52 |
+
### User Interfaces ###
|
53 |
with gr.Blocks() as demo:
|
54 |
+
|
55 |
#initialize tutor (with state)
|
56 |
study_tutor = gr.State(EchoingTutor())
|
57 |
|
58 |
+
# Student interface
|
|
|
|
|
|
|
59 |
with gr.Tab("For Students"):
|
60 |
# Chatbot interface
|
61 |
gr.Markdown("""
|
|
|
95 |
export_dialogue_button_json.click(save_json, study_tutor, file_download, show_progress=True)
|
96 |
export_dialogue_button_txt.click(save_txt, study_tutor, file_download, show_progress=True)
|
97 |
export_dialogue_button_csv.click(save_csv, study_tutor, file_download, show_progress=True)
|
98 |
+
|
99 |
+
# Instructor interface
|
100 |
with gr.Tab("Instructor Only"):
|
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>
|
108 |
+
to sign up for an account and get your personal API key</span>""",
|
109 |
+
elem_classes="textbox_label")
|
110 |
+
api_input = gr.Textbox(show_label=False, type="password", container=False, autofocus=True,
|
111 |
+
placeholder="βββββββββββββββββ", value='')
|
112 |
+
api_input.submit(fn=embed_key, inputs=[api_input, study_tutor], outputs=study_tutor)
|
113 |
+
api_input.blur(fn=embed_key, inputs=[api_input, study_tutor], outputs=study_tutor)
|
114 |
+
|
115 |
+
"""
|
116 |
+
Another way to permanently set the key is to directly go to
|
117 |
+
Settings -> Variables and secrets -> Secrets
|
118 |
+
Then replace OPENAI_API_KEY value with whatever openai key of the instructor.
|
119 |
+
"""
|
120 |
+
# api_input = os.environ.get("OPENAI_API_KEY")
|
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)
|
|
|
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)
|