Buckets:

download
raw
8.81 kB
import os
import asyncio
import warnings
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Optional
from urllib.parse import urlencode
import nest_asyncio # type: ignore
from fastapi import FastAPI, Request, Response, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from http import HTTPStatus
from loguru import logger
from pydantic import PydanticDeprecatedSince20
from rich import print as rprint
from starlette.middleware.base import BaseHTTPMiddleware
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from langflow.api import router, health_check_router, log_router
from langflow.initial_setup.setup import (
create_or_update_starter_projects,
initialize_super_user_if_needed,
load_flows_from_directory,
)
from langflow.interface.types import get_and_cache_all_types_dict
from langflow.interface.utils import setup_llm_caching
from langflow.services.deps import get_cache_service, get_settings_service, get_telemetry_service
from langflow.services.plugins.langfuse_plugin import LangfuseInstance
from langflow.services.utils import initialize_services, teardown_services
from langflow.utils.logger import configure
# Ignore Pydantic deprecation warnings from Langchain
warnings.filterwarnings("ignore", category=PydanticDeprecatedSince20)
class RequestCancelledMiddleware(BaseHTTPMiddleware):
def __init__(self, app):
super().__init__(app)
async def dispatch(self, request: Request, call_next):
sentinel = object()
async def cancel_handler():
while True:
if await request.is_disconnected():
return sentinel
await asyncio.sleep(0.1)
handler_task = asyncio.create_task(call_next(request))
cancel_task = asyncio.create_task(cancel_handler())
done, pending = await asyncio.wait([handler_task, cancel_task], return_when=asyncio.FIRST_COMPLETED)
for task in pending:
task.cancel()
if cancel_task in done:
return Response("Request was cancelled", status_code=499)
else:
return await handler_task
class JavaScriptMIMETypeMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
try:
response = await call_next(request)
except Exception as exc:
logger.error(exc)
raise exc
if "files/" not in request.url.path and request.url.path.endswith(".js") and response.status_code == 200:
response.headers["Content-Type"] = "text/javascript"
return response
def get_lifespan(fix_migration=False, socketio_server=None, version=None):
@asynccontextmanager
async def lifespan(app: FastAPI):
nest_asyncio.apply()
# Startup message
if version:
rprint(f"[bold green]Starting Langflow v{version}...[/bold green]")
else:
rprint("[bold green]Starting Langflow...[/bold green]")
try:
initialize_services(fix_migration=fix_migration, socketio_server=socketio_server)
setup_llm_caching()
LangfuseInstance.update()
initialize_super_user_if_needed()
task = asyncio.create_task(get_and_cache_all_types_dict(get_settings_service(), get_cache_service()))
await create_or_update_starter_projects(task)
asyncio.create_task(get_telemetry_service().start())
load_flows_from_directory()
yield
except Exception as exc:
if "langflow migration --fix" not in str(exc):
logger.error(exc)
raise
# Shutdown message
rprint("[bold red]Shutting down Langflow...[/bold red]")
await teardown_services()
return lifespan
def create_app():
"""Create the FastAPI app and include the router."""
try:
from langflow.version import __version__ # type: ignore
except ImportError:
from importlib.metadata import version
__version__ = version("langflow-base")
configure()
lifespan = get_lifespan(version=__version__)
app = FastAPI(lifespan=lifespan, title="Langflow", version=__version__)
setup_sentry(app)
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(JavaScriptMIMETypeMiddleware)
# ! Deactivating this until we find a better solution
# app.add_middleware(RequestCancelledMiddleware)
@app.middleware("http")
async def flatten_query_string_lists(request: Request, call_next):
flattened: list[tuple[str, str]] = []
for key, value in request.query_params.multi_items():
flattened.extend((key, entry) for entry in value.split(","))
request.scope["query_string"] = urlencode(flattened, doseq=True).encode("utf-8")
return await call_next(request)
settings = get_settings_service().settings
if prome_port_str := os.environ.get("LANGFLOW_PROMETHEUS_PORT"):
# set here for create_app() entry point
prome_port = int(prome_port_str)
if prome_port > 0 or prome_port < 65535:
rprint(f"[bold green]Starting Prometheus server on port {prome_port}...[/bold green]")
settings.prometheus_enabled = True
settings.prometheus_port = prome_port
else:
raise ValueError(f"Invalid port number {prome_port_str}")
if settings.prometheus_enabled:
from prometheus_client import start_http_server
start_http_server(settings.prometheus_port)
app.include_router(router)
app.include_router(health_check_router)
app.include_router(log_router)
@app.exception_handler(Exception)
async def exception_handler(request: Request, exc: Exception):
if isinstance(exc, HTTPException):
logger.error(f"HTTPException: {exc.detail}")
return JSONResponse(
status_code=exc.status_code,
content={"message": str(exc.detail)},
)
else:
logger.error(f"unhandled error: {exc}")
return JSONResponse(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
content={"message": str(exc)},
)
FastAPIInstrumentor.instrument_app(app)
return app
def setup_sentry(app: FastAPI):
settings = get_settings_service().settings
if settings.sentry_dsn:
import sentry_sdk
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
sentry_sdk.init(
dsn=settings.sentry_dsn,
traces_sample_rate=settings.sentry_traces_sample_rate,
profiles_sample_rate=settings.sentry_profiles_sample_rate,
)
app.add_middleware(SentryAsgiMiddleware)
def setup_static_files(app: FastAPI, static_files_dir: Path):
"""
Setup the static files directory.
Args:
app (FastAPI): FastAPI app.
path (str): Path to the static files directory.
"""
app.mount(
"/",
StaticFiles(directory=static_files_dir, html=True),
name="static",
)
@app.exception_handler(404)
async def custom_404_handler(request, __):
path = static_files_dir / "index.html"
if not path.exists():
raise RuntimeError(f"File at path {path} does not exist.")
return FileResponse(path)
def get_static_files_dir():
"""Get the static files directory relative to Langflow's main.py file."""
frontend_path = Path(__file__).parent
return frontend_path / "frontend"
def setup_app(static_files_dir: Optional[Path] = None, backend_only: bool = False) -> FastAPI:
"""Setup the FastAPI app."""
# get the directory of the current file
logger.info(f"Setting up app with static files directory {static_files_dir}")
if not static_files_dir:
static_files_dir = get_static_files_dir()
if not backend_only and (not static_files_dir or not static_files_dir.exists()):
raise RuntimeError(f"Static files directory {static_files_dir} does not exist.")
app = create_app()
if not backend_only and static_files_dir is not None:
setup_static_files(app, static_files_dir)
return app
if __name__ == "__main__":
import uvicorn
from langflow.__main__ import get_number_of_workers
configure()
uvicorn.run(
"langflow.main:create_app",
host="127.0.0.1",
port=7860,
workers=get_number_of_workers(),
log_level="error",
reload=True,
loop="asyncio",
)

Xet Storage Details

Size:
8.81 kB
·
Xet hash:
bc2bf2b17185711a08354fb8f2c96048f5ca82989d4d3f1bf3f41c6cb8c28a06

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.