fixed sesskey
Browse files
main.py
CHANGED
|
@@ -1,31 +1,42 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI, HTTPException, Request
|
| 3 |
-
from sse_starlette.sse import EventSourceResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 5 |
from openai import AsyncOpenAI
|
| 6 |
-
from fasthtml.common import
|
|
|
|
|
|
|
|
|
|
| 7 |
from styles import styles
|
| 8 |
from script import script
|
| 9 |
from fasthtml_hf import setup_hf_backup
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
import bleach
|
| 12 |
|
| 13 |
-
load_dotenv(
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
|
|
|
|
| 20 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 21 |
-
|
| 22 |
-
setup_hf_backup(app, token=hf_token)
|
| 23 |
|
| 24 |
client = AsyncOpenAI()
|
| 25 |
-
|
| 26 |
conversations = {}
|
| 27 |
|
| 28 |
-
ALLOWED_TAGS = list(bleach.sanitizer.ALLOWED_TAGS) + [
|
|
|
|
|
|
|
| 29 |
ALLOWED_ATTRIBUTES = bleach.sanitizer.ALLOWED_ATTRIBUTES
|
| 30 |
|
| 31 |
static_dir = os.path.join(os.path.dirname(__file__), "static")
|
|
@@ -43,7 +54,6 @@ def home():
|
|
| 43 |
home_text = """
|
| 44 |
## FastGPT - A ChatGPT Implementation Using FastHTML
|
| 45 |
"""
|
| 46 |
-
|
| 47 |
page = Html(
|
| 48 |
Head(
|
| 49 |
Title("FastGPT"),
|
|
|
|
| 1 |
import os
|
| 2 |
+
import secrets
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
from fastapi import FastAPI, HTTPException, Request
|
|
|
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
| 6 |
+
from sse_starlette.sse import EventSourceResponse
|
| 7 |
from openai import AsyncOpenAI
|
| 8 |
+
from fasthtml.common import (
|
| 9 |
+
FastHTML, Html, Head, Title, Body, Div, Button, Textarea,
|
| 10 |
+
Script, Style, P, Favicon, ft_hx
|
| 11 |
+
)
|
| 12 |
from styles import styles
|
| 13 |
from script import script
|
| 14 |
from fasthtml_hf import setup_hf_backup
|
|
|
|
| 15 |
import bleach
|
| 16 |
|
| 17 |
+
load_dotenv()
|
| 18 |
|
| 19 |
+
def get_or_create_secret_key(file_path=".sesskey"):
|
| 20 |
+
if os.path.exists(file_path):
|
| 21 |
+
with open(file_path, "r") as f:
|
| 22 |
+
return f.read().strip()
|
| 23 |
+
key = secrets.token_urlsafe(32)
|
| 24 |
+
with open(file_path, "w") as f:
|
| 25 |
+
f.write(key)
|
| 26 |
+
return key
|
| 27 |
|
| 28 |
+
secret_key = get_or_create_secret_key()
|
| 29 |
|
| 30 |
+
app = FastHTML(secret_key=secret_key)
|
| 31 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 32 |
+
setup_hf_backup(app)
|
|
|
|
| 33 |
|
| 34 |
client = AsyncOpenAI()
|
|
|
|
| 35 |
conversations = {}
|
| 36 |
|
| 37 |
+
ALLOWED_TAGS = list(bleach.sanitizer.ALLOWED_TAGS) + [
|
| 38 |
+
"h1", "h2", "h3", "p", "strong", "em", "ul", "ol", "li", "code", "pre", "blockquote"
|
| 39 |
+
]
|
| 40 |
ALLOWED_ATTRIBUTES = bleach.sanitizer.ALLOWED_ATTRIBUTES
|
| 41 |
|
| 42 |
static_dir = os.path.join(os.path.dirname(__file__), "static")
|
|
|
|
| 54 |
home_text = """
|
| 55 |
## FastGPT - A ChatGPT Implementation Using FastHTML
|
| 56 |
"""
|
|
|
|
| 57 |
page = Html(
|
| 58 |
Head(
|
| 59 |
Title("FastGPT"),
|