yuki-816 commited on
Commit
0ee6881
·
1 Parent(s): dd1a8bd

Update `generate_response` function

Browse files
Files changed (1) hide show
  1. streamlit_app.py +69 -63
streamlit_app.py CHANGED
@@ -32,6 +32,7 @@ class Validator:
32
  return True
33
  return False
34
 
 
35
  def get_user_data(user):
36
  data = db.fetch().items
37
  for person in data:
@@ -39,29 +40,32 @@ def get_user_data(user):
39
  return person
40
  return None
41
 
 
42
  def user_history(time, text, ):
43
  pass
44
 
 
45
  def update_questionnaire_response(user_response, username):
46
  db.update({"questionnaire_response": user_response}, key=username)
47
  st.success("Your responses have been recorded. Thank you!")
48
 
 
49
  def generate_responses(text, chat_model="gpt-3.5-turbo", paper_title="", sci_familiarity="", tech_usage="",
50
- social_media_usage="", read_news="", primary_language="", other_languages=""):
51
  # Incorporating the parameters into the context
52
  text = text[:2048]
53
  user_context = f"""
54
- The user has a familiarity level of {sci_familiarity} with scientific concepts. They often use {tech_usage} and
55
- their social media usage is {social_media_usage}. They {read_news} read or watch the news. The primary language
56
- spoken at their home is {primary_language} and they also speak {other_languages}.
57
  """
58
 
59
- print(user_context)
60
 
61
  # Prompt template
62
  prompt_template = f"""
63
- Here's the abstract of a paper titled {paper_title}. {text}
64
- {user_context}
 
65
  Generate a plain language summary that summarizes the abstract. While creating this Plain Language Summary, please keep the following must-have elements in mind:
66
  - Ensure fidelity to the original source.
67
  - Use clear and simple language, avoiding jargon.
@@ -139,7 +143,6 @@ if page == 'Login':
139
  authenticator.logout('Logout', 'sidebar', key='unique_key')
140
  # Fetch user data from the database
141
  user_data = get_user_data(username)
142
-
143
  if page == "Generate Plain Language Summary":
144
 
145
  st.title("Generate Plain Language Summary")
@@ -191,12 +194,15 @@ if page == 'Login':
191
  st.session_state.draft_response_content = ""
192
 
193
  draft_response = ''
194
- user_response = user_data['questionnaire_response'] if user_data else defaultdict(lambda : '')
195
 
 
 
 
 
 
196
  # Check if the "Submit" button is clicked
197
  if st.button("Submit"):
198
- submit_text = ''
199
-
200
  if abstract_text == '' and uploaded_file == None:
201
  st.warning('Please paste Abstract or upload a file.', icon="⚠️")
202
 
@@ -206,10 +212,6 @@ if page == 'Login':
206
  else:
207
  st.warning('Please fill in api-key in Setup.', icon="⚠️")
208
 
209
- if entire_text:
210
- submit_text = entire_text
211
- else:
212
- submit_text = abstract_text
213
  if chat_mdl is not None and submit_text:
214
  st.session_state.draft_response_content = generate_responses(
215
  text=submit_text,
@@ -218,13 +220,10 @@ if page == 'Login':
218
  'paper_familiarity'],
219
  tech_usage=user_response[
220
  'tech_usage'],
221
- social_media_usage='Unknown',
222
  read_news=user_response[
223
  'news_read'],
224
- primary_language=user_response[
225
- 'primary_language'],
226
- other_languages=user_response[
227
- 'other_languages'], )
228
 
229
  container = st.empty()
230
  # Output from function
@@ -246,13 +245,11 @@ if page == 'Login':
246
  'paper_familiarity'],
247
  tech_usage=user_response[
248
  'tech_usage'],
249
- social_media_usage='Unknown',
250
  read_news=user_response[
251
  'news_read'],
252
- primary_language=user_response[
253
- 'primary_language'],
254
- other_languages=user_response[
255
- 'other_languages'], )
256
  container.empty()
257
  container.text_area(label="Plain Language Summary", value=st.session_state.draft_response_content,
258
  height=350)
@@ -274,7 +271,8 @@ if page == 'Login':
274
  # Update button
275
  if st.button("Update"):
276
  db.put(
277
- {"key": username, "api": key.encrypt(bytes(api_input, 'utf-8')).decode(), "questionnaire_response": questionnaire_response})
 
278
  st.success('Updating successfully!')
279
  elif page == "Questionnaire":
280
  survey = survey(username)
@@ -283,7 +281,8 @@ if page == 'Login':
283
  # print(st.session_state['questionnaire_response'])
284
  page_number = 10
285
  survey_pages = survey.pages(page_number,
286
- on_submit=lambda: update_questionnaire_response(st.session_state['questionnaire_response'], username))
 
287
  # st.session_state["__streamlit-survey-data__Pages_"] = survey_pages.current
288
  st.progress((survey_pages.current + 1) / page_number)
289
  with survey_pages:
@@ -300,31 +299,41 @@ if page == 'Login':
300
  st.session_state['questionnaire_response']['level_education'] = level_education
301
  elif survey_pages.current == 1:
302
  st.write("#### What domains are you most interested in?")
303
- survey.checkbox('Global Studies')
304
- survey.checkbox('Arts')
305
- survey.checkbox('Business & Economics')
306
- survey.checkbox('History')
307
- survey.checkbox('Humanities')
308
- survey.checkbox('Law')
309
- survey.checkbox('Medicine and Health')
310
- survey.checkbox('Science - Biology')
311
- survey.checkbox('Science - Chemistry')
312
- survey.checkbox('Science - Environmental Science')
313
- survey.checkbox('Science - Physics')
314
- survey.checkbox('Mathematics')
315
- survey.checkbox('Engineering')
316
- survey.checkbox('Social Sciences')
317
  elif survey_pages.current == 2:
318
- survey.text_area("#### How did you come across this paper?")
 
319
  elif survey_pages.current == 3:
320
- survey.text_area("#### For what purpose are you reading this paper?")
 
321
  elif survey_pages.current == 4:
322
  st.write("#### What information do you want to get out of this paper?")
323
- survey.checkbox("Main findings and conclusions")
324
- survey.checkbox('Methodology and experimental design')
325
- survey.checkbox('Data and statistical analysis')
326
- survey.checkbox('Limitations or gaps in the research')
327
- survey.text_input('Other aspects:')
 
 
 
 
 
 
 
 
 
 
328
  elif survey_pages.current == 5:
329
  st.write("#### What is your familiarity with the concepts of the paper?")
330
  st.markdown('''
@@ -337,7 +346,7 @@ if page == 'Login':
337
  paper_familiarity = survey.select_slider(
338
  label="paper_familiarity",
339
  options=['No Familiarity', 'Limited Familiarity', 'Moderate Familiarity',
340
- 'Good Familiarity', 'Expert'],
341
  # min_value=1,
342
  # max_value=5,
343
  label_visibility="collapsed",
@@ -382,25 +391,22 @@ if page == 'Login':
382
  label_visibility="collapsed",
383
  horizontal=True,
384
  )
 
385
  elif survey_pages.current == 9:
386
  st.write("#### What is the primary language spoken in your home? (click from the list and others)")
387
- english = survey.checkbox("English")
388
- spanish = survey.checkbox('Spanish')
389
- primary_language = []
 
 
 
 
 
390
  other_language = survey.text_input('Other')
391
 
392
- if english:
393
- primary_language.append('English')
394
- if spanish:
395
- primary_language.append('Spanish')
396
  if other_language:
397
- st.session_state['other_languages'] = other_language
398
-
399
- st.session_state['questionnaire_response']['primary_language'] = primary_language
400
- st.session_state['questionnaire_response']['other_languages'] = other_language
401
- # questionnaire_json = survey.to_json()
402
- # # print(questionnaire_json)
403
- # db.update({"questionnaire_response": questionnaire_json}, key=username)
404
 
405
  elif authentication_status is False:
406
  st.error('Username or Password is incorrect', icon="⚠️")
 
32
  return True
33
  return False
34
 
35
+
36
  def get_user_data(user):
37
  data = db.fetch().items
38
  for person in data:
 
40
  return person
41
  return None
42
 
43
+
44
  def user_history(time, text, ):
45
  pass
46
 
47
+
48
  def update_questionnaire_response(user_response, username):
49
  db.update({"questionnaire_response": user_response}, key=username)
50
  st.success("Your responses have been recorded. Thank you!")
51
 
52
+
53
  def generate_responses(text, chat_model="gpt-3.5-turbo", paper_title="", sci_familiarity="", tech_usage="",
54
+ read_news="", language_spoken="", additional_requirements="None"):
55
  # Incorporating the parameters into the context
56
  text = text[:2048]
57
  user_context = f"""
58
+ The user has {sci_familiarity} with scientific concepts. He/She {tech_usage} uses technology products.
59
+ He/She {read_news} read or watch the news. The primary language spoken at his/her home is(are) {language_spoken}.
 
60
  """
61
 
62
+ # print(user_context)
63
 
64
  # Prompt template
65
  prompt_template = f"""
66
+ Here's the abstract of a paper (titled) {paper_title}: {text}.
67
+ Considering the user's information: {user_context}.
68
+ And user's additional requirements: {additional_requirements}.
69
  Generate a plain language summary that summarizes the abstract. While creating this Plain Language Summary, please keep the following must-have elements in mind:
70
  - Ensure fidelity to the original source.
71
  - Use clear and simple language, avoiding jargon.
 
143
  authenticator.logout('Logout', 'sidebar', key='unique_key')
144
  # Fetch user data from the database
145
  user_data = get_user_data(username)
 
146
  if page == "Generate Plain Language Summary":
147
 
148
  st.title("Generate Plain Language Summary")
 
194
  st.session_state.draft_response_content = ""
195
 
196
  draft_response = ''
197
+ user_response = user_data['questionnaire_response'] if user_data else defaultdict(lambda: '')
198
 
199
+ submit_text = ''
200
+ if entire_text:
201
+ submit_text = entire_text
202
+ else:
203
+ submit_text = abstract_text
204
  # Check if the "Submit" button is clicked
205
  if st.button("Submit"):
 
 
206
  if abstract_text == '' and uploaded_file == None:
207
  st.warning('Please paste Abstract or upload a file.', icon="⚠️")
208
 
 
212
  else:
213
  st.warning('Please fill in api-key in Setup.', icon="⚠️")
214
 
 
 
 
 
215
  if chat_mdl is not None and submit_text:
216
  st.session_state.draft_response_content = generate_responses(
217
  text=submit_text,
 
220
  'paper_familiarity'],
221
  tech_usage=user_response[
222
  'tech_usage'],
 
223
  read_news=user_response[
224
  'news_read'],
225
+ language_spoken=user_response[
226
+ 'language_spoken'],)
 
 
227
 
228
  container = st.empty()
229
  # Output from function
 
245
  'paper_familiarity'],
246
  tech_usage=user_response[
247
  'tech_usage'],
 
248
  read_news=user_response[
249
  'news_read'],
250
+ language_spoken=user_response[
251
+ 'language_spoken'],
252
+ additional_requirements=additional_prompt)
 
253
  container.empty()
254
  container.text_area(label="Plain Language Summary", value=st.session_state.draft_response_content,
255
  height=350)
 
271
  # Update button
272
  if st.button("Update"):
273
  db.put(
274
+ {"key": username, "api": key.encrypt(bytes(api_input, 'utf-8')).decode(),
275
+ "questionnaire_response": questionnaire_response})
276
  st.success('Updating successfully!')
277
  elif page == "Questionnaire":
278
  survey = survey(username)
 
281
  # print(st.session_state['questionnaire_response'])
282
  page_number = 10
283
  survey_pages = survey.pages(page_number,
284
+ on_submit=lambda: update_questionnaire_response(
285
+ st.session_state['questionnaire_response'], username))
286
  # st.session_state["__streamlit-survey-data__Pages_"] = survey_pages.current
287
  st.progress((survey_pages.current + 1) / page_number)
288
  with survey_pages:
 
299
  st.session_state['questionnaire_response']['level_education'] = level_education
300
  elif survey_pages.current == 1:
301
  st.write("#### What domains are you most interested in?")
302
+ domains = ['Global Studies', 'Arts', 'Business & Economics', 'History', 'Humanities',
303
+ 'Law', 'Medicine and Health', 'Science - Biology', 'Science - Chemistry',
304
+ 'Science - Environmental Science', 'Science - Physics', 'Mathematics',
305
+ 'Engineering', 'Social Sciences']
306
+ domains_interested = {}
307
+ for i in range(len(domains)):
308
+ domains_interested[domains[i]] = survey.checkbox(domains[i])
309
+ interested_domain = []
310
+ for domain in domains_interested:
311
+ if domains_interested[domain]:
312
+ interested_domain.append(domain)
313
+ st.session_state['questionnaire_response']['interested_domain'] = interested_domain
 
 
314
  elif survey_pages.current == 2:
315
+ paper_discovery_method = survey.text_area("#### How did you come across this paper?")
316
+ st.session_state['questionnaire_response']['paper_discovery_method'] = paper_discovery_method
317
  elif survey_pages.current == 3:
318
+ reading_purpose = survey.text_area("#### For what purpose are you reading this paper?")
319
+ st.session_state['questionnaire_response']['reading_purpose'] = reading_purpose
320
  elif survey_pages.current == 4:
321
  st.write("#### What information do you want to get out of this paper?")
322
+ information_options = ["Main findings and conclusions",
323
+ 'Methodology and experimental design',
324
+ 'Data and statistical analysis',
325
+ 'Limitations or gaps in the research']
326
+ info_interested = {}
327
+ for i in range(len(information_options)):
328
+ info_interested[information_options[i]] = survey.checkbox(information_options[i])
329
+ desired_information = []
330
+ for info in info_interested:
331
+ if info_interested[info]:
332
+ desired_information.append(info)
333
+ other_info = survey.text_input('Other aspects:')
334
+ if other_info:
335
+ desired_information.append(other_info)
336
+ st.session_state['questionnaire_response']['desired_information'] = desired_information
337
  elif survey_pages.current == 5:
338
  st.write("#### What is your familiarity with the concepts of the paper?")
339
  st.markdown('''
 
346
  paper_familiarity = survey.select_slider(
347
  label="paper_familiarity",
348
  options=['No Familiarity', 'Limited Familiarity', 'Moderate Familiarity',
349
+ 'Good Familiarity', 'Expert Familiarity'],
350
  # min_value=1,
351
  # max_value=5,
352
  label_visibility="collapsed",
 
391
  label_visibility="collapsed",
392
  horizontal=True,
393
  )
394
+ st.session_state['questionnaire_response']['books_read'] = books_read
395
  elif survey_pages.current == 9:
396
  st.write("#### What is the primary language spoken in your home? (click from the list and others)")
397
+ languages = ['English', 'Spanish', ]
398
+ language_options = {}
399
+ for i in range(len(languages)):
400
+ language_options[languages[i]] = survey.checkbox(languages[i])
401
+ language_spoken = []
402
+ for language in language_options:
403
+ if language_options[language]:
404
+ language_spoken.append(language)
405
  other_language = survey.text_input('Other')
406
 
 
 
 
 
407
  if other_language:
408
+ language_spoken.append(other_language)
409
+ st.session_state['questionnaire_response']['language_spoken'] = language_spoken
 
 
 
 
 
410
 
411
  elif authentication_status is False:
412
  st.error('Username or Password is incorrect', icon="⚠️")