changed hf_token
Browse files
main.py
CHANGED
@@ -4,19 +4,22 @@ from sse_starlette.sse import EventSourceResponse
|
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
from openai import AsyncOpenAI
|
6 |
from fasthtml.common import FastHTML, Html, Head, Title, Body, Div, Button, Textarea, Script, Style, P, Favicon, ft_hx
|
7 |
-
import bleach
|
8 |
-
|
9 |
from styles import styles
|
10 |
from script import script
|
11 |
from fasthtml_hf import setup_hf_backup
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
secret_key = os.getenv(
|
|
|
14 |
|
15 |
app = FastHTML(secret_key=secret_key)
|
16 |
|
17 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
18 |
|
19 |
-
setup_hf_backup(app)
|
20 |
|
21 |
client = AsyncOpenAI()
|
22 |
|
@@ -30,21 +33,20 @@ light_icon = os.path.join(static_dir, "favicon-light.ico")
|
|
30 |
dark_icon = os.path.join(static_dir, "favicon-dark.ico")
|
31 |
|
32 |
def Svg(*c, viewBox=None, **kwargs):
|
33 |
-
return ft_hx(
|
34 |
|
35 |
def Path(*c, d=None, fill=None, **kwargs):
|
36 |
-
return ft_hx(
|
37 |
|
38 |
@app.get("/")
|
39 |
def home():
|
40 |
-
"""Render homepage with FastGPT UI."""
|
41 |
home_text = """
|
42 |
## FastGPT - A ChatGPT Implementation Using FastHTML
|
43 |
"""
|
44 |
-
|
45 |
page = Html(
|
46 |
Head(
|
47 |
-
Title(
|
48 |
Favicon(light_icon="/static/favicon-light.ico", dark_icon="/static/favicon-dark.ico"),
|
49 |
Style(styles),
|
50 |
Script(src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"),
|
@@ -66,19 +68,19 @@ def home():
|
|
66 |
onclick="location.reload()",
|
67 |
_class="refresh-button"
|
68 |
),
|
69 |
-
_class=
|
70 |
),
|
71 |
_class="header"
|
72 |
),
|
73 |
Div(
|
74 |
Div(
|
75 |
Div(id="home-text-container", _class="markdown-container", **{"data-home-text": home_text}),
|
76 |
-
_class=
|
77 |
),
|
78 |
-
P(id=
|
79 |
Div(
|
80 |
Textarea(
|
81 |
-
id=
|
82 |
rows=1,
|
83 |
cols=50,
|
84 |
placeholder="Message FastGPT",
|
@@ -107,7 +109,6 @@ def home():
|
|
107 |
|
108 |
@app.get("/stream")
|
109 |
async def stream_response(request: Request, message: str, session_id: str = None):
|
110 |
-
"""Stream responses for the given user input."""
|
111 |
if not message:
|
112 |
raise HTTPException(status_code=400, detail="Message parameter is required")
|
113 |
if not session_id:
|
@@ -132,9 +133,7 @@ async def stream_response(request: Request, message: str, session_id: str = None
|
|
132 |
|
133 |
async for chunk in response:
|
134 |
if await request.is_disconnected():
|
135 |
-
print(f"Client for session {session_id} disconnected")
|
136 |
break
|
137 |
-
|
138 |
content = chunk.choices[0].delta.content
|
139 |
if content:
|
140 |
assistant_response += content
|
@@ -145,14 +144,10 @@ async def stream_response(request: Request, message: str, session_id: str = None
|
|
145 |
except Exception as e:
|
146 |
yield {"data": f"Error: {str(e)}"}
|
147 |
|
148 |
-
finally:
|
149 |
-
print(f"Streaming finished for session {session_id}")
|
150 |
-
|
151 |
return EventSourceResponse(event_generator())
|
152 |
|
153 |
@app.get("/reset")
|
154 |
def reset_conversation(session_id: str):
|
155 |
-
"""Reset the conversation for the specified session ID."""
|
156 |
if session_id in conversations:
|
157 |
del conversations[session_id]
|
158 |
-
return {"message": "Conversation reset."}
|
|
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
from openai import AsyncOpenAI
|
6 |
from fasthtml.common import FastHTML, Html, Head, Title, Body, Div, Button, Textarea, Script, Style, P, Favicon, ft_hx
|
|
|
|
|
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(override=True)
|
14 |
|
15 |
+
secret_key = os.getenv("SECRET_KEY")
|
16 |
+
hf_token = os.getenv("HF_TOKEN")
|
17 |
|
18 |
app = FastHTML(secret_key=secret_key)
|
19 |
|
20 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
21 |
|
22 |
+
setup_hf_backup(app, token=hf_token)
|
23 |
|
24 |
client = AsyncOpenAI()
|
25 |
|
|
|
33 |
dark_icon = os.path.join(static_dir, "favicon-dark.ico")
|
34 |
|
35 |
def Svg(*c, viewBox=None, **kwargs):
|
36 |
+
return ft_hx("svg", *c, viewBox=viewBox, **kwargs)
|
37 |
|
38 |
def Path(*c, d=None, fill=None, **kwargs):
|
39 |
+
return ft_hx("path", *c, d=d, fill=fill, **kwargs)
|
40 |
|
41 |
@app.get("/")
|
42 |
def home():
|
|
|
43 |
home_text = """
|
44 |
## FastGPT - A ChatGPT Implementation Using FastHTML
|
45 |
"""
|
46 |
+
|
47 |
page = Html(
|
48 |
Head(
|
49 |
+
Title("FastGPT"),
|
50 |
Favicon(light_icon="/static/favicon-light.ico", dark_icon="/static/favicon-dark.ico"),
|
51 |
Style(styles),
|
52 |
Script(src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"),
|
|
|
68 |
onclick="location.reload()",
|
69 |
_class="refresh-button"
|
70 |
),
|
71 |
+
_class="refresh-container"
|
72 |
),
|
73 |
_class="header"
|
74 |
),
|
75 |
Div(
|
76 |
Div(
|
77 |
Div(id="home-text-container", _class="markdown-container", **{"data-home-text": home_text}),
|
78 |
+
_class="title-wrapper"
|
79 |
),
|
80 |
+
P(id="output"),
|
81 |
Div(
|
82 |
Textarea(
|
83 |
+
id="message",
|
84 |
rows=1,
|
85 |
cols=50,
|
86 |
placeholder="Message FastGPT",
|
|
|
109 |
|
110 |
@app.get("/stream")
|
111 |
async def stream_response(request: Request, message: str, session_id: str = None):
|
|
|
112 |
if not message:
|
113 |
raise HTTPException(status_code=400, detail="Message parameter is required")
|
114 |
if not session_id:
|
|
|
133 |
|
134 |
async for chunk in response:
|
135 |
if await request.is_disconnected():
|
|
|
136 |
break
|
|
|
137 |
content = chunk.choices[0].delta.content
|
138 |
if content:
|
139 |
assistant_response += content
|
|
|
144 |
except Exception as e:
|
145 |
yield {"data": f"Error: {str(e)}"}
|
146 |
|
|
|
|
|
|
|
147 |
return EventSourceResponse(event_generator())
|
148 |
|
149 |
@app.get("/reset")
|
150 |
def reset_conversation(session_id: str):
|
|
|
151 |
if session_id in conversations:
|
152 |
del conversations[session_id]
|
153 |
+
return {"message": "Conversation reset."}
|