Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app/main.py +11 -4
- app/routers/__init__.py +0 -0
- app/routers/image/__init__.py +0 -0
- app/routers/testers/calculator.py +37 -0
- app/routers/testers/help.py +19 -0
- app/routers/testers/random_number_generator.py +36 -0
app/main.py
CHANGED
|
@@ -1,14 +1,21 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
# Include all routers
|
| 8 |
app.include_router(random_number_generator.router)
|
| 9 |
app.include_router(help.router)
|
| 10 |
-
app.include_router(calculator.router)
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@app.get("/")
|
| 13 |
def greet_json():
|
| 14 |
-
return {"
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
|
| 3 |
+
# testers routers
|
| 4 |
+
from app.routers.testers import random_number_generator
|
| 5 |
+
from app.routers.testers import help
|
| 6 |
+
from app.routers.testers import calculator
|
| 7 |
+
|
| 8 |
+
# add your more module here
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
# Include all routers
|
| 13 |
app.include_router(random_number_generator.router)
|
| 14 |
app.include_router(help.router)
|
| 15 |
+
app.include_router(calculator.router)
|
| 16 |
+
|
| 17 |
+
# add more router here
|
| 18 |
|
| 19 |
@app.get("/")
|
| 20 |
def greet_json():
|
| 21 |
+
return {"message": "Go to /docs to see all available endpoints!", "link" : "https://sameerbanchhor-multi-use-api.hf.space/docs"}
|
app/routers/__init__.py
ADDED
|
File without changes
|
app/routers/image/__init__.py
ADDED
|
File without changes
|
app/routers/testers/calculator.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter
|
| 2 |
+
|
| 3 |
+
# Create the router for calculator endpoints
|
| 4 |
+
router = APIRouter(
|
| 5 |
+
prefix="/calculate",
|
| 6 |
+
tags=["Calculator"]
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
@router.get("/add")
|
| 10 |
+
def add_numbers(a: float, b: float):
|
| 11 |
+
"""
|
| 12 |
+
Adds two numbers, 'a' and 'b'.
|
| 13 |
+
|
| 14 |
+
Query parameters: a (float), b (float)
|
| 15 |
+
"""
|
| 16 |
+
result = a + b
|
| 17 |
+
return {
|
| 18 |
+
"operation": "addition",
|
| 19 |
+
"a": a,
|
| 20 |
+
"b": b,
|
| 21 |
+
"result": result
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
@router.get("/subtract")
|
| 25 |
+
def subtract_numbers(a: float, b: float):
|
| 26 |
+
"""
|
| 27 |
+
Subtracts number 'b' from number 'a' (a - b).
|
| 28 |
+
|
| 29 |
+
Query parameters: a (float), b (float)
|
| 30 |
+
"""
|
| 31 |
+
result = a - b
|
| 32 |
+
return {
|
| 33 |
+
"operation": "subtraction",
|
| 34 |
+
"a": a,
|
| 35 |
+
"b": b,
|
| 36 |
+
"result": result
|
| 37 |
+
}
|
app/routers/testers/help.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter
|
| 2 |
+
|
| 3 |
+
# Create the router for help/info endpoints
|
| 4 |
+
router = APIRouter(
|
| 5 |
+
prefix="/help",
|
| 6 |
+
tags=["Information"]
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
@router.get("/")
|
| 10 |
+
def get_api_info():
|
| 11 |
+
"""
|
| 12 |
+
Returns general information about the API and available endpoints.
|
| 13 |
+
"""
|
| 14 |
+
return {
|
| 15 |
+
"api_name": "My Awesome FastAPI",
|
| 16 |
+
"version": "1.0",
|
| 17 |
+
"available_sections": ["/random", "/help"],
|
| 18 |
+
"message": "Use the /docs endpoint for interactive API documentation."
|
| 19 |
+
}
|
app/routers/testers/random_number_generator.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
from fastapi import APIRouter
|
| 3 |
+
|
| 4 |
+
# Create a new APIRouter instance
|
| 5 |
+
# We'll set a prefix so all paths in this router start with /random
|
| 6 |
+
router = APIRouter(
|
| 7 |
+
prefix="/random",
|
| 8 |
+
tags=["Random Numbers"] # Optional: helps organize documentation (Swagger UI)
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
@router.get("/integer")
|
| 12 |
+
def get_random_integer(min_val: int = 1, max_val: int = 100):
|
| 13 |
+
"""
|
| 14 |
+
Generate a random integer between min_val (inclusive) and max_val (inclusive).
|
| 15 |
+
|
| 16 |
+
Defaults to a number between 1 and 100.
|
| 17 |
+
"""
|
| 18 |
+
if min_val > max_val:
|
| 19 |
+
# Swap if min is greater than max to ensure the range is valid
|
| 20 |
+
min_val, max_val = max_val, min_val
|
| 21 |
+
|
| 22 |
+
random_int = random.randint(min_val, max_val)
|
| 23 |
+
|
| 24 |
+
return {
|
| 25 |
+
"min_value": min_val,
|
| 26 |
+
"max_value": max_val,
|
| 27 |
+
"random_number": random_int
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
@router.get("/float")
|
| 31 |
+
def get_random_float():
|
| 32 |
+
"""
|
| 33 |
+
Generate a random floating-point number between 0.0 (inclusive) and 1.0 (exclusive).
|
| 34 |
+
"""
|
| 35 |
+
random_float = random.random()
|
| 36 |
+
return {"random_float": random_float}
|