MANOJSEQ commited on
Commit
3566a89
·
verified ·
1 Parent(s): 54cb7c1

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +6 -1
main.py CHANGED
@@ -30,6 +30,7 @@ from starlette.middleware.gzip import GZipMiddleware
30
  from transformers import pipeline as hf_pipeline
31
  os.environ.setdefault("OMP_NUM_THREADS", "1")
32
  from fastapi.responses import PlainTextResponse
 
33
 
34
  import torch
35
  torch.set_num_threads(2)
@@ -1720,7 +1721,8 @@ def get_events(
1720
  max_events: int = Query(15, ge=5, le=50),
1721
  min_countries: int = Query(2, ge=1, le=50),
1722
  min_articles: int = Query(2, ge=1, le=200),
1723
- speed: Speed = Query(Speed.balanced),
 
1724
  ):
1725
 
1726
  # always build cache on untranslated data
@@ -1742,6 +1744,7 @@ def get_events(
1742
  events = [e for e in events if (e["country_count"] >= min_countries and e["article_count"] >= min_articles)]
1743
  events.sort(key=lambda e: e["article_count"], reverse=True)
1744
 
 
1745
  return {"events": events[:max_events], "cache_key": "|".join(map(str, cache_key))}
1746
 
1747
  @app.get("/event/{event_id}")
@@ -1814,6 +1817,7 @@ def get_news(
1814
  speed: Speed = Query(Speed.balanced),
1815
  page: int = Query(1, ge=1),
1816
  page_size: int = Query(120, ge=5, le=300),
 
1817
  ):
1818
  enriched: List[Dict[str, Any]] = []
1819
 
@@ -1881,6 +1885,7 @@ def get_news(
1881
  for k in drop:
1882
  i.pop(k, None)
1883
 
 
1884
  return {
1885
  "items": items,
1886
  "total": total,
 
30
  from transformers import pipeline as hf_pipeline
31
  os.environ.setdefault("OMP_NUM_THREADS", "1")
32
  from fastapi.responses import PlainTextResponse
33
+ from fastapi import Response
34
 
35
  import torch
36
  torch.set_num_threads(2)
 
1721
  max_events: int = Query(15, ge=5, le=50),
1722
  min_countries: int = Query(2, ge=1, le=50),
1723
  min_articles: int = Query(2, ge=1, le=200),
1724
+ speed: Speed = Query(Speed.fast),
1725
+ response: Response = None
1726
  ):
1727
 
1728
  # always build cache on untranslated data
 
1744
  events = [e for e in events if (e["country_count"] >= min_countries and e["article_count"] >= min_articles)]
1745
  events.sort(key=lambda e: e["article_count"], reverse=True)
1746
 
1747
+ response.headers["Cache-Control"] = "public, max-age=30, s-maxage=60, stale-while-revalidate=300"
1748
  return {"events": events[:max_events], "cache_key": "|".join(map(str, cache_key))}
1749
 
1750
  @app.get("/event/{event_id}")
 
1817
  speed: Speed = Query(Speed.balanced),
1818
  page: int = Query(1, ge=1),
1819
  page_size: int = Query(120, ge=5, le=300),
1820
+ response: Response = None
1821
  ):
1822
  enriched: List[Dict[str, Any]] = []
1823
 
 
1885
  for k in drop:
1886
  i.pop(k, None)
1887
 
1888
+ response.headers["Cache-Control"] = "public, max-age=30, s-maxage=60, stale-while-revalidate=300"
1889
  return {
1890
  "items": items,
1891
  "total": total,