Spaces:
Sleeping
Sleeping
Vela
commited on
Commit
·
22b9c3e
1
Parent(s):
0cde1a0
commit_message : "Added files"
Browse files
Dockerfile
CHANGED
|
@@ -23,6 +23,8 @@ ENV PATH="/home/user/.local/bin:$PATH"
|
|
| 23 |
|
| 24 |
WORKDIR /app
|
| 25 |
|
|
|
|
|
|
|
| 26 |
COPY --chown=user ./requirements.txt requirements.txt
|
| 27 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 28 |
|
|
|
|
| 23 |
|
| 24 |
WORKDIR /app
|
| 25 |
|
| 26 |
+
RUN pip install --upgrade pip
|
| 27 |
+
|
| 28 |
COPY --chown=user ./requirements.txt requirements.txt
|
| 29 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 30 |
|
backend/api/__pycache__/main.cpython-312.pyc
CHANGED
|
Binary files a/backend/api/__pycache__/main.cpython-312.pyc and b/backend/api/__pycache__/main.cpython-312.pyc differ
|
|
|
backend/api/main.py
CHANGED
|
@@ -4,8 +4,6 @@ import sys
|
|
| 4 |
src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "backend"))
|
| 5 |
sys.path.append(src_directory)
|
| 6 |
from modules import home_page
|
| 7 |
-
from api.schema import CountryDetails
|
| 8 |
-
from utils import logger
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
|
@@ -25,68 +23,12 @@ def display_countries():
|
|
| 25 |
countries = home_page.display_countries(df)
|
| 26 |
return countries.tolist()
|
| 27 |
|
| 28 |
-
@app.get("/
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
return
|
| 32 |
|
| 33 |
-
@app.get("/
|
| 34 |
-
def
|
| 35 |
-
|
| 36 |
-
return
|
| 37 |
-
|
| 38 |
-
@app.get("/ShowCountrywithHighestPopulation")
|
| 39 |
-
def display_country_with_high_pop():
|
| 40 |
-
highest_pop = home_page.country_with_highest_population(df)
|
| 41 |
-
return highest_pop
|
| 42 |
-
|
| 43 |
-
@app.get("/ShowCountrywithLowestPopulation")
|
| 44 |
-
def display_country_with_high_pop():
|
| 45 |
-
lowest_pop = home_page.country_with_lowest_population(df)
|
| 46 |
-
return lowest_pop
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# highest_pop = home_page.display_countries(df).tolist()
|
| 72 |
-
# print(type(highest_pop))
|
| 73 |
-
|
| 74 |
-
# @app.get("/")
|
| 75 |
-
# def home():
|
| 76 |
-
# return end_points.welcome_msg()
|
| 77 |
-
|
| 78 |
-
# @app.get('/{continent}')
|
| 79 |
-
# def present_countries(continent : str):
|
| 80 |
-
# return home_page.list_country_by_continent(df, continent)
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# @app.get("/{continent}/{data_type}/{stat}")
|
| 84 |
-
# def get_stats_of_cont(continent:str, data_type : str, stat:str):
|
| 85 |
-
# return home_page.get_stat_by_continent(df,continent,data_type,stat)
|
| 86 |
-
|
| 87 |
-
# @app.get("/{key}/{value}")
|
| 88 |
-
# def get_stats_of_cont(key : str ,value : str):
|
| 89 |
-
# return home_page.get_continent_with_max_value(df, key, value)
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
| 4 |
src_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..", "backend"))
|
| 5 |
sys.path.append(src_directory)
|
| 6 |
from modules import home_page
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
|
|
| 23 |
countries = home_page.display_countries(df)
|
| 24 |
return countries.tolist()
|
| 25 |
|
| 26 |
+
@app.get("/ShowContinentStats/{attribute}/{stat_type}")
|
| 27 |
+
def display_continent_stats(attribute:str, stat_type:str):
|
| 28 |
+
continent_stats = home_page.continent_stat(df, attribute, stat_type)
|
| 29 |
+
return continent_stats
|
| 30 |
|
| 31 |
+
@app.get("/ShowCountryStats/{attribute}/{stat_type}")
|
| 32 |
+
def display_country_stats(attribute : str, stat_type : str):
|
| 33 |
+
country_stats = home_page.country_stat(df, attribute,stat_type)
|
| 34 |
+
return country_stats
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend/modules/__pycache__/home_page.cpython-312.pyc
CHANGED
|
Binary files a/backend/modules/__pycache__/home_page.cpython-312.pyc and b/backend/modules/__pycache__/home_page.cpython-312.pyc differ
|
|
|
backend/modules/home_page.py
CHANGED
|
@@ -30,118 +30,101 @@ def display_countries(dataframe):
|
|
| 30 |
logger.log("Displaying the list of countries in the data")
|
| 31 |
return countries
|
| 32 |
|
| 33 |
-
def
|
| 34 |
-
highest= dataframe.groupby('Continent')['Population'].agg(total_population = 'sum')
|
| 35 |
-
max_continent = highest.idxmax().item()
|
| 36 |
-
max_population = highest.max().item()
|
| 37 |
-
result = {max_continent:max_population}
|
| 38 |
-
logger.log("Displaying the continent with highest population in the data")
|
| 39 |
-
return result
|
| 40 |
-
|
| 41 |
-
def continent_with_lowest_population(dataframe):
|
| 42 |
-
lowest= dataframe.groupby('Continent')['Population'].agg(total_population = 'sum')
|
| 43 |
-
min_continent = lowest.idxmin().item()
|
| 44 |
-
min_population = lowest.min().item()
|
| 45 |
-
result = {min_continent:min_population}
|
| 46 |
-
logger.log("Displaying the continent with lowest population in the data")
|
| 47 |
-
return result
|
| 48 |
-
|
| 49 |
-
def country_with_lowest_population(dataframe):
|
| 50 |
-
index= dataframe['Population'].idxmin()
|
| 51 |
-
min_country = dataframe['Country'][index]
|
| 52 |
-
min_population = dataframe['Population'][index]
|
| 53 |
-
result = {min_country:min_population.item()}
|
| 54 |
-
logger.log("Displaying the country with lowest population in the data")
|
| 55 |
-
return result
|
| 56 |
-
|
| 57 |
-
def country_with_highest_population(dataframe):
|
| 58 |
-
index= dataframe['Population'].idxmax()
|
| 59 |
-
max_country = dataframe['Country'][index]
|
| 60 |
-
max_population = dataframe['Population'][index]
|
| 61 |
-
result = {max_country:max_population.item()}
|
| 62 |
-
logger.log("Displaying the country with highest population in the data")
|
| 63 |
-
return result
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
def list_country_by_continent(dataframe,continent):
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
except Exception as e:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
continent = "North America"
|
| 104 |
-
if continent.lower() == "SouthAmerica".lower():
|
| 105 |
-
continent = "South America"
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
|
| 114 |
-
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
|
| 141 |
-
def get_continent_with_max_value(dataframe, key, value):
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 30 |
logger.log("Displaying the list of countries in the data")
|
| 31 |
return countries
|
| 32 |
|
| 33 |
+
def continent_stat(dataframe, attribute="Population", stat_type="highest"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
try:
|
| 35 |
+
if 'Continent' not in dataframe.columns or attribute not in dataframe.columns:
|
| 36 |
+
return ValueError(f"Dataframe must contain 'Continent' and '{attribute}' columns.")
|
| 37 |
+
|
| 38 |
+
continent_stats = dataframe.groupby('Continent')[attribute].agg(total_attribute='sum')
|
| 39 |
+
|
| 40 |
+
if stat_type == "highest":
|
| 41 |
+
continent = continent_stats.idxmax().item()
|
| 42 |
+
value = continent_stats.max().item()
|
| 43 |
+
logger.log(f"Displaying the continent with the highest {attribute}: {continent} with {attribute} {value}")
|
| 44 |
+
|
| 45 |
+
elif stat_type == "lowest":
|
| 46 |
+
continent = continent_stats.idxmin().item()
|
| 47 |
+
value = continent_stats.min().item()
|
| 48 |
+
logger.log(f"Displaying the continent with the lowest {attribute}: {continent} with {attribute} {value}")
|
| 49 |
+
|
| 50 |
+
else:
|
| 51 |
+
raise ValueError("Invalid stat_type. Use 'highest' or 'lowest'.")
|
| 52 |
+
|
| 53 |
+
return {continent: value}
|
| 54 |
+
|
| 55 |
except Exception as e:
|
| 56 |
+
logger.log(f"Error in continent_stat: {str(e)}")
|
| 57 |
+
return {"error": str(e)}
|
| 58 |
+
|
| 59 |
+
def country_stat(dataframe, attribute : str = "Population", stat_type :str = "highest"):
|
| 60 |
+
try :
|
| 61 |
+
if stat_type.lower() == "highest":
|
| 62 |
+
index= dataframe[attribute].idxmax()
|
| 63 |
+
elif stat_type.lower() == "lowest":
|
| 64 |
+
index= dataframe[attribute].idxmin()
|
| 65 |
+
|
| 66 |
+
country = dataframe['Country'][index]
|
| 67 |
+
requested_attribute = dataframe[attribute][index]
|
| 68 |
+
result = {country:requested_attribute.item()}
|
| 69 |
+
logger.log(f"Displaying the country with {stat_type} {attribute} in the data")
|
| 70 |
+
return result
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"Unable to fetch the data. Error {e}"
|
| 73 |
|
| 74 |
+
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
# def list_country_by_continent(dataframe,continent):
|
| 77 |
+
# try:
|
| 78 |
+
# df_countries = dataframe[dataframe['Continent'] == continent]
|
| 79 |
+
# countries= df_countries['Country'].to_list()
|
| 80 |
+
# logger.log("Separated data by continent")
|
| 81 |
+
# return countries
|
| 82 |
+
# except Exception as e:
|
| 83 |
+
# return f"{e}"
|
| 84 |
+
|
| 85 |
+
# def get_stat_by_continent(df ,continent: str, data_type: str, stat: str , ):
|
| 86 |
+
|
| 87 |
+
# if continent.lower() == "NorthAmerica".lower():
|
| 88 |
+
# continent = "North America"
|
| 89 |
+
# if continent.lower() == "SouthAmerica".lower():
|
| 90 |
+
# continent = "South America"
|
| 91 |
+
|
| 92 |
+
# valid_stats = ['max', 'min', 'mean' , 'sum' , 'count']
|
| 93 |
+
# if stat not in valid_stats:
|
| 94 |
+
# return f"Invalid stat. Please use one of the following: {valid_stats}."
|
| 95 |
|
| 96 |
+
# continent_population_stats = df.groupby('Continent')[data_type].agg(
|
| 97 |
+
# Maximum='max', Minimum='min', Average = 'mean',Total='sum' , Number_of_Countries = 'count')
|
| 98 |
|
| 99 |
+
# continent_countries = df[df['Continent'] == continent]
|
| 100 |
|
| 101 |
+
# if continent not in continent_population_stats.index:
|
| 102 |
+
# return f"Continent '{continent}' not found in the data."
|
| 103 |
|
| 104 |
+
# if stat == 'max':
|
| 105 |
+
# population_result = continent_population_stats.loc[continent]['Maximum']
|
| 106 |
+
# country_id = continent_countries.loc[continent_countries[data_type].idxmax()]
|
| 107 |
+
# country_name = country_id['Country']
|
| 108 |
+
# population_value = country_id[data_type]
|
| 109 |
+
# return f"{continent}'s {stat} {data_type} is {int(population_result)}. Country: {country_name} , {data_type} :{population_value}"
|
| 110 |
+
# if stat == 'min':
|
| 111 |
+
# population_result = continent_population_stats.loc[continent]['Minimum']
|
| 112 |
+
# country_id = continent_countries.loc[continent_countries[data_type].idxmin()]
|
| 113 |
+
# country_name = country_id['Country']
|
| 114 |
+
# population_value = country_id[data_type]
|
| 115 |
+
# return f"{continent}'s {stat} {data_type} is {int(population_result)}. Country: {country_name} , {data_type} :{population_value}"
|
| 116 |
+
# if stat == 'mean':
|
| 117 |
+
# population_result = continent_population_stats.loc[continent]['Average']
|
| 118 |
+
# return f"{continent}'s average {data_type} is {int(population_result)}"
|
| 119 |
+
# if stat == 'sum':
|
| 120 |
+
# population_result = continent_population_stats.loc[continent]['Total']
|
| 121 |
+
# return f"{continent}'s total {data_type} is {int(population_result)}"
|
| 122 |
+
# if stat == 'count' :
|
| 123 |
+
# population_result = continent_population_stats.loc[continent]['Number_of_Countries']
|
| 124 |
+
# return f"Total countries in {continent} is {int(population_result)}"
|
| 125 |
|
| 126 |
+
# def get_continent_with_max_value(dataframe, key, value):
|
| 127 |
+
# max_id = dataframe[value].idxmax()
|
| 128 |
+
# value_num = dataframe[value][max_id]
|
| 129 |
+
# value_country = dataframe[key][max_id]
|
| 130 |
+
# return f"{value_country}'s max {value} is {value_num}"
|
|
|
|
|
|