Spaces:
Sleeping
Sleeping
skeleton
Browse files- app.py +46 -4
- config.py +22 -0
- data_repository.py +66 -0
- designs_submission_validations.py +9 -0
app.py
CHANGED
|
@@ -1,7 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
demo.launch()
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
+
import random
|
| 3 |
+
import uuid
|
| 4 |
+
from os import PathLike
|
| 5 |
+
|
| 6 |
import gradio as gr
|
| 7 |
+
import pandas as pd
|
| 8 |
+
|
| 9 |
+
from config import APP_CONFIG
|
| 10 |
+
from data_repository import REPOSITORY_INSTANCE, ModelScoringResult
|
| 11 |
+
from designs_submission_validations import validate_github_link, validate_user_designs
|
| 12 |
+
|
| 13 |
+
def compute_scores(user_gen_designs: pd.DataFrame) -> ModelScoringResult:
|
| 14 |
+
return ModelScoringResult(
|
| 15 |
+
score=random.randint(50, 5000),
|
| 16 |
+
scoring_time=datetime.datetime.now(),
|
| 17 |
+
submission_time=datetime.datetime.now(),
|
| 18 |
+
uuid=str(uuid.uuid4())
|
| 19 |
+
)
|
| 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 |
+
REPOSITORY_INSTANCE.add_row(compute_scores(user_gen_designs))
|
| 29 |
+
return "File uploaded successfully, uuid {uuid4}"
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
with gr.Blocks() as gradio_app:
|
| 33 |
+
with gr.Tab("Bike Bench Leaderboard"):
|
| 34 |
+
gr.Markdown("Hello beautiful people!")
|
| 35 |
+
gr.Dataframe(REPOSITORY_INSTANCE.get_data_to_display(), label="Scores of Previous Files")
|
| 36 |
|
| 37 |
+
with gr.Tab("Upload File"):
|
| 38 |
+
gr.Interface(
|
| 39 |
+
fn=process_generated_designs,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Textbox(label="Github Link"),
|
| 42 |
+
gr.File(label="Upload a file"),
|
| 43 |
+
],
|
| 44 |
+
outputs="text",
|
| 45 |
+
title="Bike Bench Leaderboard",
|
| 46 |
+
description="Upload a file to see the result."
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
gradio_app.launch(debug=(not APP_CONFIG.production))
|
|
|
config.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import attrs
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
@attrs.define(frozen=True)
|
| 7 |
+
class AppConfig:
|
| 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() -> AppConfig:
|
| 17 |
+
return AppConfig(
|
| 18 |
+
production=os.getenv("DEPLOYMENT_ENVIRONMENT").upper() == "PRODUCTION"
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
APP_CONFIG = get_app_config()
|
data_repository.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import abc
|
| 2 |
+
import datetime
|
| 3 |
+
import os.path
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
import attrs
|
| 7 |
+
import pandas as pd
|
| 8 |
+
|
| 9 |
+
from config import AppConfig
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@attrs.define
|
| 13 |
+
class ModelScoringResult:
|
| 14 |
+
uuid: str
|
| 15 |
+
score: float
|
| 16 |
+
submission_time: datetime.datetime
|
| 17 |
+
scoring_time: datetime.datetime
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
ORDERED_COLUMNS = [
|
| 21 |
+
"model_id",
|
| 22 |
+
"model_score",
|
| 23 |
+
"submission_time",
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class ModelScoresRepository(metaclass=abc.ABCMeta):
|
| 29 |
+
|
| 30 |
+
@abc.abstractmethod
|
| 31 |
+
def get_data_to_display(self) -> pd.DataFrame:
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
@abc.abstractmethod
|
| 35 |
+
def add_row(self, row: ModelScoringResult) -> None:
|
| 36 |
+
pass
|
| 37 |
+
|
| 38 |
+
@abc.abstractmethod
|
| 39 |
+
def save_current_state(self, rows: List[ModelScoringResult]) -> None:
|
| 40 |
+
pass
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class LocalModelScoresRepository(ModelScoresRepository):
|
| 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 get_data_to_display(self):
|
| 51 |
+
return pd.DataFrame(pd.read_csv(self.dummy_file_path), columns=ORDERED_COLUMNS)
|
| 52 |
+
|
| 53 |
+
def add_row(self, row: ModelScoringResult):
|
| 54 |
+
previous_state = pd.read_csv(self.dummy_file_path)
|
| 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 |
+
class HuggingFaceDatasetModelScoresRepository(ModelScoresRepository):
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
REPOSITORY_INSTANCE: ModelScoresRepository = HuggingFaceDatasetModelScoresRepository() if AppConfig.production else LocalModelScoresRepository()
|
designs_submission_validations.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def validate_github_link(github_link: str):
|
| 5 |
+
pass
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def validate_user_designs(user_generated_designs: pd.DataFrame):
|
| 9 |
+
pass
|