Iker commited on
Commit
24e2197
1 Parent(s): 97d7e99

Take into account redirects for cache

Browse files
Files changed (2) hide show
  1. app.py +8 -0
  2. cache_system.py +9 -2
app.py CHANGED
@@ -63,6 +63,7 @@ def generate_text(
63
  if title is not None and text is not None and temp is not None:
64
  temp = finish_generation(temp)
65
  yield title, temp, text
 
66
  else:
67
  try:
68
  title, text, url = download_text_and_title(url)
@@ -82,6 +83,13 @@ def generate_text(
82
  "Error",
83
  )
84
 
 
 
 
 
 
 
 
85
  progress(0.5, desc="🤖 Leyendo noticia")
86
 
87
  try:
 
63
  if title is not None and text is not None and temp is not None:
64
  temp = finish_generation(temp)
65
  yield title, temp, text
66
+ return title, temp, text
67
  else:
68
  try:
69
  title, text, url = download_text_and_title(url)
 
83
  "Error",
84
  )
85
 
86
+ # Test if the redirected and clean url is in the cache
87
+ _, _, temp = cache_handler.get_from_cache(url, mode, second_try=True)
88
+ if temp is not None:
89
+ temp = finish_generation(temp)
90
+ yield title, temp, text
91
+ return title, temp, text
92
+
93
  progress(0.5, desc="🤖 Leyendo noticia")
94
 
95
  try:
cache_system.py CHANGED
@@ -33,18 +33,25 @@ class CacheHandler:
33
  if len(self.cache) > self.max_cache_size:
34
  self.cache.popitem(last=False) # pop the oldest item
35
 
36
- def get_from_cache(self, url: str, summary_type: int) -> Optional[tuple]:
 
 
37
  if url in self.cache and self.cache[url][f"summary_{summary_type}"] is not None:
38
  # Move the accessed item to the end to mark it as recently used
39
  self.cache.move_to_end(url)
40
  self.hits += 1
 
 
 
 
41
  return (
42
  self.cache[url]["title"],
43
  self.cache[url]["text"],
44
  self.cache[url][f"summary_{summary_type}"],
45
  )
46
  else:
47
- self.misses += 1
 
48
  return None, None, None
49
 
50
  def get_cache_stats(self):
 
33
  if len(self.cache) > self.max_cache_size:
34
  self.cache.popitem(last=False) # pop the oldest item
35
 
36
+ def get_from_cache(
37
+ self, url: str, summary_type: int, second_try: bool = False
38
+ ) -> Optional[tuple]:
39
  if url in self.cache and self.cache[url][f"summary_{summary_type}"] is not None:
40
  # Move the accessed item to the end to mark it as recently used
41
  self.cache.move_to_end(url)
42
  self.hits += 1
43
+ if second_try:
44
+ # In the first try we didn't get the cache hit, probably because it was a shortened URL
45
+ # So me decrease the number of misses, because we got the cache hit in the end
46
+ self.misses -= 1
47
  return (
48
  self.cache[url]["title"],
49
  self.cache[url]["text"],
50
  self.cache[url][f"summary_{summary_type}"],
51
  )
52
  else:
53
+ if not second_try:
54
+ self.misses += 1
55
  return None, None, None
56
 
57
  def get_cache_stats(self):