Spaces:
Sleeping
Sleeping
Commit
·
95f55b3
1
Parent(s):
19ca13a
修改流程顺序,看看效果
Browse files
app.py
CHANGED
|
@@ -210,7 +210,7 @@ def app():
|
|
| 210 |
with st.sidebar:
|
| 211 |
st.image("https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231021212525.png")
|
| 212 |
added_files = st.file_uploader('Upload .md file', type=['.md'], accept_multiple_files=True)
|
| 213 |
-
num_lessons = st.slider('How many lessons do you want this course to have?', min_value=5, max_value=
|
| 214 |
language = 'English'
|
| 215 |
Chinese = st.checkbox('Output in Chinese')
|
| 216 |
if Chinese:
|
|
@@ -218,87 +218,88 @@ def app():
|
|
| 218 |
btn = st.button('submit')
|
| 219 |
|
| 220 |
|
| 221 |
-
col1, col2 = st.columns([0.6,0.4]
|
| 222 |
|
| 223 |
if btn:
|
| 224 |
temp_file_paths = []
|
| 225 |
-
|
| 226 |
for added_file in added_files:
|
| 227 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as tmp:
|
| 228 |
tmp.write(added_file.getvalue())
|
| 229 |
tmp_path = tmp.name
|
| 230 |
temp_file_paths.append(tmp_path)
|
| 231 |
-
|
| 232 |
|
| 233 |
-
|
| 234 |
-
course_outline_list = courseOutlineGenerating(temp_file_paths, num_lessons, language)
|
| 235 |
-
col1.outline_generating_state.text("Generating Course Oueline...Done")
|
| 236 |
-
|
| 237 |
-
#把课程大纲打印出来
|
| 238 |
-
course_outline_string = ''
|
| 239 |
-
lessons_count = 0
|
| 240 |
-
for outline in course_outline_list:
|
| 241 |
-
lessons_count += 1
|
| 242 |
-
course_outline_string += f"{lessons_count}." + outline[0] + '\n'
|
| 243 |
-
course_outline_string += '\n' + outline[1] + '\n\n'
|
| 244 |
-
#time.sleep(1)
|
| 245 |
-
with col1.st.expander("Check the course outline", expanded=False):
|
| 246 |
-
st.write(course_outline_string)
|
| 247 |
-
|
| 248 |
-
col1.vdb_state = st.text("Constructing vector database from provided materials...")
|
| 249 |
embeddings_df, faiss_index = constructVDB(temp_file_paths)
|
| 250 |
-
|
| 251 |
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
prompt = decorate_user_question(user_question, retrieved_chunks_for_user)
|
| 286 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 287 |
-
with st.chat_message("user"):
|
| 288 |
-
st.markdown(user_question)
|
| 289 |
-
# Display assistant response in chat message container
|
| 290 |
with st.chat_message("assistant"):
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
|
| 304 |
if __name__ == "__main__":
|
|
|
|
| 210 |
with st.sidebar:
|
| 211 |
st.image("https://siyuan-harry.oss-cn-beijing.aliyuncs.com/oss://siyuan-harry/20231021212525.png")
|
| 212 |
added_files = st.file_uploader('Upload .md file', type=['.md'], accept_multiple_files=True)
|
| 213 |
+
num_lessons = st.slider('How many lessons do you want this course to have?', min_value=5, max_value=19, value=10, step=1)
|
| 214 |
language = 'English'
|
| 215 |
Chinese = st.checkbox('Output in Chinese')
|
| 216 |
if Chinese:
|
|
|
|
| 218 |
btn = st.button('submit')
|
| 219 |
|
| 220 |
|
| 221 |
+
col1, col2 = st.columns([0.6,0.4])
|
| 222 |
|
| 223 |
if btn:
|
| 224 |
temp_file_paths = []
|
| 225 |
+
file_proc_state = st.text("Processing file...")
|
| 226 |
for added_file in added_files:
|
| 227 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as tmp:
|
| 228 |
tmp.write(added_file.getvalue())
|
| 229 |
tmp_path = tmp.name
|
| 230 |
temp_file_paths.append(tmp_path)
|
| 231 |
+
file_proc_state.text("Processing file...Done")
|
| 232 |
|
| 233 |
+
vdb_state = st.text("Constructing vector database from provided materials...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
embeddings_df, faiss_index = constructVDB(temp_file_paths)
|
| 235 |
+
vdb_state.text("Constructing vector database from provided materials...Done")
|
| 236 |
|
| 237 |
+
outline_generating_state = st.text("Generating Course Oueline...")
|
| 238 |
+
course_outline_list = courseOutlineGenerating(temp_file_paths, num_lessons, language)
|
| 239 |
+
outline_generating_state.text("Generating Course Oueline...Done")
|
| 240 |
+
|
| 241 |
+
with col1:
|
| 242 |
+
#把课程大纲打印出来
|
| 243 |
+
course_outline_string = ''
|
| 244 |
+
lessons_count = 0
|
| 245 |
+
for outline in course_outline_list:
|
| 246 |
+
lessons_count += 1
|
| 247 |
+
course_outline_string += f"{lessons_count}." + outline[0] + '\n'
|
| 248 |
+
course_outline_string += '\n' + outline[1] + '\n\n'
|
| 249 |
+
#time.sleep(1)
|
| 250 |
+
with st.expander("Check the course outline", expanded=False):
|
| 251 |
+
st.write(course_outline_string)
|
| 252 |
+
|
| 253 |
+
count_generating_content = 0
|
| 254 |
+
for lesson in course_outline_list:
|
| 255 |
+
count_generating_content += 1
|
| 256 |
+
content_generating_state = st.text(f"Writing content for lesson {count_generating_content}...")
|
| 257 |
+
retrievedChunksList = searchVDB(lesson, embeddings_df, faiss_index)
|
| 258 |
+
courseContent = generateCourse(lesson, retrievedChunksList, language)
|
| 259 |
+
content_generating_state.text(f"Writing content for lesson {count_generating_content}...Done")
|
| 260 |
+
#st.text_area("Course Content", value=courseContent)
|
| 261 |
+
with st.expander(f"Learn the lesson {count_generating_content} ", expanded=False):
|
| 262 |
+
st.markdown(courseContent)
|
| 263 |
+
|
| 264 |
+
user_question = st.chat_input("Enter your questions when learning...")
|
| 265 |
+
|
| 266 |
+
with col2:
|
| 267 |
+
st.caption(''':blue[AI Assistant]: Ask this TA any questions related to this course and get direct answers. :sunglasses:''')
|
| 268 |
+
# Set a default model
|
| 269 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
with st.chat_message("assistant"):
|
| 271 |
+
st.write("Hello👋, how can I help you today? 😄")
|
| 272 |
+
if "openai_model" not in st.session_state:
|
| 273 |
+
st.session_state["openai_model"] = "gpt-3.5-turbo"
|
| 274 |
+
|
| 275 |
+
# Initialize chat history
|
| 276 |
+
if "messages" not in st.session_state:
|
| 277 |
+
st.session_state.messages = []
|
| 278 |
+
|
| 279 |
+
# Display chat messages from history on app rerun
|
| 280 |
+
for message in st.session_state.messages:
|
| 281 |
+
with st.chat_message(message["role"]):
|
| 282 |
+
st.markdown(message["content"])
|
| 283 |
+
#这里的session.state就是保存了这个对话会话的一些基本信息和设置
|
| 284 |
+
if user_question:
|
| 285 |
+
retrieved_chunks_for_user = searchVDB(user_question, embeddings_df, faiss_index)
|
| 286 |
+
prompt = decorate_user_question(user_question, retrieved_chunks_for_user)
|
| 287 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 288 |
+
with st.chat_message("user"):
|
| 289 |
+
st.markdown(user_question)
|
| 290 |
+
# Display assistant response in chat message container
|
| 291 |
+
with st.chat_message("assistant"):
|
| 292 |
+
message_placeholder = st.empty()
|
| 293 |
+
full_response = ""
|
| 294 |
+
for response in openai.ChatCompletion.create(
|
| 295 |
+
model=st.session_state["openai_model"],
|
| 296 |
+
messages=[{"role": m["role"], "content": m["content"]} for m in st.session_state.messages],
|
| 297 |
+
stream=True,
|
| 298 |
+
):
|
| 299 |
+
full_response += response.choices[0].delta.get("content", "")
|
| 300 |
+
message_placeholder.markdown(full_response + "▌")
|
| 301 |
+
message_placeholder.markdown(full_response)
|
| 302 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 303 |
|
| 304 |
|
| 305 |
if __name__ == "__main__":
|