Commit ·
2b30142
0
Parent(s):
added file
Browse files- .gitattributes +35 -0
- README.md +10 -0
- app.py +113 -0
- dockerfile +16 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Backend1
|
| 3 |
+
emoji: 🏢
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: yellow
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: false
|
| 8 |
+
---
|
| 9 |
+
|
| 10 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel,Field
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
level=logging.INFO, # Set the logging level to INFO
|
| 9 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 10 |
+
handlers=[
|
| 11 |
+
logging.FileHandler("app.log"), # Log to a file named app.log
|
| 12 |
+
logging.StreamHandler() # Log to the console
|
| 13 |
+
]
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Create logger instance
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
|
| 19 |
+
app = FastAPI()
|
| 20 |
+
|
| 21 |
+
file_path = "C:/java lab/python/backend/world_population.csv"
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
countries_df = pd.read_csv(file_path)
|
| 25 |
+
# Convert Population and Area to numeric values with error handling
|
| 26 |
+
countries_df["Population"] = pd.to_numeric(countries_df["Population"])
|
| 27 |
+
countries_df["Area"] = pd.to_numeric(countries_df["Area"])
|
| 28 |
+
logger.info("CSV file loaded and processed successfully.")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.error(f"Failed to load or process CSV file: {e}")
|
| 31 |
+
raise
|
| 32 |
+
|
| 33 |
+
# Pydantic models for API response
|
| 34 |
+
class CountryPopulation(BaseModel):
|
| 35 |
+
country: str
|
| 36 |
+
population: int
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
class ContinentPopulationResponse(BaseModel):
|
| 40 |
+
continent: str=Field(description="continent of country")
|
| 41 |
+
total_population: int
|
| 42 |
+
total_area: int
|
| 43 |
+
continent_population_density: float
|
| 44 |
+
max_population: CountryPopulation
|
| 45 |
+
min_population: CountryPopulation
|
| 46 |
+
avg_population: int
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
@app.get("/")
|
| 50 |
+
def home():
|
| 51 |
+
logger.info("Accessed home endpoint.")
|
| 52 |
+
return {"message": "Welcome to the World Population API"}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
@app.get("/continent/{continent_name}", response_model=ContinentPopulationResponse)
|
| 56 |
+
def get_continent_population(continent_name: str):
|
| 57 |
+
try:
|
| 58 |
+
logger.info(f"Received request for continent: {continent_name}")
|
| 59 |
+
|
| 60 |
+
# Filter data for the given continent
|
| 61 |
+
continent_group = countries_df[countries_df["Continent"] == continent_name]
|
| 62 |
+
continent_group = continent_group.dropna(subset=["Population", "Area"])
|
| 63 |
+
|
| 64 |
+
if continent_group.empty:
|
| 65 |
+
logger.warning(f"No data found for continent: {continent_name}")
|
| 66 |
+
raise HTTPException(status_code=404, detail=f"No data found for continent: {continent_name}")
|
| 67 |
+
|
| 68 |
+
# Calculate total population and total area
|
| 69 |
+
total_population = int(continent_group["Population"].sum())
|
| 70 |
+
total_area = int(continent_group["Area"].sum())
|
| 71 |
+
|
| 72 |
+
# Handle case where total_area is zero to avoid division by zero
|
| 73 |
+
if total_area == 0:
|
| 74 |
+
logger.error(f"Total area is zero for continent: {continent_name}. Cannot calculate population density.")
|
| 75 |
+
raise HTTPException(status_code=400, detail="Total area is zero. Cannot calculate population density.")
|
| 76 |
+
|
| 77 |
+
# Calculate population density
|
| 78 |
+
continent_density = total_population / total_area
|
| 79 |
+
|
| 80 |
+
# Find max and min population countries
|
| 81 |
+
max_row = continent_group.loc[continent_group["Population"].idxmax()]
|
| 82 |
+
min_row = continent_group.loc[continent_group["Population"].idxmin()]
|
| 83 |
+
avg_population = int(continent_group["Population"].mean())
|
| 84 |
+
|
| 85 |
+
logger.info(
|
| 86 |
+
f"Processed data for continent: {continent_name} | "
|
| 87 |
+
f"Total Population: {total_population}, Total Area: {total_area}, "
|
| 88 |
+
f"Population Density: {continent_density:.2f}"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
return ContinentPopulationResponse(
|
| 92 |
+
continent=continent_name,
|
| 93 |
+
total_population=total_population,
|
| 94 |
+
total_area=total_area,
|
| 95 |
+
continent_population_density=continent_density,
|
| 96 |
+
max_population=CountryPopulation(
|
| 97 |
+
country=max_row["Country"],
|
| 98 |
+
population=int(max_row["Population"])
|
| 99 |
+
),
|
| 100 |
+
min_population=CountryPopulation(
|
| 101 |
+
country=min_row["Country"],
|
| 102 |
+
population=int(min_row["Population"])
|
| 103 |
+
),
|
| 104 |
+
avg_population=avg_population
|
| 105 |
+
)
|
| 106 |
+
except HTTPException as http_err:
|
| 107 |
+
# Log and re-raise HTTP-specific exceptions
|
| 108 |
+
logger.error(f"HTTP exception occurred: {http_err.detail}")
|
| 109 |
+
raise http_err
|
| 110 |
+
except Exception as e:
|
| 111 |
+
# Handle unexpected errors
|
| 112 |
+
logger.error(f"Unexpected error occurred while processing continent: {continent_name} | Error: {e}")
|
| 113 |
+
raise HTTPException(status_code=500, detail="An unexpected error occurred while processing your request.")
|
dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|