Added tool to get article summary from Redis cache.
Browse files- assets/text.py +1 -0
- functions/summarization.py +1 -1
- functions/tools.py +31 -0
- rss_server.py +29 -3
assets/text.py
CHANGED
@@ -27,5 +27,6 @@ DESCRIPTION = ('''
|
|
27 |
2. `context_search()`: Vector search on article content for RAG context.
|
28 |
3. `find_article()`: Uses vector search on article content to find title of article
|
29 |
that user is referring to.
|
|
|
30 |
|
31 |
''')
|
|
|
27 |
2. `context_search()`: Vector search on article content for RAG context.
|
28 |
3. `find_article()`: Uses vector search on article content to find title of article
|
29 |
that user is referring to.
|
30 |
+
4. `get_summary()`: Gets article summary from Redis cache using article title.
|
31 |
|
32 |
''')
|
functions/summarization.py
CHANGED
@@ -25,7 +25,7 @@ def summarize_content(title: str, content: str) -> str:
|
|
25 |
logger.info('Summarizing extracted content')
|
26 |
|
27 |
# Check Redis cache for summary
|
28 |
-
cache_key = f'{title}
|
29 |
cached_summary = REDIS.get(cache_key)
|
30 |
|
31 |
if cached_summary:
|
|
|
25 |
logger.info('Summarizing extracted content')
|
26 |
|
27 |
# Check Redis cache for summary
|
28 |
+
cache_key = f'{title} summary'
|
29 |
cached_summary = REDIS.get(cache_key)
|
30 |
|
31 |
if cached_summary:
|
functions/tools.py
CHANGED
@@ -8,6 +8,7 @@ import logging
|
|
8 |
import queue
|
9 |
from typing import Tuple
|
10 |
from upstash_vector import Index
|
|
|
11 |
|
12 |
import functions.feed_extraction as extraction_funcs
|
13 |
import functions.summarization as summarization_funcs
|
@@ -166,3 +167,33 @@ def find_article(query: str) -> list[Tuple[float, str]]:
|
|
166 |
contexts.append((result.score, result.metadata['namespace']))
|
167 |
|
168 |
return contexts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
import queue
|
9 |
from typing import Tuple
|
10 |
from upstash_vector import Index
|
11 |
+
from upstash_redis import Redis
|
12 |
|
13 |
import functions.feed_extraction as extraction_funcs
|
14 |
import functions.summarization as summarization_funcs
|
|
|
167 |
contexts.append((result.score, result.metadata['namespace']))
|
168 |
|
169 |
return contexts
|
170 |
+
|
171 |
+
|
172 |
+
def get_summary(title: str) -> str:
|
173 |
+
'''Uses article title to get summary of article content.
|
174 |
+
|
175 |
+
Args:
|
176 |
+
title: title of article to get summary for
|
177 |
+
|
178 |
+
Returns:
|
179 |
+
Short summary of article content. Returns "No summary found"
|
180 |
+
if summary does not exist.
|
181 |
+
'''
|
182 |
+
|
183 |
+
logger = logging.getLogger(__name__ + '.get_summary()')
|
184 |
+
|
185 |
+
redis = Redis(
|
186 |
+
url='https://sensible-midge-19304.upstash.io',
|
187 |
+
token=os.environ['UPSTASH_REDIS_KEY']
|
188 |
+
)
|
189 |
+
|
190 |
+
cache_key = f'{title} summary'
|
191 |
+
summary = redis.get(cache_key)
|
192 |
+
|
193 |
+
if summary:
|
194 |
+
|
195 |
+
logger.info('Got summary for "%s": %s', title, summary[:100])
|
196 |
+
return summary
|
197 |
+
|
198 |
+
logger.info('Could not find summary for: "%s"', title)
|
199 |
+
return 'No summary found'
|
rss_server.py
CHANGED
@@ -55,7 +55,7 @@ with gr.Blocks() as demo:
|
|
55 |
|
56 |
# Get feed tool
|
57 |
gr.Markdown('### 1. `get_feed()`')
|
58 |
-
website_url = gr.Textbox('
|
59 |
feed_output = gr.Textbox(label='RSS entries', lines=7, max_lines=7)
|
60 |
|
61 |
with gr.Row():
|
@@ -73,7 +73,7 @@ with gr.Blocks() as demo:
|
|
73 |
gr.Markdown('### 2. `context_search()`')
|
74 |
|
75 |
context_search_query = gr.Textbox(
|
76 |
-
'
|
77 |
label='Context search query'
|
78 |
)
|
79 |
context_search_output = gr.Textbox(
|
@@ -99,7 +99,7 @@ with gr.Blocks() as demo:
|
|
99 |
gr.Markdown('### 3. `find_article()`')
|
100 |
|
101 |
article_search_query = gr.Textbox(
|
102 |
-
'
|
103 |
label='Article search query'
|
104 |
)
|
105 |
article_search_output = gr.Textbox(
|
@@ -121,6 +121,32 @@ with gr.Blocks() as demo:
|
|
121 |
api_name='Article vector search'
|
122 |
)
|
123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
if __name__ == '__main__':
|
126 |
|
|
|
55 |
|
56 |
# Get feed tool
|
57 |
gr.Markdown('### 1. `get_feed()`')
|
58 |
+
website_url = gr.Textbox('slashdot', label='Website')
|
59 |
feed_output = gr.Textbox(label='RSS entries', lines=7, max_lines=7)
|
60 |
|
61 |
with gr.Row():
|
|
|
73 |
gr.Markdown('### 2. `context_search()`')
|
74 |
|
75 |
context_search_query = gr.Textbox(
|
76 |
+
'How is the air traffic control system being updated?',
|
77 |
label='Context search query'
|
78 |
)
|
79 |
context_search_output = gr.Textbox(
|
|
|
99 |
gr.Markdown('### 3. `find_article()`')
|
100 |
|
101 |
article_search_query = gr.Textbox(
|
102 |
+
'How is the air traffic control system being updated?',
|
103 |
label='Article search query'
|
104 |
)
|
105 |
article_search_output = gr.Textbox(
|
|
|
121 |
api_name='Article vector search'
|
122 |
)
|
123 |
|
124 |
+
# Get summary tool
|
125 |
+
gr.Markdown('### 4. `get_summary()`')
|
126 |
+
|
127 |
+
article_title = gr.Textbox(
|
128 |
+
'FAA To Eliminate Floppy Disks Used In Air Traffic Control Systems',
|
129 |
+
label='Article title'
|
130 |
+
)
|
131 |
+
article_summary = gr.Textbox(
|
132 |
+
label='Article summary',
|
133 |
+
lines=7,
|
134 |
+
max_lines=7
|
135 |
+
)
|
136 |
+
|
137 |
+
with gr.Row():
|
138 |
+
article_title_submit_button = gr.Button('Submit title')
|
139 |
+
article_title_clear_button = gr.ClearButton(
|
140 |
+
components=[article_title, article_summary]
|
141 |
+
)
|
142 |
+
|
143 |
+
article_title_submit_button.click( # pylint: disable=no-member
|
144 |
+
fn=tool_funcs.get_summary,
|
145 |
+
inputs=article_title,
|
146 |
+
outputs=article_summary,
|
147 |
+
api_name='Article summary search'
|
148 |
+
)
|
149 |
+
|
150 |
|
151 |
if __name__ == '__main__':
|
152 |
|