Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -72,68 +72,72 @@ def main():
|
|
| 72 |
st.title("Educational Assistant Chatbot")
|
| 73 |
st.markdown("Welcome! Let's create your student profile to provide tailored guidance.")
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
if st.sidebar.button("Load Dataset"):
|
| 83 |
-
st.session_state.pdf_texts = extract_text_from_pdfs("chatbot")
|
| 84 |
st.session_state.embedder, st.session_state.embeddings = create_embeddings(st.session_state.pdf_texts)
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
else:
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
if "embedder" not in st.session_state or "pdf_texts" not in st.session_state:
|
| 129 |
-
st.error("Dataset is not loaded. Please load the dataset in the sidebar.")
|
| 130 |
-
else:
|
| 131 |
-
relevant_content = get_relevant_content(
|
| 132 |
-
user_query, st.session_state.pdf_texts, st.session_state.embedder, st.session_state.embeddings
|
| 133 |
-
)
|
| 134 |
-
model = load_model()
|
| 135 |
-
response = model(f"Question: {user_query} Context: {relevant_content}", max_length=200)
|
| 136 |
-
st.success(response[0]['generated_text'])
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|
| 139 |
main()
|
|
|
|
| 72 |
st.title("Educational Assistant Chatbot")
|
| 73 |
st.markdown("Welcome! Let's create your student profile to provide tailored guidance.")
|
| 74 |
|
| 75 |
+
# Hardcoded dataset folder and email
|
| 76 |
+
dataset_folder = "./pdf_dataset"
|
| 77 |
+
email_to_send = "application@aspireec.pk"
|
| 78 |
+
|
| 79 |
+
# Load dataset on startup
|
| 80 |
+
if "pdf_texts" not in st.session_state:
|
| 81 |
+
st.session_state.pdf_texts = extract_text_from_pdfs(dataset_folder)
|
|
|
|
|
|
|
| 82 |
st.session_state.embedder, st.session_state.embeddings = create_embeddings(st.session_state.pdf_texts)
|
| 83 |
+
|
| 84 |
+
# Initialize session state for profile submission
|
| 85 |
+
if "profile_submitted" not in st.session_state:
|
| 86 |
+
st.session_state.profile_submitted = False
|
| 87 |
+
|
| 88 |
+
# Show profile creation form only if profile is not submitted
|
| 89 |
+
if not st.session_state.profile_submitted:
|
| 90 |
+
st.markdown("### Create a Student Profile")
|
| 91 |
+
name = st.text_input("Name (required):")
|
| 92 |
+
email = st.text_input("Email (required):")
|
| 93 |
+
contact_number = st.text_input("Contact Number (optional):")
|
| 94 |
+
study_level = st.selectbox("Level of Study:", ["Undergraduate", "Postgraduate", "PhD"])
|
| 95 |
+
field_of_interest = st.text_input("Field of Interest:")
|
| 96 |
+
career_goal = st.text_area("Career Goals:")
|
| 97 |
+
visa_query = st.text_area("Visa Concerns or Questions:")
|
| 98 |
+
|
| 99 |
+
# CAPTCHA
|
| 100 |
+
if "captcha_result" not in st.session_state:
|
| 101 |
+
num1, num2, st.session_state.captcha_result = generate_captcha()
|
| 102 |
+
st.markdown(f"**CAPTCHA: {num1} + {num2} = ?**")
|
| 103 |
+
captcha_input = st.text_input("Enter CAPTCHA Result:")
|
| 104 |
+
|
| 105 |
+
if st.button("Submit Profile"):
|
| 106 |
+
if not name or not email:
|
| 107 |
+
st.error("Name and Email are required fields.")
|
| 108 |
+
elif int(captcha_input) != st.session_state.captcha_result:
|
| 109 |
+
st.error("Invalid CAPTCHA. Please try again.")
|
| 110 |
+
else:
|
| 111 |
+
profile = f"""
|
| 112 |
+
Name: {name}
|
| 113 |
+
Email: {email}
|
| 114 |
+
Contact Number: {contact_number}
|
| 115 |
+
Level of Study: {study_level}
|
| 116 |
+
Field of Interest: {field_of_interest}
|
| 117 |
+
Career Goals: {career_goal}
|
| 118 |
+
Visa Queries: {visa_query}
|
| 119 |
+
"""
|
| 120 |
+
email_sent = send_email(email_to_send, "New Student Profile Submission", profile)
|
| 121 |
+
if email_sent:
|
| 122 |
+
st.session_state.profile_submitted = True
|
| 123 |
+
st.success(f"Profile submitted successfully to {email_to_send}!")
|
| 124 |
+
else:
|
| 125 |
+
st.error("Failed to submit the profile. Please try again later.")
|
| 126 |
+
|
| 127 |
+
# Show question input only if profile is submitted
|
| 128 |
+
if st.session_state.profile_submitted:
|
| 129 |
+
st.markdown("### Ask a Question")
|
| 130 |
+
user_query = st.text_input("Your Query:")
|
| 131 |
+
if st.button("Ask"):
|
| 132 |
+
if "embedder" not in st.session_state or "pdf_texts" not in st.session_state:
|
| 133 |
+
st.error("Dataset is not loaded. Please restart the application.")
|
| 134 |
else:
|
| 135 |
+
relevant_content = get_relevant_content(
|
| 136 |
+
user_query, st.session_state.pdf_texts, st.session_state.embedder, st.session_state.embeddings
|
| 137 |
+
)
|
| 138 |
+
model = load_model()
|
| 139 |
+
response = model(f"Question: {user_query} Context: {relevant_content}", max_length=200)
|
| 140 |
+
st.success(response[0]['generated_text'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
main()
|