Spaces:
Sleeping
Sleeping
Download app/routers/random.py from Salahin/random-api: direct link, hf CLI and curl.
- Browser
- Download file 1.85 kB
-
https://huggingface.co/spaces/Salahin/random-api/resolve/main/app/routers/random.py
- Command line
-
hf download hf://spaces/Salahin/random-api/app/routers/random.py
-
curl -L -o random.py https://huggingface.co/spaces/Salahin/random-api/resolve/main/app/routers/random.py
1.85 kB
| from fastapi import APIRouter, HTTPException | |
| import json | |
| import string | |
| from utils.utils import * | |
| router = APIRouter(prefix="/random") | |
| def get_root() -> dict: | |
| """Return a random float between 0 and 1.""" | |
| return {"float": random_float()} | |
| def get_random_int() -> dict: | |
| """Return a random integer.""" | |
| return {"int": random_int()} | |
| def get_random_in_range(left: int, right: int) -> dict: | |
| """Return a random integer within the given range.""" | |
| if left > right: | |
| raise HTTPException(status_code=400, detail="Invalid range") | |
| return {"int": random_int(left, right)} | |
| def generate_random_char() -> dict: | |
| """Get a random ASCII character.""" | |
| return {"char": random_choice(string.ascii_letters)} | |
| def toss_coin() -> dict: | |
| """Simulate a coin toss and return the result.""" | |
| return {"result": random_choice(["Heads", "Tails"])} | |
| def get_random_option(options: str) -> dict: | |
| """Select a random option from the given list.""" | |
| options_list = json.loads(options) | |
| if not options_list: | |
| raise HTTPException(status_code=400, detail="Invalid options") | |
| return {"selected": random_choice(options_list)} | |
| def get_color() -> dict: | |
| """Select a random hex color.""" | |
| hex_color = "#" + \ | |
| "".join([random_choice('0123456789ABCDEF') for _ in range(6)]) | |
| return {"color": hex_color} | |
| def get_user() -> User: | |
| """Generate a random user with name, email, and password.""" | |
| return random_user() | |
| def get_password(length: int) -> dict: | |
| """Generate a random password of a specified length.""" | |
| return {"password": random_password(length=length)} | |