grapplerulrich commited on
Commit
dc5c663
1 Parent(s): a604031

Inline docs & seperation

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -10,6 +10,9 @@ import uuid
10
 
11
  from beautiful_soup.beautiful_soup import get_url_content
12
 
 
 
 
13
  @cache
14
  def google_search_api_request( query ):
15
  api_key = st.secrets["google_search_api_key"]
@@ -32,6 +35,9 @@ def google_search_api_request( query ):
32
  fields='items(title,link),searchInformation(totalResults)'
33
  ).execute()
34
 
 
 
 
35
  def search_results( query ):
36
  file_path = 'search-results/' + slugify( query ) + '.json'
37
 
@@ -52,7 +58,10 @@ def search_results( query ):
52
 
53
  return results
54
 
55
- def content_summary( url_id, content ):
 
 
 
56
  file_path = 'summaries/' + url_id + '.json'
57
  makedirs(dirname(file_path), exist_ok=True)
58
  if exists( file_path ):
@@ -71,6 +80,9 @@ def content_summary( url_id, content ):
71
 
72
  return summary
73
 
 
 
 
74
  def exception_notice( exception ):
75
  query_params = st.experimental_get_query_params()
76
  if 'debug' in query_params.keys() and query_params['debug'][0] == 'true':
@@ -78,12 +90,23 @@ def exception_notice( exception ):
78
  else:
79
  st.warning(str(exception))
80
 
 
 
 
81
  def is_keyword_in_string( keywords, string ):
82
  for keyword in keywords:
83
  if keyword in string:
84
  return True
85
  return False
86
 
 
 
 
 
 
 
 
 
87
  def main():
88
  st.title('Racoon Search')
89
  query = st.text_input('Search query')
@@ -119,11 +142,10 @@ def main():
119
  try:
120
  strings = get_url_content( result['link'] )
121
  keywords = query.split(' ')
122
- content = ''
123
- for string in strings:
124
- if is_keyword_in_string( keywords, string ):
125
- content += string + '\n'
126
- summary = content_summary( url_id, content )
127
  for sentence in summary:
128
  st.write(sentence['summary_text'])
129
  except Exception as exception:
 
10
 
11
  from beautiful_soup.beautiful_soup import get_url_content
12
 
13
+ """
14
+ Request Google Search API with query and return results.
15
+ """
16
  @cache
17
  def google_search_api_request( query ):
18
  api_key = st.secrets["google_search_api_key"]
 
35
  fields='items(title,link),searchInformation(totalResults)'
36
  ).execute()
37
 
38
+ """
39
+ Request Google Search API with query and return results. Results are cached in files.
40
+ """
41
  def search_results( query ):
42
  file_path = 'search-results/' + slugify( query ) + '.json'
43
 
 
58
 
59
  return results
60
 
61
+ """
62
+ Generate summary for content.
63
+ """
64
+ def generate_summary( url_id, content ):
65
  file_path = 'summaries/' + url_id + '.json'
66
  makedirs(dirname(file_path), exist_ok=True)
67
  if exists( file_path ):
 
80
 
81
  return summary
82
 
83
+ """
84
+ Helper function for exception notices.
85
+ """
86
  def exception_notice( exception ):
87
  query_params = st.experimental_get_query_params()
88
  if 'debug' in query_params.keys() and query_params['debug'][0] == 'true':
 
90
  else:
91
  st.warning(str(exception))
92
 
93
+ """
94
+ Checks if string contains keyword.
95
+ """
96
  def is_keyword_in_string( keywords, string ):
97
  for keyword in keywords:
98
  if keyword in string:
99
  return True
100
  return False
101
 
102
+ def filter_strings_by_keywords( strings, keywords ):
103
+ content = ''
104
+ for string in strings:
105
+ # Filter strings with keywords
106
+ if is_keyword_in_string( keywords, string ):
107
+ content += string + '\n'
108
+ return content
109
+
110
  def main():
111
  st.title('Racoon Search')
112
  query = st.text_input('Search query')
 
142
  try:
143
  strings = get_url_content( result['link'] )
144
  keywords = query.split(' ')
145
+ content = filter_strings_by_keywords( strings, keywords )
146
+ # print(content)
147
+ # print(len(content.split()))
148
+ summary = generate_summary( url_id, content )
 
149
  for sentence in summary:
150
  st.write(sentence['summary_text'])
151
  except Exception as exception: