Spaces:
Paused
Paused
Download demo.py from Adity00/dqa-openenv: direct link, hf CLI and curl.
- Browser
- Download file 6.91 kB
-
https://huggingface.co/spaces/Adity00/dqa-openenv/resolve/main/demo.py
- Command line
-
hf download hf://spaces/Adity00/dqa-openenv/demo.py
-
curl -L -o demo.py https://huggingface.co/spaces/Adity00/dqa-openenv/resolve/main/demo.py
6.91 kB
| import argparse | |
| import time | |
| import sys | |
| from colorama import init, Fore, Style | |
| init(autoreset=True) | |
| from server.dqa_openenv_environment import DqaOpenenvEnvironment | |
| from models import DQAAction | |
| STEP_PAUSE = 1.5 | |
| def print_bar(label, score, width=30): | |
| filled = int(score * width) | |
| empty = width - filled | |
| bar = 'β' * filled + 'β' * empty | |
| if score > 0.80: | |
| color = Fore.GREEN | |
| elif score > 0.60: | |
| color = Fore.YELLOW | |
| else: | |
| color = Fore.RED | |
| print(f' {label:<22} {color}{bar}{Style.RESET_ALL} {score:.4f}') | |
| def print_banner(task_id): | |
| task_str = f"{task_id.upper()} TASK" | |
| print("ββββββββββββββββββββββββββββββββββββββββββββββββ") | |
| print(f"β DQA-OpenEnv LIVE DEMO β {task_str:<18} β") | |
| print("β Hackathon: Scaler x Meta PyTorch OpenEnv β") | |
| print("ββββββββββββββββββββββββββββββββββββββββββββββββ") | |
| ACTIONS = { | |
| 'easy': [ | |
| {'action_type':'fill_nulls','column':'age','parameters':{'strategy':'median'}}, | |
| {'action_type':'fill_nulls','column':'income','parameters':{'strategy':'median'}}, | |
| {'action_type':'fill_nulls','column':'satisfaction_score','parameters':{'strategy':'median'}}, | |
| {'action_type':'cast_type','column':'age','parameters':{'dtype':'int'}}, | |
| {'action_type':'submit','column':'','parameters':{}} | |
| ], | |
| 'medium': [ | |
| {'action_type':'drop_column','column':'performance_score','parameters':{}}, | |
| {'action_type':'drop_duplicates','column':'','parameters':{}}, | |
| {'action_type':'normalize_category','column':'department','parameters':{'mapping':{'Eng':'Engineering','engineering':'Engineering','ENGINEERING':'Engineering'}}}, | |
| {'action_type':'fill_nulls','column':'salary','parameters':{'strategy':'median'}}, | |
| {'action_type':'submit','column':'','parameters':{}} | |
| ], | |
| 'hard': [ | |
| {'action_type':'fill_nulls','column':'processing_fee','parameters':{'strategy':'constant','fill_value':0}}, | |
| {'action_type':'filter_rows','column':'amount','parameters':{'condition':'amount >= 0'}}, | |
| {'action_type':'clip_outliers','column':'amount','parameters':{'method':'iqr'}}, | |
| {'action_type':'normalize_category','column':'status','parameters':{'mapping':{'Pending':'pending','COMPLETED':'completed','Failed':'failed'}}}, | |
| {'action_type':'submit','column':'','parameters':{}} | |
| ], | |
| 'expert': [ | |
| {'action_type':'fill_nulls','column':'blood_glucose','parameters':{'strategy':'median'}}, | |
| {'action_type':'filter_rows','column':'bmi','parameters':{'condition':'bmi >= 10 and bmi <= 60'}}, | |
| {'action_type':'drop_duplicates','column':'','parameters':{}}, | |
| {'action_type':'normalize_category','column':'medication','parameters':{'mapping':{'Metformin':'metformin','LISINOPRIL':'lisinopril','None':'none','Albuterol':'albuterol'}}}, | |
| {'action_type':'normalize_category','column':'discharge_status','parameters':{'mapping':{'Recovered':'recovered','DECEASED':'deceased','Transferred':'transferred'}}}, | |
| {'action_type':'submit','column':'','parameters':{}} | |
| ] | |
| } | |
| def run_demo(task_id): | |
| # Step 1 | |
| env = DqaOpenenvEnvironment() | |
| obs = env.reset(task_id=task_id) | |
| # Step 2 | |
| initial_scores = obs.quality_scores | |
| issues = obs.issue_hints | |
| current_score = initial_scores.get('overall', 0.0) | |
| # Step 3 | |
| print_banner(task_id) | |
| # Step 4 | |
| print(f"{Fore.CYAN}Initial Quality Scores:{Style.RESET_ALL}") | |
| print_bar("overall", initial_scores.get("overall", 0.0)) | |
| print_bar("completeness", initial_scores.get("completeness", 0.0)) | |
| print_bar("consistency", initial_scores.get("consistency", 0.0)) | |
| print_bar("uniqueness", initial_scores.get("uniqueness", 0.0)) | |
| print_bar("validity", initial_scores.get("validity", 0.0)) | |
| print(f"{Fore.YELLOW} Issues detected: {len(issues)}{Style.RESET_ALL}") | |
| for hint in issues: | |
| print(f"{Fore.YELLOW} {hint}{Style.RESET_ALL}") | |
| # Step 5 | |
| actions = ACTIONS[task_id] | |
| print("-" * 50) | |
| print(f"{Fore.CYAN}Executing {len(actions)} actions...{Style.RESET_ALL}") | |
| print("-" * 50) | |
| # Step 6 | |
| for i, action_dict in enumerate(actions): | |
| # a) | |
| action = DQAAction( | |
| action_type=action_dict['action_type'], | |
| column=action_dict['column'], | |
| parameters=action_dict['parameters'] | |
| ) | |
| # b) | |
| prev_score = current_score | |
| # c) | |
| result = env.step(action) | |
| new_scores = result.quality_scores | |
| if hasattr(result, 'reward') and result.reward is not None: | |
| reward = result.reward | |
| else: | |
| reward = (new_scores.get('overall', current_score) - current_score) * 2.0 | |
| if action_dict['action_type'] == 'submit': | |
| reward = 0.05 | |
| done = result.done if hasattr(result, 'done') else False | |
| # d) | |
| current_score = new_scores.get('overall', current_score) | |
| # e) | |
| if action_dict['column']: | |
| label = f"{action_dict['action_type']}({action_dict['column']})" | |
| else: | |
| label = action_dict['action_type'] | |
| # f) | |
| reward_color = Fore.GREEN if reward >= 0 else Fore.RED | |
| sign = '+' if reward >= 0 else '' | |
| print(f" Step {i+1}: {Fore.WHITE}{label:<35}{Style.RESET_ALL} reward: {reward_color}{sign}{reward:.4f}{Style.RESET_ALL}") | |
| # g) | |
| print_bar(' quality', current_score) | |
| # h) | |
| time.sleep(STEP_PAUSE) | |
| # i) | |
| if done: | |
| break | |
| # Step 7 | |
| if current_score >= 0.95: | |
| grade = 'A+' | |
| elif current_score >= 0.90: | |
| grade = 'A' | |
| elif current_score >= 0.80: | |
| grade = 'B' | |
| else: | |
| grade = 'C' | |
| # Step 8 | |
| print() | |
| print(f"{Fore.GREEN}ββββββββββββββββββββββββββββββββββββββββββββββββ") | |
| print(f"β{' EPISODE COMPLETE':<46}β") | |
| line2 = f" Final Score : {current_score:.4f} Grade: {grade}" | |
| print(f"β{line2:<46}β") | |
| line3 = f" Steps taken : {i+1}" | |
| print(f"β{line3:<46}β") | |
| line4 = f" Task : {task_id.upper()}" | |
| print(f"β{line4:<46}β") | |
| print(f"ββββββββββββββββββββββββββββββββββββββββββββββββ{Style.RESET_ALL}") | |
| if __name__ == '__main__': | |
| parser = argparse.ArgumentParser(description='DQA-OpenEnv Live Demo') | |
| parser.add_argument('--task', default='medium', | |
| choices=['easy','medium','hard','expert']) | |
| args = parser.parse_args() | |
| run_demo(args.task) | |