brunhild217 commited on
Commit
b1cc84d
·
1 Parent(s): 10dd907

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +165 -0
app.py ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import pandas as pd
4
+ from functools import partial
5
+ from ai_classroom_suite.MediaVectorStores import *
6
+ from ai_classroom_suite.UIBaseComponents import *
7
+
8
+ # default folder path
9
+ folder_path = "context_files"
10
+ # default output file name
11
+ out_file_name = "vector_store.txt"
12
+
13
+ # Check if vector store file already exist on disk
14
+ def vector_store_file_exist():
15
+ # Get all files in the folder
16
+ files = os.listdir(folder_path)
17
+ # Check if output file already exist in this folder
18
+ return (out_file_name in files)
19
+
20
+ # Helper function to get all files' paths from a folder
21
+ # Return a list of file paths except for README.txt and vector_store.txt (if exist)
22
+ def get_filepaths_from_folder(folder_path):
23
+ # Store the paths of files
24
+ filepath_list = []
25
+
26
+ # Check if the specified folder exists
27
+ if not os.path.exists(folder_path):
28
+ print(f"Folder '{folder_path}' does not exist.")
29
+ return filepath_list
30
+
31
+ # Get all the files in the folder
32
+ files = os.listdir(folder_path)
33
+
34
+ for file_name in files:
35
+ # Excluding README.txt and vector_store.txt
36
+ if file_name != "README.txt" and file_name != "vector_store.txt":
37
+ # Get the file path for each item
38
+ file_path = os.path.join(folder_path, file_name)
39
+ # Check if the item is a file and not a subdirectory
40
+ if os.path.isfile(file_path):
41
+ filepath_list.append(file_path)
42
+
43
+ return filepath_list
44
+
45
+ # Helper function to write content of files in a folder to output file
46
+ def write_vector_store_to_file(out_file_name):
47
+ # If vector_store.txt already exist, return nothing
48
+ if vector_store_file_exist():
49
+ return gr.File(value=out_file_name, visible=False)
50
+
51
+ # Only try to create the vector store if vector_store.txt doesn't exist
52
+ else:
53
+ # Call the function to read files (excluding README.txt and vector_store.txt) pathes
54
+ filepath_list = get_filepaths_from_folder(folder_path)
55
+ # Extract the text out from files
56
+ files_content = files_to_text(filepath_list, chunk_size=100, chunk_overlap=20)
57
+
58
+ # Write the vector_store onto the output file
59
+ with open(out_file_name, "w") as f:
60
+ for i in range(len(files_content)):
61
+ item = str(files_content[i]) + "\n"
62
+ f.write(item)
63
+
64
+ # Show the downlodable vector store file and give instruction on upload the vector store file to disk (on HuggingFace)
65
+ return gr.File(title="Download your vector store file and upload it into the context_files folder under Files",
66
+ value=out_file_name, visible=True)
67
+
68
+ # overwrites the original method since we don't deal with any vector stores display here
69
+ def get_tutor_reply(chat_tutor):
70
+ chat_tutor.get_tutor_reply()
71
+ return gr.update(value="", interactive=True), chat_tutor.conversation_memory, chat_tutor
72
+
73
+ def get_conversation_history(chat_tutor):
74
+ return chat_tutor.conversation_memory, chat_tutor
75
+
76
+ # To show the loading process on the button when creating vector store file
77
+ def creating_vs_button(obj_in):
78
+ return gr.update(interactive=False, value='Creating Vector Store file...')
79
+ # To show the loading process on the button when initializing tutor
80
+ def initializing_tutor_button(obj_in):
81
+ return gr.update(interactive=False, value='Initializing Tutor...')
82
+
83
+
84
+ with gr.Blocks() as ReadingQuiz:
85
+ #initialize tutor (with state)
86
+ study_tutor = gr.State(SlightlyDelusionalTutor())
87
+
88
+ # Student chatbot interface
89
+ gr.Markdown("""
90
+ ## Chat with the Model
91
+ Description here
92
+ """)
93
+
94
+ # Instead of ask students to provide key, the key is now provided by the instructor.
95
+ api_input = gr.Textbox(show_label=False, type="password", visible=False, value=os.environ.get("OPENAI_API_KEY"))
96
+
97
+ # The instructor will provide a secret prompt/persona to the tutor
98
+ instructor_prompt = gr.Textbox(label="Verify your prompt content", value = os.environ.get("SECRET_PROMPT"), visible=False)
99
+
100
+ # Show input files
101
+ file_input = gr.File(label="Reading materials", value=get_filepaths_from_folder(folder_path), visible=True)
102
+
103
+ # Show output file for vector store when needed
104
+ vs_file_name = gr.Text(visible=False, value=out_file_name)
105
+ file_output = gr.File(visible=False)
106
+
107
+ # Placeholders components
108
+ text_input_none = gr.Textbox(visible=False)
109
+ file_input_none = gr.File(visible=False)
110
+ instructor_input_none = gr.TextArea(visible=False)
111
+ learning_objectives_none = gr.Textbox(visible=False)
112
+
113
+ # Set the secret prompt in this session and embed it to the study tutor
114
+ vs_build_button = gr.Button("Initialize Tutor")
115
+ vs_build_button.click(
116
+ fn=creating_vs_button, inputs=vs_build_button, outputs=vs_build_button
117
+ ).then(
118
+ fn=write_vector_store_to_file, inputs=[vs_file_name], outputs=[file_output]
119
+ ).then(
120
+ fn=initializing_tutor_button, inputs=[vs_build_button], outputs=[vs_build_button]
121
+ ).then(
122
+ fn=create_reference_store,
123
+ inputs=[study_tutor, vs_build_button, instructor_prompt, file_output, instructor_input_none, api_input, learning_objectives_none],
124
+ outputs=[study_tutor, vs_build_button]
125
+ )
126
+
127
+ with gr.Row(equal_height=True):
128
+ with gr.Column(scale=2):
129
+ chatbot = gr.Chatbot()
130
+ with gr.Row():
131
+ user_chat_input = gr.Textbox(label="User input", scale=9)
132
+ user_chat_submit = gr.Button("Ask/answer model", scale=1)
133
+
134
+ # First add user's message to the conversation history
135
+ # Then get reply from the tutor and add that to the conversation history
136
+ user_chat_submit.click(
137
+ fn = add_user_message, inputs = [user_chat_input, study_tutor], outputs = [user_chat_input, chatbot, study_tutor], queue=False
138
+ ).then(
139
+ fn = get_tutor_reply, inputs = [study_tutor], outputs = [user_chat_input, chatbot, study_tutor], queue=True
140
+ )
141
+ # User can also press "Enter" on keyboard to submit a message
142
+ user_chat_input.submit(
143
+ fn = add_user_message, inputs = [user_chat_input, study_tutor], outputs = [user_chat_input, chatbot, study_tutor], queue=False
144
+ ).then(
145
+ fn = get_tutor_reply, inputs = [study_tutor], outputs = [user_chat_input, chatbot, study_tutor], queue=True
146
+ )
147
+
148
+ # Download conversation history file
149
+ with gr.Blocks():
150
+ gr.Markdown("""
151
+ ## Export Your Chat History
152
+ Export your chat history as a .json, .txt, or .csv file
153
+ """)
154
+ with gr.Row():
155
+ export_dialogue_button_json = gr.Button("JSON")
156
+ export_dialogue_button_txt = gr.Button("TXT")
157
+ export_dialogue_button_csv = gr.Button("CSV")
158
+
159
+ file_download = gr.Files(label="Download here", file_types=['.json', '.txt', '.csv'], type="file", visible=False)
160
+
161
+ export_dialogue_button_json.click(save_json, study_tutor, file_download, show_progress=True)
162
+ export_dialogue_button_txt.click(save_txt, study_tutor, file_download, show_progress=True)
163
+ export_dialogue_button_csv.click(save_csv, study_tutor, file_download, show_progress=True)
164
+
165
+ ReadingQuiz.queue().launch(server_name='0.0.0.0', server_port=7860)