Spaces:
Sleeping
Sleeping
from fastapi import FastAPI | |
from fastapi.responses import HTMLResponse, RedirectResponse | |
from fastapi.staticfiles import StaticFiles | |
import os | |
app = FastAPI() | |
# Mount static files | |
app.mount("/src", StaticFiles(directory="src"), name="src") | |
async def get_icon(): | |
# Redirect to GitHub-hosted icon | |
return RedirectResponse(url="https://raw.githubusercontent.com/zmuhls/cloze-reader/main/icon.png") | |
async def read_root(): | |
# Read the HTML file and inject environment variables | |
with open("index.html", "r") as f: | |
html_content = f.read() | |
# Inject environment variables as a script | |
openrouter_key = os.getenv("OPENROUTER_API_KEY", "") | |
hf_key = os.getenv("HF_API_KEY", "") | |
# Create a CSP-compliant way to inject the keys | |
env_script = f""" | |
<meta name="openrouter-key" content="{openrouter_key}"> | |
<meta name="hf-key" content="{hf_key}"> | |
<script> | |
// Read keys from meta tags to avoid CSP issues | |
document.addEventListener('DOMContentLoaded', function() {{ | |
const openrouterMeta = document.querySelector('meta[name="openrouter-key"]'); | |
const hfMeta = document.querySelector('meta[name="hf-key"]'); | |
if (openrouterMeta) window.OPENROUTER_API_KEY = openrouterMeta.content; | |
if (hfMeta) window.HF_API_KEY = hfMeta.content; | |
}}); | |
</script> | |
""" | |
# Insert the script before closing head tag | |
html_content = html_content.replace("</head>", env_script + "</head>") | |
return HTMLResponse(content=html_content) | |
if __name__ == "__main__": | |
import uvicorn | |
uvicorn.run(app, host="0.0.0.0", port=7860) |