LyXoR51 commited on
Commit
a028193
·
1 Parent(s): ae741d3

pushing the app

Browse files
Files changed (3) hide show
  1. Dockerfile +22 -0
  2. requirements.txt +3 -0
  3. streaming_api.py +81 -0
Dockerfile ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ============================================================
2
+ # 🐳 DOCKERFILE - Streaming API (FastAPI)
3
+ # Compatible avec Hugging Face Spaces
4
+ # ============================================================
5
+
6
+ FROM python:3.11-slim
7
+
8
+ # Définir le répertoire de travail
9
+ WORKDIR /app
10
+
11
+ # Copier les fichiers du projet
12
+ COPY requirements.txt .
13
+ RUN pip install --no-cache-dir -r requirements.txt
14
+
15
+ COPY . .
16
+
17
+ # Exposer le port utilisé par Uvicorn (important pour Hugging Face)
18
+ EXPOSE 7860
19
+
20
+ # Lancer l'application
21
+ # Hugging Face attend que ton app tourne sur le port 7860
22
+ CMD ["uvicorn", "streaming_api:app", "--host", "0.0.0.0", "--port", "7860"]
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pandas
streaming_api.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # streaming_api_single.py
2
+ from fastapi import FastAPI
3
+ import pandas as pd
4
+ import random
5
+ import threading
6
+ import time
7
+ from datetime import datetime
8
+
9
+ # ==============================================================
10
+ # 🔧 CONFIGURATION FACILE
11
+ # ==============================================================
12
+
13
+ GENERATION_INTERVAL = 60 # secondes entre chaque nouvelle ligne
14
+ RANGES = {
15
+ "mileage": (10_000, 200_000),
16
+ "engine_power": (60, 300),
17
+ "rental_price_per_day": (50, 300),
18
+ }
19
+
20
+ MODEL_KEYS = ["Renault", "Peugeot", "Citroën", "BMW", "Audi", "Mercedes", "Nissan", "Toyota"]
21
+ FUELS = ["diesel", "petrol", "hybrid_petrol", "electro"]
22
+ CAR_TYPES = ["hatchback", "estate", "sedan", "suv", "convertible", "coupe", "van", "subcompact"]
23
+ PAINT_COLORS = ["black", "grey", "white", "silver", "blue", "red", "brown", "green", "beige", "orange"]
24
+
25
+ # ==============================================================
26
+ # 🧩 INITIALISATION
27
+ # ==============================================================
28
+
29
+ app = FastAPI(title="Live Car Rental API", version="1.0")
30
+ latest_row = None # la dernière ligne générée
31
+
32
+ # ==============================================================
33
+ # 🧠 GÉNÉRATION DE NOUVELLE LIGNE
34
+ # ==============================================================
35
+
36
+ def generate_random_row():
37
+ """Génère une seule ligne de données réaliste"""
38
+ return {
39
+ "timestamp": datetime.now().isoformat(),
40
+ "model_key": random.choice(MODEL_KEYS),
41
+ "mileage": random.randint(*RANGES["mileage"]),
42
+ "engine_power": random.randint(*RANGES["engine_power"]),
43
+ "fuel": random.choices(FUELS, weights=[0.7, 0.25, 0.04, 0.01])[0],
44
+ "paint_color": random.choice(PAINT_COLORS),
45
+ "car_type": random.choice(CAR_TYPES),
46
+ "private_parking_available": random.choice([True, False]),
47
+ "has_gps": random.choice([True, False]),
48
+ "has_air_conditioning": random.choice([True, False]),
49
+ "automatic_car": random.choice([True, False]),
50
+ "has_speed_regulator": random.choice([True, False]),
51
+ "winter_tires": random.choice([True, False]),
52
+ "rental_price_per_day": random.randint(*RANGES["rental_price_per_day"])
53
+ }
54
+
55
+ def background_generator():
56
+ global latest_row
57
+ while True:
58
+ latest_row = generate_random_row()
59
+ time.sleep(GENERATION_INTERVAL)
60
+
61
+ # Lancer la génération en arrière-plan
62
+ threading.Thread(target=background_generator, daemon=True).start()
63
+
64
+ # ==============================================================
65
+ # 🌐 ROUTES API
66
+ # ==============================================================
67
+
68
+ @app.get("/")
69
+ def home():
70
+ return {
71
+ "message": "Live car rental API running 🚗",
72
+ "generation_interval_sec": GENERATION_INTERVAL,
73
+ "example_endpoint": "/data"
74
+ }
75
+
76
+ @app.get("/data")
77
+ def get_latest_data():
78
+ """Renvoie uniquement la dernière ligne générée"""
79
+ if latest_row is None:
80
+ return {"status": "waiting", "message": "No data generated yet"}
81
+ return latest_row