Spaces:
Sleeping
Sleeping
skeleton
Browse files- app.py +3 -3
- config.py +7 -5
- data_repository.py +30 -21
app.py
CHANGED
|
@@ -20,13 +20,13 @@ def compute_scores(user_gen_designs: pd.DataFrame) -> ModelScoringResult:
|
|
| 20 |
|
| 21 |
|
| 22 |
def process_generated_designs(github_link: str, file: PathLike[str]):
|
| 23 |
-
uuid4 = uuid.uuid4()
|
| 24 |
validate_github_link(github_link)
|
| 25 |
with open(file, 'r') as user_file:
|
| 26 |
user_gen_designs = pd.read_csv(user_file)
|
| 27 |
validate_user_designs(user_gen_designs)
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
with gr.Blocks() as gradio_app:
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
def process_generated_designs(github_link: str, file: PathLike[str]):
|
|
|
|
| 23 |
validate_github_link(github_link)
|
| 24 |
with open(file, 'r') as user_file:
|
| 25 |
user_gen_designs = pd.read_csv(user_file)
|
| 26 |
validate_user_designs(user_gen_designs)
|
| 27 |
+
scores = compute_scores(user_gen_designs)
|
| 28 |
+
REPOSITORY_INSTANCE.add_row(scores)
|
| 29 |
+
return f"File uploaded successfully, uuid {scores.uuid}"
|
| 30 |
|
| 31 |
|
| 32 |
with gr.Blocks() as gradio_app:
|
config.py
CHANGED
|
@@ -4,18 +4,20 @@ import attrs
|
|
| 4 |
|
| 5 |
|
| 6 |
@attrs.define(frozen=True)
|
| 7 |
-
class
|
| 8 |
production: bool
|
| 9 |
|
| 10 |
def get_env_or_warn(key: str):
|
| 11 |
result = os.getenv(key)
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
return result
|
| 14 |
|
| 15 |
|
| 16 |
-
def get_app_config() ->
|
| 17 |
-
return
|
| 18 |
-
production=
|
| 19 |
)
|
| 20 |
|
| 21 |
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
@attrs.define(frozen=True)
|
| 7 |
+
class AppConfigModel:
|
| 8 |
production: bool
|
| 9 |
|
| 10 |
def get_env_or_warn(key: str):
|
| 11 |
result = os.getenv(key)
|
| 12 |
+
if (result is None) or (result.strip() == ""):
|
| 13 |
+
print(f"Warning: could not find environment variable {key}")
|
| 14 |
+
return ""
|
| 15 |
return result
|
| 16 |
|
| 17 |
|
| 18 |
+
def get_app_config() -> AppConfigModel:
|
| 19 |
+
return AppConfigModel(
|
| 20 |
+
production=get_env_or_warn("DEPLOYMENT_ENVIRONMENT").upper() == "PRODUCTION"
|
| 21 |
)
|
| 22 |
|
| 23 |
|
data_repository.py
CHANGED
|
@@ -6,7 +6,7 @@ from typing import List
|
|
| 6 |
import attrs
|
| 7 |
import pandas as pd
|
| 8 |
|
| 9 |
-
from config import
|
| 10 |
|
| 11 |
|
| 12 |
@attrs.define
|
|
@@ -18,49 +18,58 @@ class ModelScoringResult:
|
|
| 18 |
|
| 19 |
|
| 20 |
ORDERED_COLUMNS = [
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
"submission_time",
|
|
|
|
| 24 |
]
|
| 25 |
|
|
|
|
| 26 |
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
pass
|
| 33 |
|
| 34 |
@abc.abstractmethod
|
| 35 |
-
def
|
| 36 |
pass
|
| 37 |
|
| 38 |
@abc.abstractmethod
|
| 39 |
-
def
|
| 40 |
pass
|
| 41 |
|
| 42 |
|
| 43 |
-
class
|
| 44 |
def __init__(self, dummy_file_path=os.path.join(os.path.dirname(__file__), "dummy_data.txt")):
|
| 45 |
self.dummy_file_path = dummy_file_path
|
| 46 |
if not os.path.exists(self.dummy_file_path):
|
| 47 |
with open(self.dummy_file_path, "w") as file:
|
| 48 |
file.write("uuid,score,submission_time,scoring_time")
|
| 49 |
|
| 50 |
-
def
|
| 51 |
-
return pd.
|
| 52 |
|
| 53 |
-
def
|
| 54 |
-
|
| 55 |
-
result = pd.concat([previous_state, pd.DataFrame(row.as_dict())])
|
| 56 |
-
result.to_csv(self.dummy_file_path)
|
| 57 |
|
| 58 |
-
def save_current_state(self, rows: List[ModelScoringResult]):
|
| 59 |
-
pd.DataFrame([r.as_dict() for r in rows]).to_csv(self.dummy_file_path)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
|
| 66 |
-
REPOSITORY_INSTANCE:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import attrs
|
| 7 |
import pandas as pd
|
| 8 |
|
| 9 |
+
from config import APP_CONFIG
|
| 10 |
|
| 11 |
|
| 12 |
@attrs.define
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
ORDERED_COLUMNS = [
|
| 21 |
+
"uuid",
|
| 22 |
+
"score",
|
| 23 |
"submission_time",
|
| 24 |
+
"scoring_time",
|
| 25 |
]
|
| 26 |
|
| 27 |
+
class PandasModelScoresRepository(metaclass=abc.ABCMeta):
|
| 28 |
|
| 29 |
+
def get_data_to_display(self):
|
| 30 |
+
return pd.DataFrame(self.read_curr_state(), columns=ORDERED_COLUMNS)
|
| 31 |
|
| 32 |
+
def add_row(self, row: ModelScoringResult):
|
| 33 |
+
previous_state = self.read_curr_state()
|
| 34 |
+
result = pd.concat([previous_state, pd.DataFrame(attrs.asdict(row), index=range(1))])
|
| 35 |
+
self.save_to_disk(result)
|
| 36 |
|
| 37 |
+
def save_current_state(self, rows: List[ModelScoringResult]):
|
| 38 |
+
self.save_to_disk(pd.DataFrame([attrs.asdict(r) for r in rows]))
|
|
|
|
| 39 |
|
| 40 |
@abc.abstractmethod
|
| 41 |
+
def save_to_disk(self, result: pd.DataFrame):
|
| 42 |
pass
|
| 43 |
|
| 44 |
@abc.abstractmethod
|
| 45 |
+
def read_curr_state(self) -> pd.DataFrame:
|
| 46 |
pass
|
| 47 |
|
| 48 |
|
| 49 |
+
class LocalPandasModelScoresRepository(PandasModelScoresRepository):
|
| 50 |
def __init__(self, dummy_file_path=os.path.join(os.path.dirname(__file__), "dummy_data.txt")):
|
| 51 |
self.dummy_file_path = dummy_file_path
|
| 52 |
if not os.path.exists(self.dummy_file_path):
|
| 53 |
with open(self.dummy_file_path, "w") as file:
|
| 54 |
file.write("uuid,score,submission_time,scoring_time")
|
| 55 |
|
| 56 |
+
def read_curr_state(self) -> pd.DataFrame:
|
| 57 |
+
return pd.read_csv(self.dummy_file_path, index_col=None)
|
| 58 |
|
| 59 |
+
def save_to_disk(self, result: pd.DataFrame):
|
| 60 |
+
result.to_csv(self.dummy_file_path, index=False)
|
|
|
|
|
|
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
class HuggingFaceDatasetModelScoresRepository(PandasModelScoresRepository):
|
| 64 |
+
def read_curr_state(self) -> pd.DataFrame:
|
| 65 |
+
pass
|
| 66 |
|
| 67 |
+
def save_to_disk(self, result: pd.DataFrame):
|
| 68 |
+
pass
|
| 69 |
|
| 70 |
|
| 71 |
+
REPOSITORY_INSTANCE: PandasModelScoresRepository
|
| 72 |
+
if APP_CONFIG.production:
|
| 73 |
+
REPOSITORY_INSTANCE = HuggingFaceDatasetModelScoresRepository()
|
| 74 |
+
else:
|
| 75 |
+
REPOSITORY_INSTANCE = LocalPandasModelScoresRepository()
|