Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,109 @@
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
| 2 |
from fastapi.staticfiles import StaticFiles
|
| 3 |
-
from
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return FileResponse('static/index.html')
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
from fastapi import FastAPI
|
| 4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
+
from fastapi.responses import JSONResponse, FileResponse
|
| 6 |
from fastapi.staticfiles import StaticFiles
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from openai import AsyncOpenAI
|
| 9 |
|
| 10 |
+
# Import your custom modules
|
| 11 |
+
from chainlit.auth import create_jwt
|
| 12 |
+
import chainlit as cl
|
| 13 |
+
import uvicorn
|
| 14 |
+
# Load environment variables from .env file
|
| 15 |
+
load_dotenv()
|
| 16 |
+
|
| 17 |
+
# Initialize logging
|
| 18 |
+
logging.basicConfig(level=logging.INFO)
|
| 19 |
+
logger = logging.getLogger(__name__) # Use __name__ to get the root logger
|
| 20 |
+
|
| 21 |
+
# Initialize FastAPI app
|
| 22 |
app = FastAPI()
|
| 23 |
|
| 24 |
+
# CORS middleware setup
|
| 25 |
+
app.add_middleware(
|
| 26 |
+
CORSMiddleware,
|
| 27 |
+
allow_origins=["*"], # Specify domains or use ["*"] for open access
|
| 28 |
+
allow_credentials=True,
|
| 29 |
+
allow_methods=["*"], # Specify methods or use ["*"] for all methods
|
| 30 |
+
allow_headers=["*"], # Specify headers or use ["*"] for all headers
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
client = AsyncOpenAI(api_key=os.environ["OPENAI_API_KEY"])
|
| 34 |
+
|
| 35 |
+
settings = {
|
| 36 |
+
"model": "gpt-3.5-turbo",
|
| 37 |
+
"temperature": 0.7,
|
| 38 |
+
"max_tokens": 500,
|
| 39 |
+
"top_p": 1,
|
| 40 |
+
"frequency_penalty": 0,
|
| 41 |
+
"presence_penalty": 0,
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
| 45 |
+
logger.info("Static files are being served from the 'static' directory.")
|
| 46 |
+
|
| 47 |
+
@app.get("/custom-auth")
|
| 48 |
+
async def custom_auth():
|
| 49 |
+
# Verify the user's identity with custom logic.
|
| 50 |
+
token = create_jwt(cl.User(identifier="Test User"))
|
| 51 |
+
logger.info("Custom auth token generated.")
|
| 52 |
+
return JSONResponse({"token": token})
|
| 53 |
+
|
| 54 |
+
@cl.on_chat_start
|
| 55 |
+
async def on_chat_start():
|
| 56 |
+
cl.user_session.set(
|
| 57 |
+
"message_history",
|
| 58 |
+
[{"role": "system", "content": "You are a helpful assistant."}],
|
| 59 |
+
)
|
| 60 |
+
await cl.Message(content="Connected to Chainlit!").send()
|
| 61 |
+
logger.info("Chat started with Chainlit.")
|
| 62 |
+
|
| 63 |
+
@cl.on_message
|
| 64 |
+
async def on_message(message: cl.Message):
|
| 65 |
+
message_history = cl.user_session.get("message_history")
|
| 66 |
+
message_history.append({"role": "user", "content": message.content})
|
| 67 |
|
| 68 |
+
msg = cl.Message(content="")
|
| 69 |
+
await msg.send()
|
| 70 |
+
|
| 71 |
+
stream = await client.chat.completions.create(
|
| 72 |
+
messages=message_history, stream=True, **settings
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
async for part in stream:
|
| 76 |
+
if token := part.choices[0].delta.content or "":
|
| 77 |
+
await msg.stream_token(token)
|
| 78 |
+
|
| 79 |
+
message_history.append({"role": "assistant", "content": msg.content})
|
| 80 |
+
await msg.update()
|
| 81 |
+
logger.info("Message processed and response sent.")
|
| 82 |
+
|
| 83 |
+
@app.get("/{full_path:path}")
|
| 84 |
+
async def catch_all(full_path: str):
|
| 85 |
+
"""
|
| 86 |
+
Catch-all route to serve index.html for any undefined routes,
|
| 87 |
+
allowing client-side routing to function properly.
|
| 88 |
+
"""
|
| 89 |
+
logger.info(f"Serving static file for path: {full_path}")
|
| 90 |
return FileResponse('static/index.html')
|
| 91 |
|
| 92 |
+
# from fastapi import FastAPI
|
| 93 |
+
# from fastapi.staticfiles import StaticFiles
|
| 94 |
+
# from fastapi.responses import FileResponse
|
| 95 |
+
|
| 96 |
+
# app = FastAPI()
|
| 97 |
+
|
| 98 |
+
# # Mount the 'static' directory to serve static files.
|
| 99 |
+
# # Assuming your static files are in a directory named 'static'.
|
| 100 |
+
# app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 101 |
+
|
| 102 |
+
# @app.get("/")
|
| 103 |
+
# def read_root():
|
| 104 |
+
# # Serve your static HTML file at the root.
|
| 105 |
+
# return FileResponse('static/index.html')
|
| 106 |
+
|
| 107 |
+
# @app.get("/api")
|
| 108 |
+
# def read_api():
|
| 109 |
+
# return {"message": "Hello from the FastAPI API!"}
|