# | |
# SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org> | |
# SPDX-License-Identifier: Apache-2.0 | |
# | |
import asyncio # Import asyncio for asynchronous programming support in Python | |
import json # Import json module to handle JSON encoding and decoding | |
import uvicorn # Import uvicorn, an ASGI server implementation for running FastAPI applications | |
from contextlib import asynccontextmanager # Import asynccontextmanager to create asynchronous context managers if needed | |
from fastapi import FastAPI # Import FastAPI class to create the main web application instance | |
from fastapi.responses import Response # Import Response class to send raw HTTP responses | |
from fastapi.responses import JSONResponse # Import JSONResponse class for sending JSON formatted HTTP responses | |
from src.routes.v1 import responses, chat_completions, models, history # Import router modules from the src.routes.v1 package to organize API endpoints | |
# Initialize a FastAPI application | |
app = FastAPI() | |
# Include the 'responses' router under the '/v1' prefix with the tag "Responses" | |
# This router handles endpoints related to general responses | |
app.include_router(responses.router, prefix="/v1", tags=["Responses"]) | |
# Include the 'chat_completions' router under the '/v1/chat' prefix with the tag "Chat Completions" | |
# This router manages chat completion related API endpoints | |
app.include_router(chat_completions.router, prefix="/v1/chat", tags=["Chat Completions"]) | |
# Include the 'models' router under the '/v1' prefix with the tag "Models" | |
# This router provides endpoints related to available models | |
app.include_router(models.router, prefix="/v1", tags=["Models"]) | |
# Include the 'history' router under the '/v1' prefix with the tag "History" | |
# This router manages API endpoints related to user or session history | |
app.include_router(history.router, prefix="/v1", tags=["History"]) | |
# Define a root path GET endpoint for the base URL '/' | |
# This endpoint acts as a health check to confirm the API is operational | |
def root(): | |
""" | |
Health check endpoint that returns a JSON response with API status information. | |
It confirms the API is running and lists active routers with their URLs and statuses. | |
This is useful for monitoring and basic connectivity testing. | |
""" | |
# Create a dictionary containing status information and URLs of active routers | |
data = { | |
"Status": "API is running!", | |
"Endpoint": "https://hadadrjt-api.hf.space/v1", | |
"Type": "OpenAI-style", | |
"Router 1": { | |
"URL": "https://hadadrjt-api.hf.space/v1/chat/completions", | |
"Status": "Active" | |
}, | |
"Router 2": { | |
"URL": "https://hadadrjt-api.hf.space/v1/responses", | |
"Status": "Active" | |
}, | |
"Router 3": { | |
"URL": "https://hadadrjt-api.hf.space/v1/models", | |
"Status": "Active" | |
}, | |
"Router 4": { | |
"URL": "https://hadadrjt-api.hf.space/v1/history", | |
"Status": "Active" | |
} | |
} | |
# Convert the dictionary to a pretty-printed JSON string with indentation for readability | |
json_content = json.dumps(data, indent=4) | |
# Return the JSON string as an HTTP response with content type set to application/json | |
return Response(content=json_content, media_type="application/json") | |
# Check if this script is being run directly (not imported as a module) | |
if __name__ == "__main__": | |
# Run the FastAPI app using the Uvicorn ASGI server | |
# Bind to all available network interfaces on port 7860 | |
uvicorn.run(app, host="0.0.0.0", port=7860) | |