Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -85,44 +85,6 @@ def get_first_column_values(local_df):
|
|
| 85 |
return []
|
| 86 |
|
| 87 |
|
| 88 |
-
# # File Uploader in sidebar
|
| 89 |
-
|
| 90 |
-
# # Load terms from a CSV file
|
| 91 |
-
# def load_terms(file_input):
|
| 92 |
-
# try:
|
| 93 |
-
# if isinstance(file_input, str):
|
| 94 |
-
# data = pd.read_csv(file_input)
|
| 95 |
-
# else:
|
| 96 |
-
# data = pd.read_csv(io.StringIO(file_input.read().decode('utf-8')))
|
| 97 |
-
# return data
|
| 98 |
-
# except Exception as e:
|
| 99 |
-
# st.error(f"An error occurred while loading the file: {str(e)}")
|
| 100 |
-
# logging.exception(f"Error loading file: {e}")
|
| 101 |
-
|
| 102 |
-
# # Function to create a download link for a file
|
| 103 |
-
# def create_download_link(file_path, file_name):
|
| 104 |
-
# try:
|
| 105 |
-
# with open(file_path, "rb") as file:
|
| 106 |
-
# file_content = file.read()
|
| 107 |
-
# encoded_content = base64.b64encode(file_content).decode("utf-8")
|
| 108 |
-
# download_link = f'<a href="data:file/csv;base64,{encoded_content}" download="{file_name}">Download {file_name}</a>'
|
| 109 |
-
# return download_link
|
| 110 |
-
# except FileNotFoundError:
|
| 111 |
-
# error_message = f"The file {file_name} was not found."
|
| 112 |
-
# st.error(error_message)
|
| 113 |
-
# logging.exception(error_message)
|
| 114 |
-
# except Exception as e:
|
| 115 |
-
# error_message = f"An error occurred: {str(e)}"
|
| 116 |
-
# st.error(error_message)
|
| 117 |
-
# logging.exception(error_message)
|
| 118 |
-
|
| 119 |
-
# # Function to extract the first column values
|
| 120 |
-
# def get_first_column_values(df):
|
| 121 |
-
# if not df.empty:
|
| 122 |
-
# return df.iloc[:, 0].tolist()
|
| 123 |
-
# else:
|
| 124 |
-
# return []
|
| 125 |
-
|
| 126 |
############################################################################################################
|
| 127 |
# Prepare terms (no user file uploader anymore—only config.default_terms_csv)
|
| 128 |
|
|
@@ -130,30 +92,6 @@ terms = load_terms(config.default_terms_csv)
|
|
| 130 |
term_list = get_first_column_values(terms)
|
| 131 |
|
| 132 |
|
| 133 |
-
|
| 134 |
-
# # Download link for the template file
|
| 135 |
-
# template_file_path = config.default_terms_csv
|
| 136 |
-
|
| 137 |
-
# # File Uploader
|
| 138 |
-
# uploaded_file = config.default_terms_csv#st.sidebar.file_uploader(" ", type=["csv"])
|
| 139 |
-
# if uploaded_file is not None:
|
| 140 |
-
# logging.info(f"File uploaded: {uploaded_file.name}")
|
| 141 |
-
# st.session_state.uploaded_file = uploaded_file
|
| 142 |
-
|
| 143 |
-
# # Load terms from the file
|
| 144 |
-
# if 'uploaded_file' in st.session_state and st.session_state.uploaded_file is not None:
|
| 145 |
-
# terms = load_terms(st.session_state.uploaded_file)
|
| 146 |
-
# else:
|
| 147 |
-
# terms = load_terms(template_file_path)
|
| 148 |
-
|
| 149 |
-
# # Extract first column values
|
| 150 |
-
# term_list = get_first_column_values(terms)
|
| 151 |
-
|
| 152 |
-
# # st.sidebar.markdown(create_download_link(template_file_path, "terms.csv"), unsafe_allow_html=True)
|
| 153 |
-
|
| 154 |
-
# # # line break in the sidebar
|
| 155 |
-
# # st.sidebar.markdown('<hr>', unsafe_allow_html=True)
|
| 156 |
-
|
| 157 |
############################################################################################################
|
| 158 |
# Term Selection and session state
|
| 159 |
|
|
@@ -204,12 +142,6 @@ if st.session_state.display_term and st.session_state.selected_term:
|
|
| 204 |
|
| 205 |
with st.expander("INSTRUCTIONS FOR STUDENTS:"):
|
| 206 |
st.markdown(config.instructions)
|
| 207 |
-
# with st.expander("**INSTRUCTORS**: For a look at the current terms file driving the interaction, click here:"):
|
| 208 |
-
# st.markdown("This is the terms.csv file that drives the interaction. You can edit this file to change the terms and context that the chatbot uses. You may add any term or phrase. You may leave the context blank if you prefer or you can add anything relevant that the GPT does not normally know about the term. This may include relevant learning objectives, course examples, notable scientists, assessment dates, syllabus information, etc.")
|
| 209 |
-
# st.table(df)
|
| 210 |
-
# with st.expander("**INSTRUCTORS**: For a look at the prompt driving the chatbot, click here:"):
|
| 211 |
-
# prompt_text = config.term_prompt(st.session_state.selected_term, st.session_state.selected_context, term_list)
|
| 212 |
-
# st.markdown(prompt_text)
|
| 213 |
|
| 214 |
############################################################################################################
|
| 215 |
# ChatGPT
|
|
@@ -337,210 +269,3 @@ with st.sidebar:
|
|
| 337 |
# Using the config objects in your Streamlit app
|
| 338 |
st.markdown(config.app_creation_message, unsafe_allow_html=True)
|
| 339 |
st.markdown(config.app_repo_license_message, unsafe_allow_html=True)
|
| 340 |
-
|
| 341 |
-
# #################################################################
|
| 342 |
-
# # Streamlit app layout
|
| 343 |
-
|
| 344 |
-
# # Set the page to wide or centered mode
|
| 345 |
-
# st.set_page_config(layout="wide")
|
| 346 |
-
|
| 347 |
-
# # Load the terms file into a DataFrame using config.default_terms_csv
|
| 348 |
-
# df = pd.read_csv(config.default_terms_csv)
|
| 349 |
-
|
| 350 |
-
# # Streamlit app layout
|
| 351 |
-
# st.title(config.app_title)
|
| 352 |
-
# st.markdown(config.intro_para)
|
| 353 |
-
# st.caption(config.app_author)
|
| 354 |
-
|
| 355 |
-
# ############################################################################################################
|
| 356 |
-
# # Loading Terms
|
| 357 |
-
|
| 358 |
-
# def load_terms(file_path):
|
| 359 |
-
# """Loads the CSV from the directory."""
|
| 360 |
-
# try:
|
| 361 |
-
# return pd.read_csv(file_path)
|
| 362 |
-
# except Exception as e:
|
| 363 |
-
# st.error(f"An error occurred while loading the file: {str(e)}")
|
| 364 |
-
# logging.exception(f"Error loading file: {e}")
|
| 365 |
-
# return pd.DataFrame()
|
| 366 |
-
|
| 367 |
-
# # Function to extract the first column values
|
| 368 |
-
# def get_first_column_values(local_df):
|
| 369 |
-
# if not local_df.empty:
|
| 370 |
-
# return local_df.iloc[:, 0].tolist()
|
| 371 |
-
# else:
|
| 372 |
-
# return []
|
| 373 |
-
|
| 374 |
-
# ############################################################################################################
|
| 375 |
-
# # Prepare terms (no user file uploader anymore—only config.default_terms_csv)
|
| 376 |
-
|
| 377 |
-
# terms = load_terms(config.default_terms_csv)
|
| 378 |
-
# term_list = get_first_column_values(terms)
|
| 379 |
-
|
| 380 |
-
# ############################################################################################################
|
| 381 |
-
# # Term Selection and session state
|
| 382 |
-
|
| 383 |
-
# # Initialize the session state variables for selected term, context, and display messages
|
| 384 |
-
# if 'selected_term' not in st.session_state:
|
| 385 |
-
# st.session_state.selected_term = None
|
| 386 |
-
# if 'selected_context' not in st.session_state:
|
| 387 |
-
# st.session_state.selected_context = None
|
| 388 |
-
# if 'display_messages' not in st.session_state:
|
| 389 |
-
# st.session_state.display_messages = []
|
| 390 |
-
# if 'display_term' not in st.session_state:
|
| 391 |
-
# st.session_state.display_term = False
|
| 392 |
-
# if 'initial_message_displayed' not in st.session_state:
|
| 393 |
-
# st.session_state.initial_message_displayed = False
|
| 394 |
-
# if 'old_term' not in st.session_state:
|
| 395 |
-
# st.session_state.old_term = None
|
| 396 |
-
|
| 397 |
-
# # Dropdown menu for selecting a term
|
| 398 |
-
# selected_term = st.selectbox('**SELECT FROM THE DROPDOWN MENU**', term_list)
|
| 399 |
-
|
| 400 |
-
# if selected_term:
|
| 401 |
-
# # If a new term is selected, ask the user for input about that term
|
| 402 |
-
# if selected_term != st.session_state.old_term:
|
| 403 |
-
# user_message = (
|
| 404 |
-
# f"What is one thing you know about '{selected_term}'? What do you want to know about it? "
|
| 405 |
-
# "This could include a definition, examples, misconceptions, associations with other course terms, opinions, etc."
|
| 406 |
-
# )
|
| 407 |
-
# st.session_state["display_messages"].append({"role": "user", "content": user_message})
|
| 408 |
-
# st.session_state.old_term = selected_term
|
| 409 |
-
|
| 410 |
-
# selected_context = terms.loc[terms['TERM'] == selected_term, 'CONTEXT'].values[0]
|
| 411 |
-
# st.session_state.selected_term = selected_term
|
| 412 |
-
# st.session_state.selected_context = selected_context
|
| 413 |
-
# st.session_state.display_term = True
|
| 414 |
-
|
| 415 |
-
# # Build the prompt for the model
|
| 416 |
-
# updated_prompt = config.term_prompt(st.session_state.selected_term, st.session_state.selected_context, term_list)
|
| 417 |
-
|
| 418 |
-
# else:
|
| 419 |
-
# st.session_state.old_term = None
|
| 420 |
-
|
| 421 |
-
# # Display the selected term and context
|
| 422 |
-
# if st.session_state.display_term and st.session_state.selected_term:
|
| 423 |
-
# st.header(st.session_state.selected_term)
|
| 424 |
-
|
| 425 |
-
# with st.expander("INSTRUCTIONS FOR STUDENTS:"):
|
| 426 |
-
# st.markdown(config.instructions)
|
| 427 |
-
|
| 428 |
-
# ############################################################################################################
|
| 429 |
-
# # ChatGPT
|
| 430 |
-
# # Initialize the OpenAI client
|
| 431 |
-
# client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
| 432 |
-
|
| 433 |
-
# # Initialize the model in session state if it doesn't exist
|
| 434 |
-
# if "openai_model" not in st.session_state:
|
| 435 |
-
# st.session_state["openai_model"] = config.ai_model
|
| 436 |
-
|
| 437 |
-
# # Ensure the system prompt is set with the latest selected term and context
|
| 438 |
-
# if st.session_state.get('selected_term') and st.session_state.get('selected_context'):
|
| 439 |
-
# updated_prompt = config.term_prompt(
|
| 440 |
-
# st.session_state.selected_term,
|
| 441 |
-
# st.session_state.selected_context,
|
| 442 |
-
# term_list
|
| 443 |
-
# )
|
| 444 |
-
# if st.session_state.display_messages:
|
| 445 |
-
# st.session_state.display_messages[0]["content"] = updated_prompt
|
| 446 |
-
# else:
|
| 447 |
-
# st.session_state.display_messages = [{"role": "system", "content": updated_prompt}]
|
| 448 |
-
|
| 449 |
-
# # Chat input
|
| 450 |
-
# prompt = st.chat_input("What do you know? What do you want to know?")
|
| 451 |
-
|
| 452 |
-
# # Input for new messages
|
| 453 |
-
# if prompt:
|
| 454 |
-
# # If no initial context in display_messages, add it
|
| 455 |
-
# if not st.session_state["display_messages"]:
|
| 456 |
-
# st.session_state["display_messages"].append({"role": "system", "content": updated_prompt})
|
| 457 |
-
# st.session_state["display_messages"].append({"role": "user", "content": prompt})
|
| 458 |
-
|
| 459 |
-
# # Function to reset all chat-related session state
|
| 460 |
-
# def reset_chat_history():
|
| 461 |
-
# st.session_state["display_messages"] = []
|
| 462 |
-
# if 'selected_term' in st.session_state:
|
| 463 |
-
# st.session_state.selected_term = None
|
| 464 |
-
# if 'selected_context' in st.session_state:
|
| 465 |
-
# st.session_state.selected_context = None
|
| 466 |
-
# if 'display_term' in st.session_state:
|
| 467 |
-
# st.session_state.display_term = False
|
| 468 |
-
# st.rerun()
|
| 469 |
-
|
| 470 |
-
# ############################################################################################################
|
| 471 |
-
# # Main chat container
|
| 472 |
-
|
| 473 |
-
# with st.container():
|
| 474 |
-
# # Display chat history in reverse order
|
| 475 |
-
# for message in st.session_state["display_messages"][1:]:
|
| 476 |
-
# if message["role"] == "user":
|
| 477 |
-
# with st.chat_message("user"):
|
| 478 |
-
# st.markdown(message["content"])
|
| 479 |
-
# else:
|
| 480 |
-
# with st.chat_message("assistant"):
|
| 481 |
-
# st.markdown(message["content"])
|
| 482 |
-
|
| 483 |
-
# # Generate assistant response
|
| 484 |
-
# if prompt:
|
| 485 |
-
# with st.chat_message("assistant"):
|
| 486 |
-
# try:
|
| 487 |
-
# stream = client.chat.completions.create(
|
| 488 |
-
# model=st.session_state["openai_model"],
|
| 489 |
-
# messages=[
|
| 490 |
-
# {"role": m["role"], "content": m["content"]}
|
| 491 |
-
# for m in st.session_state["display_messages"]
|
| 492 |
-
# ],
|
| 493 |
-
# stream=True,
|
| 494 |
-
# temperature=config.temperature,
|
| 495 |
-
# max_tokens=config.max_tokens,
|
| 496 |
-
# frequency_penalty=config.frequency_penalty,
|
| 497 |
-
# presence_penalty=config.presence_penalty,
|
| 498 |
-
# )
|
| 499 |
-
# response = st.write_stream(stream)
|
| 500 |
-
# st.session_state["display_messages"].append({"role": "assistant", "content": response})
|
| 501 |
-
# logging.info(f"User prompt: {prompt}")
|
| 502 |
-
# logging.info(f"Assistant response: {response}")
|
| 503 |
-
# except Exception as e:
|
| 504 |
-
# st.error(f"An error occurred: {str(e)}")
|
| 505 |
-
# logging.exception(f"Error generating response: {e}")
|
| 506 |
-
|
| 507 |
-
# # Clear chat history button
|
| 508 |
-
# if st.button("Clear Chat History"):
|
| 509 |
-
# reset_chat_history()
|
| 510 |
-
# logging.info("Chat history cleared")
|
| 511 |
-
|
| 512 |
-
# st.markdown(config.warning_message, unsafe_allow_html=True)
|
| 513 |
-
|
| 514 |
-
# ############################################################################################################
|
| 515 |
-
# # Resources and About Sections in the Sidebar
|
| 516 |
-
|
| 517 |
-
# st.sidebar.title("Resources")
|
| 518 |
-
|
| 519 |
-
# for resource in config.resources:
|
| 520 |
-
# with st.sidebar:
|
| 521 |
-
# with st.expander(resource["title"]):
|
| 522 |
-
# st.markdown(f"Description: {resource['description']}")
|
| 523 |
-
# if "url" in resource:
|
| 524 |
-
# st.markdown(f"[{resource['title']}]({resource['url']})")
|
| 525 |
-
# if "file_path" in resource:
|
| 526 |
-
# file_path = resource["file_path"]
|
| 527 |
-
# if os.path.exists(file_path):
|
| 528 |
-
# with open(file_path, "rb") as file:
|
| 529 |
-
# file_bytes = file.read()
|
| 530 |
-
# with st.spinner(f"Loading {resource['title']}..."):
|
| 531 |
-
# st.download_button(
|
| 532 |
-
# label=resource["title"],
|
| 533 |
-
# data=file_bytes,
|
| 534 |
-
# file_name=os.path.basename(file_path),
|
| 535 |
-
# mime="application/octet-stream",
|
| 536 |
-
# help=resource["description"],
|
| 537 |
-
# )
|
| 538 |
-
# else:
|
| 539 |
-
# st.warning(f"File not found: {file_path}")
|
| 540 |
-
|
| 541 |
-
# with st.sidebar:
|
| 542 |
-
# st.markdown("---")
|
| 543 |
-
# st.title("About")
|
| 544 |
-
# st.markdown(config.app_creation_message, unsafe_allow_html=True)
|
| 545 |
-
# st.markdown(config.app_repo_license_message, unsafe_allow_html=True)
|
| 546 |
-
|
|
|
|
| 85 |
return []
|
| 86 |
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
############################################################################################################
|
| 89 |
# Prepare terms (no user file uploader anymore—only config.default_terms_csv)
|
| 90 |
|
|
|
|
| 92 |
term_list = get_first_column_values(terms)
|
| 93 |
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
############################################################################################################
|
| 96 |
# Term Selection and session state
|
| 97 |
|
|
|
|
| 142 |
|
| 143 |
with st.expander("INSTRUCTIONS FOR STUDENTS:"):
|
| 144 |
st.markdown(config.instructions)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
############################################################################################################
|
| 147 |
# ChatGPT
|
|
|
|
| 269 |
# Using the config objects in your Streamlit app
|
| 270 |
st.markdown(config.app_creation_message, unsafe_allow_html=True)
|
| 271 |
st.markdown(config.app_repo_license_message, unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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