Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,25 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
|
| 24 |
# # CORS middleware setup
|
| 25 |
# app.add_middleware(
|
|
@@ -30,92 +30,87 @@
|
|
| 30 |
# allow_headers=["*"], # Specify headers or use ["*"] for all headers
|
| 31 |
# )
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
|
|
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
|
| 95 |
-
from fastapi.staticfiles import StaticFiles
|
| 96 |
-
from fastapi.responses import FileResponse, JSONResponse
|
| 97 |
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
@app.get("/")
|
| 104 |
-
def
|
| 105 |
-
|
| 106 |
-
|
| 107 |
|
| 108 |
-
@app.get("/api")
|
| 109 |
-
def read_api():
|
| 110 |
-
# This endpoint simply returns a JSON message.
|
| 111 |
-
return {"message": "Hello from the FastAPI API!"}
|
| 112 |
|
| 113 |
-
@app.get("/custom-auth")
|
| 114 |
-
def custom_auth():
|
| 115 |
-
# This is your custom authentication endpoint.
|
| 116 |
-
# Here, you should include your logic for authentication.
|
| 117 |
-
# For demonstration, it returns a JSON response with a token.
|
| 118 |
-
# token = "your_generated_token_here" # Placeholder token generation logic
|
| 119 |
-
token = create_jwt(cl.User(identifier="Test User"))
|
| 120 |
-
print("tokeeeeeee", token)
|
| 121 |
-
return JSONResponse({"token": token})
|
|
|
|
| 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(
|
|
|
|
| 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("/")
|
| 48 |
+
def read_root():
|
| 49 |
+
# Serve your static HTML file at the root.
|
| 50 |
+
logger.info(f"Serving static file for path")
|
| 51 |
+
return FileResponse('static/index.html')
|
| 52 |
+
|
| 53 |
+
@app.get("/api")
|
| 54 |
+
def read_api():
|
| 55 |
+
# This endpoint simply returns a JSON message.
|
| 56 |
+
return {"message": "Hello from the FastAPI API!"}
|
| 57 |
|
| 58 |
+
@app.get("/custom-auth")
|
| 59 |
+
def custom_auth():
|
| 60 |
+
# Verify the user's identity with custom logic.
|
| 61 |
+
token = create_jwt(cl.User(identifier="Test User"))
|
| 62 |
+
logger.info("Custom auth token generated.")
|
| 63 |
+
print("teeeeeee", token)
|
| 64 |
+
return JSONResponse({"token": token})
|
| 65 |
|
| 66 |
+
@cl.on_chat_start
|
| 67 |
+
async def on_chat_start():
|
| 68 |
+
cl.user_session.set(
|
| 69 |
+
"message_history",
|
| 70 |
+
[{"role": "system", "content": "You are a helpful assistant."}],
|
| 71 |
+
)
|
| 72 |
+
await cl.Message(content="Connected to Chainlit!").send()
|
| 73 |
+
logger.info("Chat started with Chainlit.")
|
| 74 |
|
| 75 |
+
@cl.on_message
|
| 76 |
+
async def on_message(message: cl.Message):
|
| 77 |
+
message_history = cl.user_session.get("message_history")
|
| 78 |
+
message_history.append({"role": "user", "content": message.content})
|
| 79 |
|
| 80 |
+
msg = cl.Message(content="")
|
| 81 |
+
await msg.send()
|
| 82 |
|
| 83 |
+
stream = await client.chat.completions.create(
|
| 84 |
+
messages=message_history, stream=True, **settings
|
| 85 |
+
)
|
| 86 |
|
| 87 |
+
async for part in stream:
|
| 88 |
+
if token := part.choices[0].delta.content or "":
|
| 89 |
+
await msg.stream_token(token)
|
| 90 |
|
| 91 |
+
message_history.append({"role": "assistant", "content": msg.content})
|
| 92 |
+
await msg.update()
|
| 93 |
+
logger.info("Message processed and response sent.")
|
| 94 |
|
| 95 |
+
#########################################################################################################################
|
| 96 |
|
| 97 |
+
# from fastapi import FastAPI
|
| 98 |
+
# from fastapi.staticfiles import StaticFiles
|
| 99 |
+
# from fastapi.responses import FileResponse, JSONResponse
|
| 100 |
|
| 101 |
+
# app = FastAPI()
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
# # Assuming your static files are in a directory named 'static'.
|
| 104 |
+
# app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 105 |
|
| 106 |
+
# @app.get("/")
|
| 107 |
+
# def read_root():
|
| 108 |
+
# # Serve your static HTML file at the root.
|
| 109 |
+
# return FileResponse('static/index.html')
|
| 110 |
|
| 111 |
+
# @app.get("/api")
|
| 112 |
+
# def read_api():
|
| 113 |
+
# # This endpoint simply returns a JSON message.
|
| 114 |
+
# return {"message": "Hello from the FastAPI API!"}
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|