Jack
commited on
Commit
·
54ffdc7
1
Parent(s):
03950f4
removed comments from main.py
Browse files
main.py
CHANGED
|
@@ -10,42 +10,31 @@ from styles import styles
|
|
| 10 |
from script import script
|
| 11 |
from fasthtml_hf import setup_hf_backup
|
| 12 |
|
| 13 |
-
# Set the secret key from the environment or use default
|
| 14 |
secret_key = os.getenv('SECRET_KEY')
|
| 15 |
|
| 16 |
-
# Initialize FastHTML with the secret key
|
| 17 |
app = FastHTML(secret_key=secret_key)
|
| 18 |
|
| 19 |
-
# Mount static files for favicon and other static assets
|
| 20 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 21 |
|
| 22 |
-
# Setup Hugging Face backup with writable directory
|
| 23 |
setup_hf_backup(app)
|
| 24 |
|
| 25 |
-
# OpenAI client instance
|
| 26 |
client = AsyncOpenAI()
|
| 27 |
|
| 28 |
-
# Store user conversations by session ID
|
| 29 |
conversations = {}
|
| 30 |
|
| 31 |
-
# Bleach allowed tags and attributes for sanitization
|
| 32 |
ALLOWED_TAGS = list(bleach.sanitizer.ALLOWED_TAGS) + ["h1", "h2", "h3", "p", "strong", "em", "ul", "ol", "li", "code", "pre", "blockquote"]
|
| 33 |
ALLOWED_ATTRIBUTES = bleach.sanitizer.ALLOWED_ATTRIBUTES
|
| 34 |
|
| 35 |
-
# Static file paths
|
| 36 |
static_dir = os.path.join(os.path.dirname(__file__), "static")
|
| 37 |
light_icon = os.path.join(static_dir, "favicon-light.ico")
|
| 38 |
dark_icon = os.path.join(static_dir, "favicon-dark.ico")
|
| 39 |
|
| 40 |
-
# Custom SVG component
|
| 41 |
def Svg(*c, viewBox=None, **kwargs):
|
| 42 |
return ft_hx('svg', *c, viewBox=viewBox, **kwargs)
|
| 43 |
|
| 44 |
-
# Custom Path component for SVG
|
| 45 |
def Path(*c, d=None, fill=None, **kwargs):
|
| 46 |
return ft_hx('path', *c, d=d, fill=fill, **kwargs)
|
| 47 |
|
| 48 |
-
# Homepage route
|
| 49 |
@app.get("/")
|
| 50 |
def home():
|
| 51 |
"""Render homepage with FastGPT UI."""
|
|
@@ -56,7 +45,7 @@ def home():
|
|
| 56 |
page = Html(
|
| 57 |
Head(
|
| 58 |
Title('FastGPT'),
|
| 59 |
-
Favicon(light_icon="/static/favicon-light.ico", dark_icon="/static/favicon-dark.ico"),
|
| 60 |
Style(styles),
|
| 61 |
Script(src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"),
|
| 62 |
Script(src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.2.9/purify.min.js")
|
|
@@ -116,7 +105,6 @@ def home():
|
|
| 116 |
)
|
| 117 |
return page
|
| 118 |
|
| 119 |
-
# Route to stream responses based on user input
|
| 120 |
@app.get("/stream")
|
| 121 |
async def stream_response(request: Request, message: str, session_id: str = None):
|
| 122 |
"""Stream responses for the given user input."""
|
|
@@ -125,18 +113,15 @@ async def stream_response(request: Request, message: str, session_id: str = None
|
|
| 125 |
if not session_id:
|
| 126 |
raise HTTPException(status_code=400, detail="Session ID is required")
|
| 127 |
|
| 128 |
-
# Initialize conversation if the session ID is new
|
| 129 |
if session_id not in conversations:
|
| 130 |
conversations[session_id] = [
|
| 131 |
{"role": "system", "content": "You are a helpful assistant. Use Markdown for formatting."}
|
| 132 |
]
|
| 133 |
|
| 134 |
-
# Add user's message to the conversation
|
| 135 |
conversations[session_id].append({"role": "user", "content": message})
|
| 136 |
|
| 137 |
async def event_generator():
|
| 138 |
try:
|
| 139 |
-
# Stream response from OpenAI
|
| 140 |
response = await client.chat.completions.create(
|
| 141 |
model="gpt-4o-mini",
|
| 142 |
messages=conversations[session_id],
|
|
@@ -145,7 +130,6 @@ async def stream_response(request: Request, message: str, session_id: str = None
|
|
| 145 |
|
| 146 |
assistant_response = ""
|
| 147 |
|
| 148 |
-
# Stream each chunk of the response
|
| 149 |
async for chunk in response:
|
| 150 |
if await request.is_disconnected():
|
| 151 |
print(f"Client for session {session_id} disconnected")
|
|
@@ -156,7 +140,6 @@ async def stream_response(request: Request, message: str, session_id: str = None
|
|
| 156 |
assistant_response += content
|
| 157 |
yield {"data": content}
|
| 158 |
|
| 159 |
-
# Store assistant's full response
|
| 160 |
conversations[session_id].append({"role": "assistant", "content": assistant_response})
|
| 161 |
|
| 162 |
except Exception as e:
|
|
@@ -167,7 +150,6 @@ async def stream_response(request: Request, message: str, session_id: str = None
|
|
| 167 |
|
| 168 |
return EventSourceResponse(event_generator())
|
| 169 |
|
| 170 |
-
# Route to reset the conversation for a given session ID
|
| 171 |
@app.get("/reset")
|
| 172 |
def reset_conversation(session_id: str):
|
| 173 |
"""Reset the conversation for the specified session ID."""
|
|
|
|
| 10 |
from script import script
|
| 11 |
from fasthtml_hf import setup_hf_backup
|
| 12 |
|
|
|
|
| 13 |
secret_key = os.getenv('SECRET_KEY')
|
| 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 |
|
|
|
|
| 23 |
conversations = {}
|
| 24 |
|
|
|
|
| 25 |
ALLOWED_TAGS = list(bleach.sanitizer.ALLOWED_TAGS) + ["h1", "h2", "h3", "p", "strong", "em", "ul", "ol", "li", "code", "pre", "blockquote"]
|
| 26 |
ALLOWED_ATTRIBUTES = bleach.sanitizer.ALLOWED_ATTRIBUTES
|
| 27 |
|
|
|
|
| 28 |
static_dir = os.path.join(os.path.dirname(__file__), "static")
|
| 29 |
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('svg', *c, viewBox=viewBox, **kwargs)
|
| 34 |
|
|
|
|
| 35 |
def Path(*c, d=None, fill=None, **kwargs):
|
| 36 |
return ft_hx('path', *c, d=d, fill=fill, **kwargs)
|
| 37 |
|
|
|
|
| 38 |
@app.get("/")
|
| 39 |
def home():
|
| 40 |
"""Render homepage with FastGPT UI."""
|
|
|
|
| 45 |
page = Html(
|
| 46 |
Head(
|
| 47 |
Title('FastGPT'),
|
| 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"),
|
| 51 |
Script(src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.2.9/purify.min.js")
|
|
|
|
| 105 |
)
|
| 106 |
return page
|
| 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."""
|
|
|
|
| 113 |
if not session_id:
|
| 114 |
raise HTTPException(status_code=400, detail="Session ID is required")
|
| 115 |
|
|
|
|
| 116 |
if session_id not in conversations:
|
| 117 |
conversations[session_id] = [
|
| 118 |
{"role": "system", "content": "You are a helpful assistant. Use Markdown for formatting."}
|
| 119 |
]
|
| 120 |
|
|
|
|
| 121 |
conversations[session_id].append({"role": "user", "content": message})
|
| 122 |
|
| 123 |
async def event_generator():
|
| 124 |
try:
|
|
|
|
| 125 |
response = await client.chat.completions.create(
|
| 126 |
model="gpt-4o-mini",
|
| 127 |
messages=conversations[session_id],
|
|
|
|
| 130 |
|
| 131 |
assistant_response = ""
|
| 132 |
|
|
|
|
| 133 |
async for chunk in response:
|
| 134 |
if await request.is_disconnected():
|
| 135 |
print(f"Client for session {session_id} disconnected")
|
|
|
|
| 140 |
assistant_response += content
|
| 141 |
yield {"data": content}
|
| 142 |
|
|
|
|
| 143 |
conversations[session_id].append({"role": "assistant", "content": assistant_response})
|
| 144 |
|
| 145 |
except Exception as e:
|
|
|
|
| 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."""
|