kinely commited on
Commit
3da7b14
·
verified ·
1 Parent(s): 24d7505

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -29
app.py CHANGED
@@ -8,7 +8,7 @@ from email.mime.multipart import MIMEMultipart
8
  import os
9
  import random
10
 
11
- # Load Hugging Face model (e.g., FLAN-T5 or GPT-like model)
12
  @st.cache_resource
13
  def load_model():
14
  return pipeline("text2text-generation", model="google/flan-t5-base")
@@ -43,17 +43,15 @@ def send_email(to_email, subject, body):
43
  try:
44
  sender_email = "your-email@example.com" # Replace with your email
45
  sender_password = "your-email-password" # Replace with your email password
46
- smtp_server = "smtp.gmail.com" # Adjust for your email provider
47
  smtp_port = 587
48
-
49
- # Set up the email
50
  msg = MIMEMultipart()
51
  msg['From'] = sender_email
52
  msg['To'] = to_email
53
  msg['Subject'] = subject
54
  msg.attach(MIMEText(body, 'plain'))
55
-
56
- # Send the email
57
  with smtplib.SMTP(smtp_server, smtp_port) as server:
58
  server.starttls()
59
  server.login(sender_email, sender_password)
@@ -72,51 +70,40 @@ def generate_captcha():
72
  # Streamlit UI
73
  def main():
74
  st.title("Educational Assistant Chatbot")
75
- st.markdown("Welcome! Ask me about academic programs, visa requirements, and more!")
76
 
77
  # Sidebar for Admin Options
78
  st.sidebar.header("Admin Options")
79
  dataset_folder = st.sidebar.text_input("Dataset Folder Path", "./pdf_dataset")
80
  email_to_send = st.sidebar.text_input("Email for Profile Submission", "application@aspireec.pk")
81
 
82
- # Load model and dataset
83
  st.sidebar.markdown("### Data Loading")
84
  if st.sidebar.button("Load Dataset"):
85
  st.session_state.pdf_texts = extract_text_from_pdfs("chatbot")
86
  st.session_state.embedder, st.session_state.embeddings = create_embeddings(st.session_state.pdf_texts)
87
  st.sidebar.success("Dataset Loaded Successfully!")
88
 
89
- # Chat interface
90
- user_query = st.text_input("Your Query:")
91
- if st.button("Ask"):
92
- if "embedder" not in st.session_state or "pdf_texts" not in st.session_state:
93
- st.error("Dataset is not loaded. Please load the dataset in the sidebar.")
94
- else:
95
- relevant_content = get_relevant_content(
96
- user_query, st.session_state.pdf_texts, st.session_state.embedder, st.session_state.embeddings
97
- )
98
- model = load_model()
99
- response = model(f"Question: {user_query} Context: {relevant_content}", max_length=200)
100
- st.success(response[0]['generated_text'])
101
-
102
- # Profile creation with CAPTCHA
103
  st.markdown("### Create a Student Profile")
104
- name = st.text_input("Name:")
105
- email = st.text_input("Email:")
106
- contact_number = st.text_input("Contact Number (Optional):")
107
- study_level = st.selectbox("Level of Study", ["Undergraduate", "Postgraduate", "PhD"])
108
  field_of_interest = st.text_input("Field of Interest:")
109
  career_goal = st.text_area("Career Goals:")
110
  visa_query = st.text_area("Visa Concerns or Questions:")
111
 
112
- # CAPTCHA generation
113
  if "captcha_result" not in st.session_state:
114
  num1, num2, st.session_state.captcha_result = generate_captcha()
115
  st.markdown(f"**CAPTCHA: {num1} + {num2} = ?**")
116
  captcha_input = st.text_input("Enter CAPTCHA Result:")
117
 
118
  if st.button("Submit Profile"):
119
- if int(captcha_input) != st.session_state.captcha_result:
 
 
120
  st.error("Invalid CAPTCHA. Please try again.")
121
  else:
122
  profile = f"""
@@ -128,12 +115,25 @@ def main():
128
  Career Goals: {career_goal}
129
  Visa Queries: {visa_query}
130
  """
131
- # Send profile via email
132
  email_sent = send_email(email_to_send, "New Student Profile Submission", profile)
133
  if email_sent:
134
  st.success(f"Profile submitted successfully to {email_to_send}!")
135
  else:
136
  st.error("Failed to submit the profile. Please try again later.")
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  if __name__ == "__main__":
139
  main()
 
8
  import os
9
  import random
10
 
11
+ # Load Hugging Face model (e.g., FLAN-T5)
12
  @st.cache_resource
13
  def load_model():
14
  return pipeline("text2text-generation", model="google/flan-t5-base")
 
43
  try:
44
  sender_email = "your-email@example.com" # Replace with your email
45
  sender_password = "your-email-password" # Replace with your email password
46
+ smtp_server = "smtp.gmail.com"
47
  smtp_port = 587
48
+
 
49
  msg = MIMEMultipart()
50
  msg['From'] = sender_email
51
  msg['To'] = to_email
52
  msg['Subject'] = subject
53
  msg.attach(MIMEText(body, 'plain'))
54
+
 
55
  with smtplib.SMTP(smtp_server, smtp_port) as server:
56
  server.starttls()
57
  server.login(sender_email, sender_password)
 
70
  # Streamlit UI
71
  def main():
72
  st.title("Educational Assistant Chatbot")
73
+ st.markdown("Welcome! Let's create your student profile to provide tailored guidance.")
74
 
75
  # Sidebar for Admin Options
76
  st.sidebar.header("Admin Options")
77
  dataset_folder = st.sidebar.text_input("Dataset Folder Path", "./pdf_dataset")
78
  email_to_send = st.sidebar.text_input("Email for Profile Submission", "application@aspireec.pk")
79
 
80
+ # Load dataset
81
  st.sidebar.markdown("### Data Loading")
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
  st.sidebar.success("Dataset Loaded Successfully!")
86
 
87
+ # Collect user information
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  st.markdown("### Create a Student Profile")
89
+ name = st.text_input("Name (required):")
90
+ email = st.text_input("Email (required):")
91
+ contact_number = st.text_input("Contact Number (optional):")
92
+ study_level = st.selectbox("Level of Study:", ["Undergraduate", "Postgraduate", "PhD"])
93
  field_of_interest = st.text_input("Field of Interest:")
94
  career_goal = st.text_area("Career Goals:")
95
  visa_query = st.text_area("Visa Concerns or Questions:")
96
 
97
+ # CAPTCHA
98
  if "captcha_result" not in st.session_state:
99
  num1, num2, st.session_state.captcha_result = generate_captcha()
100
  st.markdown(f"**CAPTCHA: {num1} + {num2} = ?**")
101
  captcha_input = st.text_input("Enter CAPTCHA Result:")
102
 
103
  if st.button("Submit Profile"):
104
+ if not name or not email:
105
+ st.error("Name and Email are required fields.")
106
+ elif int(captcha_input) != st.session_state.captcha_result:
107
  st.error("Invalid CAPTCHA. Please try again.")
108
  else:
109
  profile = f"""
 
115
  Career Goals: {career_goal}
116
  Visa Queries: {visa_query}
117
  """
 
118
  email_sent = send_email(email_to_send, "New Student Profile Submission", profile)
119
  if email_sent:
120
  st.success(f"Profile submitted successfully to {email_to_send}!")
121
  else:
122
  st.error("Failed to submit the profile. Please try again later.")
123
 
124
+ # Chat interface
125
+ st.markdown("### Ask a Question")
126
+ user_query = st.text_input("Your Query:")
127
+ if st.button("Ask"):
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()