Spaces:
Sleeping
Sleeping
File size: 4,636 Bytes
e4f5c0d 908d5c9 e4f5c0d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
import json
import os
from datetime import datetime, timezone
from fasthtml.common import *
from huggingface_hub import HfApi, hf_hub_download
from starlette.responses import FileResponse
from generate_newsletter import process_new_papers
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
from fasthtml_hf import setup_hf_backup
# Initialize Hugging Face API
HF_TOKEN = os.environ.get("HF_TOKEN")
DATASET_NAME = "cmcmaster/this_week_in_rheumatology"
api = HfApi(token=HF_TOKEN)
# Initialize scheduler
scheduler = BackgroundScheduler()
# Schedule newsletter generation to run every Monday at 1 AM UTC
scheduler.add_job(process_new_papers,
CronTrigger(day_of_week='mon', hour=1),
kwargs={
'end_date': '{{ (execution_date - timedelta(days=1)).strftime("%Y-%m-%d") }}',
'test': False
},
id='generate_newsletter',
name='Weekly newsletter generation',
replace_existing=True)
css = Style("""
body {
font-family: Georgia, Times, serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background: #fff;
}
h1, h2 {
color: #2c3e50;
font-family: Georgia, Times, serif;
}
a {
color: #2c3e50;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
ul {
list-style-type: none;
padding: 0;
}
li {
margin-bottom: 10px;
}
.newsletter-content {
margin-top: 20px;
}
.download-link {
display: inline-block;
padding: 10px 20px;
background-color: #2c3e50;
color: white;
border-radius: 3px;
margin: 10px 0;
font-family: Georgia, Times, serif;
}
.download-link:hover {
background-color: #34495e;
text-decoration: none;
}
""")
app = FastHTML(hdrs=(css, MarkdownJS(),
HighlightJS(
langs=['python', 'javascript', 'html', 'css'])))
# Start the scheduler when the app starts
@app.on_event("startup")
async def start_scheduler():
scheduler.start()
# Shut down the scheduler when the app stops
@app.on_event("shutdown")
async def shutdown_scheduler():
scheduler.shutdown()
def get_newsletter_list():
# Fetch the list of newsletters from the Hugging Face repository
files = api.list_repo_files(repo_id=DATASET_NAME, repo_type="dataset")
newsletters = [f for f in files if f.endswith('newsletter.json')]
return sorted(newsletters, reverse=True)
def get_newsletter_content(path):
# Download and parse the newsletter content
content = api.hf_hub_download(repo_id=DATASET_NAME,
filename=path,
repo_type="dataset")
with open(content, 'r') as f:
return json.load(f)
@app.get("/")
def index():
newsletters = get_newsletter_list()
links = [
Li(
A(datetime.strptime(n.split('/')[0], '%Y%m%d').strftime('%B %d, %Y'),
href=f"/newsletter/{n.split('/')[0]}")) for n in newsletters
]
return Titled("This Week in Rheumatology", H2("Available Newsletters"),
Ul(*links))
@app.get("/newsletter/{date}")
def newsletter(date: str):
path = f"{date}/newsletter.json"
pdf_path = f"{date}/newsletter.pdf"
try:
content = get_newsletter_content(path)
return Titled(
f"This Week in Rheumatology - {content['date']}",
A("Back to Index", href="/"),
Div(
A("Download PDF", href=f"/download/{date}", cls="download-link")
),
Div(content['content'], cls="marked"))
except Exception as e:
return Titled("Error", H2("Newsletter not found"),
P(f"Unable to load newsletter for date: {date}"),
A("Back to Index", href="/"))
@app.get("/download/{date}")
def download_pdf(date: str):
try:
pdf_path = f"{date}/newsletter.pdf"
content = api.hf_hub_download(repo_id=DATASET_NAME,
filename=pdf_path,
repo_type="dataset")
return FileResponse(content,
media_type="application/pdf",
filename=f"newsletter_{date}.pdf")
except Exception as e:
return Titled("Error", H2("PDF not found"),
P(f"Unable to load PDF for date: {date}"),
A("Back to Index", href="/"))
setup_hf_backup(app)
serve()
|