brunhild217's picture
Update app.py
869c9ac
raw
history blame
5.98 kB
import os
import gradio as gr
import pandas as pd
from functools import partial
from ai_classroom_suite.UIBaseComponents import *
# Testing purpose
class EchoingTutor(SlightlyDelusionalTutor):
def add_user_message(self, user_message):
self.conversation_memory.append([user_message, None])
self.flattened_conversation = self.flattened_conversation + '\n\n' + 'User: ' + user_message
def get_tutor_reply(self, user_message):
# get tutor message
tutor_message = "You said: " + user_message
# add tutor message to conversation memory
self.conversation_memory[-1][1] = tutor_message
self.flattened_conversation = self.flattened_conversation + '\nAI: ' + tutor_message
def forget_conversation(self):
self.conversation_memory = []
self.flattened_conversation = ''
### Chatbot Functions ###
def add_user_message(user_message, chat_tutor):
chat_tutor.add_user_message(user_message)
return chat_tutor.conversation_memory, chat_tutor
def get_tutor_reply(chat_tutor):
chat_tutor.get_tutor_reply(input_kwargs={'question':''})
return gr.update(value="", interactive=True), chat_tutor.conversation_memory, chat_tutor
def get_conversation_history(chat_tutor):
return chat_tutor.conversation_memory, chat_tutor
### Instructor Interface Helper Functions ###
def get_instructor_prompt(fileobj):
file_path = fileobj.name
f = open(file_path, "r")
instructor_prompt = f.read()
return instructor_prompt
def embed_prompt(instructor_prompt):
os.environ["SECRET_PROMPT"] = instructor_prompt
return os.environ.get("SECRET_PROMPT")
### User Interfaces ###
with gr.Blocks() as demo:
#initialize tutor (with state)
study_tutor = gr.State(SlightlyDelusionalTutor())
# Student interface
with gr.Tab("For Students"):
# Chatbot interface
gr.Markdown("""
## Chat with the Model
Description here
""")
with gr.Row(equal_height=True):
with gr.Column(scale=2):
chatbot = gr.Chatbot()
with gr.Row():
user_chat_input = gr.Textbox(label="User input", scale=9)
user_chat_submit = gr.Button("Ask/answer model", scale=1)
user_chat_submit.click(
add_user_message,
[user_chat_input, study_tutor],
[chatbot, study_tutor],
queue=False
).then(
get_tutor_reply,
[study_tutor],
[user_chat_input, chatbot, study_tutor],
queue=True)
# Testing purpose
test_btn = gr.Button("View your chat history")
chat_history = gr.JSON(label = "conversation history")
test_btn.click(get_conversation_history, inputs=[study_tutor], outputs=[chat_history, study_tutor])
# Download conversation history file
with gr.Blocks():
gr.Markdown("""
## Export Your Chat History
Export your chat history as a .json, .txt, or .csv file
""")
with gr.Row():
export_dialogue_button_json = gr.Button("JSON")
export_dialogue_button_txt = gr.Button("TXT")
export_dialogue_button_csv = gr.Button("CSV")
file_download = gr.Files(label="Download here", file_types=['.json', '.txt', '.csv'], type="file", visible=False)
export_dialogue_button_json.click(save_json, study_tutor, file_download, show_progress=True)
export_dialogue_button_txt.click(save_txt, study_tutor, file_download, show_progress=True)
export_dialogue_button_csv.click(save_csv, study_tutor, file_download, show_progress=True)
# Instructor interface
with gr.Tab("Instructor Only"):
# API Authentication functionality
# Instead of ask students to provide key, the key is now provided by the instructor
"""
To permanently set the key, go to Settings -> Variables and secrets -> Secrets,
then replace OPENAI_API_KEY value with whatever openai key of the instructor.
"""
api_input = gr.Textbox(show_label=False, type="password", visible=False, value=os.environ.get("OPENAI_API_KEY"))
# Upload secret prompt functionality
# The instructor will provide a secret prompt/persona to the tutor
with gr.Blocks():
# testing purpose, change visible to False at deployment
test_secret = gr.Textbox(label="Current secret prompt", value=os.environ.get("SECRET_PROMPT"), visible=True)
# Just placeholders components
text_input = gr.TextArea(visible=False)
file_input = gr.File(visible=False)
instructor_input = gr.TextArea(visible=False)
learning_objectives = gr.Textbox(visible=False)
# Upload secret file
instructor_file_upload = gr.File(
label="Load a .txt or .py file", file_types=['.py', '.txt'], type="file", elem_classes="short-height")
# Verify prompt content
instructor_prompt = gr.Textbox(label="Verify your prompt content", visible=True)
instructor_file_upload.upload(fn=get_instructor_prompt, inputs=file_input, outputs=instructor_prompt)
# Set the secret prompt in this session and embed it to the study tutor
prompt_submit_btn = gr.Button("Submit")
prompt_submit_btn.click(
fn=embed_prompt, inputs=[instructor_prompt], outputs=[test_secret]
).then(
fn=create_reference_store,
inputs=[study_tutor, prompt_submit_btn, instructor_prompt, file_input, instructor_input, api_input, learning_objectives],
outputs=[study_tutor, prompt_submit_btn]
)
# TODO: may need a way to set the secret prompt permanently in settings/secret
demo.queue().launch(server_name='0.0.0.0', server_port=7860)