Spaces:
Sleeping
Sleeping
Download streaming_api.py from lyx51/data_generator_API: direct link, hf CLI and curl.
- Browser
- Download file 6.82 kB
-
https://huggingface.co/spaces/lyx51/data_generator_API/resolve/main/streaming_api.py
- Command line
-
hf download hf://spaces/lyx51/data_generator_API/streaming_api.py
-
curl -L -o streaming_api.py https://huggingface.co/spaces/lyx51/data_generator_API/resolve/main/streaming_api.py
6.82 kB
| from fastapi import FastAPI | |
| import random | |
| import threading | |
| import time | |
| from datetime import datetime, timedelta | |
| app = FastAPI(title="Live Car Rental API", version="1.0") | |
| GENERATION_INTERVAL = 60 # secondes entre chaque nouvelle ligne | |
| GENERATE_RENT = True # True pour calculer rental_price, False pour 0 | |
| brands_list = [ | |
| ("Renault", 16.1, 30000, 50, 160, True, True, False), | |
| ("Peugeot", 13.5, 33000, 50, 325, True, True, False), | |
| ("Citroen", 6.5, 28000, 75, 230, True, True, False), | |
| ("Dacia", 8.4, 23000, 45, 100, False, False, False), | |
| ("Volkswagen", 7.0, 35000, 80, 394, True, True, False), | |
| ("Toyota", 7.4, 34000, 70, 220, True, True, False), | |
| ("BMW", 4.2, 54000, 100, 450, True, True, True), | |
| ("Mercedes-Benz", 3.2, 75000, 100, 600, True, True, True), | |
| ("Audi", 2.8, 60000, 90, 580, True, True, True), | |
| ("Ford", 2.6, 30000, 70, 270, True, True, False), | |
| ("Fiat", 2.1, 27000, 60, 160, False, True, False), | |
| ("Kia", 2.1, 28000, 65, 260, True, True, False), | |
| ("SEAT", 1.3, 28000, 70, 270, True, True, False), | |
| ("Mini", 0.9, 32000, 75, 200, True, False, False), | |
| ("Porsche", 0.4, 160000, 300, 1020, True, False, True), | |
| ("Tesla", 2.0, 58000, 250, 1020, True, False, True), | |
| ("Opel", 1.7, 27000, 60, 160, False, True, False), | |
| ("Nissan", 1.8, 28000, 70, 220, True, True, False), | |
| ("Honda", 0.3, 29000, 70, 200, False, True, False), | |
| ("Mazda", 0.5, 29000, 75, 200, False, True, False), | |
| ("Suzuki", 0.8, 21000, 45, 140, False, False, False), | |
| ("Lexus", 0.3, 55000, 150, 450, True, True, True), | |
| ("Mitsubishi", 0.2, 26000, 70, 160, True, True, False), | |
| ("Alfa Romeo", 0.2, 40000, 110, 300, False, False, True), | |
| ("Subaru", 0.1, 28000, 70, 220, False, False, False) | |
| ] | |
| years = list(range(2010, 2025)) | |
| years_weights = [10, 9, 8, 8, 7, 7, 6, 6, 6, 6, 7, 6, 6, 6, 6] | |
| fuel_by_year = { | |
| 2010: {"diesel": 0.67, "petrol": 0.34, "hybrid": 0.005, "electric": 0}, | |
| 2015: {"diesel": 0.60, "petrol": 0.38, "hybrid": 0.02, "electric": 0.004}, | |
| 2020: {"diesel": 0.30, "petrol": 0.48, "hybrid": 0.148, "electric": 0.067}, | |
| 2024: {"diesel": 0.13, "petrol": 0.24, "hybrid": 0.44, "electric": 0.18} | |
| } | |
| CAR_TYPES = ["hatchback", "estate", "sedan", "suv", "convertible", "coupe"] | |
| CAR_TYPE_WEIGHTS = [0.35, 0.10, 0.15, 0.25, 0.05, 0.10] | |
| car_type_price_factors = { | |
| "hatchback": 1.00, | |
| "estate": 1.05, | |
| "sedan": 1.05, | |
| "suv": 1.15, | |
| "convertible": 1.10, | |
| "coupe": 1.10 | |
| } | |
| PAINT_COLORS = ["black", "grey", "white", "silver", "blue", "red", "brown", "green", "beige", "orange"] | |
| def yesno(prob): | |
| return random.random() < prob | |
| def get_fuel(year, allows_electric, allows_hybrid, is_tesla): | |
| step = 2010 if year < 2015 else 2015 if year < 2020 else 2020 if year < 2024 else 2024 | |
| dist = fuel_by_year[step].copy() | |
| if not allows_electric or is_tesla: | |
| dist.pop("electric", None) | |
| if not allows_hybrid: | |
| dist.pop("hybrid", None) | |
| if is_tesla: | |
| return "electric" | |
| total = sum(dist.values()) | |
| fracs = {k: v / total for k, v in dist.items()} | |
| return random.choices(list(fracs.keys()), weights=list(fracs.values()), k=1)[0] | |
| def get_car_type(): | |
| return random.choices(CAR_TYPES, weights=CAR_TYPE_WEIGHTS, k=1)[0] | |
| def get_paint_color(): | |
| return random.choice(PAINT_COLORS) | |
| def get_option_chances(is_premium): | |
| base = 0.50 | |
| bonus = 0.35 if is_premium else 0.0 | |
| return { | |
| 'has_gps': base + bonus, | |
| 'has_air_conditioning': 0.85 + 0.12 * is_premium, | |
| 'automatic': 0.4 + 0.45 * is_premium, | |
| 'has_speed_regulator': 0.45 + 0.40 * is_premium, | |
| 'winter_tires': 0.40 + 0.20 * is_premium | |
| } | |
| def gen_price(avg_sale_price, age_years, km, power, options_count, is_premium, car_type): | |
| price_bracket = ( | |
| 52 if avg_sale_price < 25000 else | |
| 62 if avg_sale_price < 35000 else | |
| 78 if avg_sale_price < 50000 else | |
| 130 if avg_sale_price < 80000 else | |
| 180 | |
| ) | |
| age_fac = 1.10 - 0.07 * min(age_years, 10) | |
| km_fac = 1.0 - (km / 250000) * 0.6 | |
| opt_fac = 1.0 + (options_count * 0.07) | |
| premium_fac = 1.2 if is_premium else 1.0 | |
| car_type_factor = car_type_price_factors.get(car_type, 1.0) | |
| base_price = price_bracket * age_fac * km_fac * opt_fac * premium_fac | |
| management_fee_factor = 1.20 | |
| final_price = base_price * management_fee_factor * car_type_factor | |
| return round(max(final_price, 18), 1) | |
| latest_row = None | |
| def generate_random_row(): | |
| global latest_row | |
| brand, share, avg_price, power_min, power_max, allows_electric, allows_hybrid, is_premium = random.choices(brands_list, weights=[b[1] for b in brands_list], k=1)[0] | |
| year = random.choices(years, weights=years_weights, k=1)[0] | |
| day = random.randint(1, 365) | |
| date_str = (datetime(year, 1, 1) + timedelta(days=day-1)).strftime("%Y-%m-%d") | |
| is_tesla = brand.lower() == "tesla" | |
| fuel = get_fuel(year, allows_electric, allows_hybrid, is_tesla) | |
| power = random.randint(power_min, power_max) | |
| car_type = get_car_type() | |
| paint_color = get_paint_color() | |
| base_km = (2025 - year) * 13000 + random.randint(-4000, 3000) | |
| mileage = int(max(5000, base_km * random.uniform(0.7, 1.0))) | |
| opt_prob = get_option_chances(is_premium) | |
| gps = yesno(opt_prob['has_gps']) | |
| ac = yesno(opt_prob['has_air_conditioning']) | |
| auto = yesno(opt_prob['automatic']) | |
| spd = yesno(opt_prob['has_speed_regulator']) | |
| winter = yesno(opt_prob['winter_tires']) | |
| opt_count = sum([gps, ac, auto, spd, winter]) | |
| age_years = 2025 - year | |
| if GENERATE_RENT: | |
| rent = gen_price(avg_price, age_years, mileage, power, opt_count, is_premium, car_type) | |
| else: | |
| rent = 0 | |
| row = { | |
| "timestamp": datetime.now().isoformat(), | |
| "brand": brand, | |
| "mileage": mileage, | |
| "date_of_first_registration": date_str, | |
| "engine_power": power, | |
| "fuel": fuel, | |
| "paint_color": paint_color, | |
| "car_type": car_type, | |
| "has_gps": gps, | |
| "has_air_conditioning": ac, | |
| "automatic": auto, | |
| "has_speed_regulator": spd, | |
| "winter_tires": winter, | |
| "daily_rental_price": rent | |
| } | |
| latest_row = row | |
| return row | |
| def background_generator(): | |
| global latest_row | |
| while True: | |
| latest_row = generate_random_row() | |
| time.sleep(GENERATION_INTERVAL) | |
| threading.Thread(target=background_generator, daemon=True).start() | |
| def home(): | |
| return { | |
| "message": "Live car rental API running 🚗", | |
| "generation_interval_sec": GENERATION_INTERVAL, | |
| "generate_rent_enabled": GENERATE_RENT, | |
| "example_endpoint": "/data" | |
| } | |
| def get_latest_data(): | |
| if latest_row is None: | |
| return {"status": "waiting", "message": "No data generated yet"} | |
| return latest_row | |