quyip commited on
Commit
d5f14ef
·
1 Parent(s): 1ccfd50
Files changed (4) hide show
  1. main.py +6 -2
  2. utils/cache_layer.py +2 -3
  3. utils/logging.py +26 -0
  4. utils/summary_utils.py +11 -5
main.py CHANGED
@@ -2,6 +2,7 @@ from fastapi import FastAPI, BackgroundTasks
2
 
3
  from utils.cache_layer import get_summarize_from_cache, summarize_un_cache_page
4
  from utils.data_proto import SummaryReq, SummariesReq
 
5
  from utils.summary_utils import summarize
6
 
7
  KEY = 'J9l#K4wP5h@2'
@@ -25,7 +26,10 @@ async def summaries(background_tasks: BackgroundTasks, request: SummariesReq):
25
  if request.key != KEY:
26
  return 'Unauthorized'
27
  pages_summaries, uncached_pages = get_summarize_from_cache(request.pages)
28
- print(f'processing cached pages: {len(pages_summaries)}, uncached pages: {len(uncached_pages)}')
29
  background_tasks.add_task(summarize_un_cache_page, uncached_pages)
30
- print('return')
31
  return pages_summaries
 
 
 
 
 
 
2
 
3
  from utils.cache_layer import get_summarize_from_cache, summarize_un_cache_page
4
  from utils.data_proto import SummaryReq, SummariesReq
5
+ from utils.logging import read_last_n_logs
6
  from utils.summary_utils import summarize
7
 
8
  KEY = 'J9l#K4wP5h@2'
 
26
  if request.key != KEY:
27
  return 'Unauthorized'
28
  pages_summaries, uncached_pages = get_summarize_from_cache(request.pages)
 
29
  background_tasks.add_task(summarize_un_cache_page, uncached_pages)
 
30
  return pages_summaries
31
+
32
+
33
+ @app.get("/logs/")
34
+ async def logs(n: int = 100):
35
+ return read_last_n_logs(n)
utils/cache_layer.py CHANGED
@@ -3,6 +3,7 @@ from typing import List
3
  from diskcache import Cache
4
 
5
  from utils.data_proto import Page
 
6
  from utils.summary_utils import summarize
7
 
8
  cache = Cache(directory='/.cache/tmp/summary', size_limit=int(1e9)) # 1GB
@@ -22,10 +23,8 @@ def get_summarize_from_cache(pages: List[Page]):
22
 
23
 
24
  def summarize_un_cache_page(pages: List[Page]):
25
- print(f'summarize pages length: {len(pages)}')
26
  for page in pages:
27
  id = page['id']
28
- print(f'processing page: {id}')
29
  summary = summarize(id, page['text'])
30
  cache.set(id, summary)
31
- print(f'processed page: {id}')
 
3
  from diskcache import Cache
4
 
5
  from utils.data_proto import Page
6
+ from utils.logging import logger
7
  from utils.summary_utils import summarize
8
 
9
  cache = Cache(directory='/.cache/tmp/summary', size_limit=int(1e9)) # 1GB
 
23
 
24
 
25
  def summarize_un_cache_page(pages: List[Page]):
 
26
  for page in pages:
27
  id = page['id']
 
28
  summary = summarize(id, page['text'])
29
  cache.set(id, summary)
30
+ logger.info(f'processed page {id}')
utils/logging.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from logging.handlers import RotatingFileHandler
2
+
3
+ import logging
4
+
5
+ log_file = '/.cache/app.log'
6
+
7
+ # 配置 RotatingFileHandler
8
+ handler = RotatingFileHandler(log_file, maxBytes=100 * 1024 * 1024, backupCount=3) # 100MG
9
+ handler.setLevel(logging.INFO)
10
+ handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
11
+
12
+ logger = logging.getLogger()
13
+ logger.addHandler(handler)
14
+
15
+
16
+ def read_last_n_logs(n, level='ERROR'):
17
+ error_logs = []
18
+ with open(log_file, 'r') as file:
19
+ lines = file.readlines()[-n:]
20
+
21
+ # 检查每行日志的级别,只保留 ERROR 级别的日志
22
+ for line in lines:
23
+ if level in line:
24
+ error_logs.append(line.strip())
25
+
26
+ return error_logs
utils/summary_utils.py CHANGED
@@ -3,6 +3,7 @@ import re
3
  from langdetect import detect
4
  from transformers import pipeline
5
 
 
6
  from utils.tag_utils import filter_tags
7
 
8
  AiSummaryVersion = 1
@@ -37,7 +38,8 @@ def get_summarization(text: str):
37
  try:
38
  result = summarization_pipeline(text)
39
  return result[0]['summary_text'] if isinstance(result, list) else result['summary_text']
40
- except:
 
41
  return None
42
 
43
 
@@ -49,7 +51,8 @@ def get_en_translation(text: str):
49
  return text
50
  result = en_translation_pipe(text)
51
  return result[0]['translation_text'] if isinstance(result, list) else result['translation_text']
52
- except:
 
53
  return None
54
 
55
 
@@ -57,7 +60,8 @@ def is_english(text):
57
  try:
58
  lang = detect(text)
59
  return lang == 'en'
60
- except:
 
61
  return False
62
 
63
 
@@ -71,7 +75,8 @@ def get_tags(text: str):
71
  tags = [tag.strip() for tag in tags]
72
  tags = [tag for tag in tags if len(tag) > 2 and len(tag.split(' ')) == 1]
73
  return tags
74
- except:
 
75
  return []
76
 
77
 
@@ -84,5 +89,6 @@ def get_classification(text: str):
84
  return [tag['label'].strip() for tag in result if tag['score'] > 0.75]
85
  else:
86
  return [result['label'].strip()] if result['score'] > 0.75 else []
87
- except:
 
88
  return []
 
3
  from langdetect import detect
4
  from transformers import pipeline
5
 
6
+ from utils.logging import logger
7
  from utils.tag_utils import filter_tags
8
 
9
  AiSummaryVersion = 1
 
38
  try:
39
  result = summarization_pipeline(text)
40
  return result[0]['summary_text'] if isinstance(result, list) else result['summary_text']
41
+ except Exception as e:
42
+ logger.error(e)
43
  return None
44
 
45
 
 
51
  return text
52
  result = en_translation_pipe(text)
53
  return result[0]['translation_text'] if isinstance(result, list) else result['translation_text']
54
+ except Exception as e:
55
+ logger.error(e)
56
  return None
57
 
58
 
 
60
  try:
61
  lang = detect(text)
62
  return lang == 'en'
63
+ except Exception as e:
64
+ logger.error(e)
65
  return False
66
 
67
 
 
75
  tags = [tag.strip() for tag in tags]
76
  tags = [tag for tag in tags if len(tag) > 2 and len(tag.split(' ')) == 1]
77
  return tags
78
+ except Exception as e:
79
+ logger.error(e)
80
  return []
81
 
82
 
 
89
  return [tag['label'].strip() for tag in result if tag['score'] > 0.75]
90
  else:
91
  return [result['label'].strip()] if result['score'] > 0.75 else []
92
+ except Exception as e:
93
+ logger.error(e)
94
  return []