Spaces:
Sleeping
Sleeping
Download storage.py from aysan98/empathy-map-interview: direct link, hf CLI and curl.
- Browser
- Download file 4.59 kB
-
https://huggingface.co/spaces/aysan98/empathy-map-interview/resolve/main/storage.py
- Command line
-
hf download hf://spaces/aysan98/empathy-map-interview/storage.py
-
curl -L -o storage.py https://huggingface.co/spaces/aysan98/empathy-map-interview/resolve/main/storage.py
4.59 kB
| import json | |
| import os | |
| from datetime import datetime | |
| from huggingface_hub import HfApi, hf_hub_download | |
| import tempfile | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| DATASET_ID = "aysan98/empathy-interview-data" | |
| FILENAME = "sessions.json" | |
| def load_data(): | |
| """Load complete data structure from HuggingFace Dataset""" | |
| try: | |
| filepath = hf_hub_download( | |
| repo_id=DATASET_ID, | |
| filename=FILENAME, | |
| repo_type="dataset", | |
| token=HF_TOKEN | |
| ) | |
| with open(filepath, 'r', encoding='utf-8') as f: | |
| data = json.load(f) | |
| # Ensure structure | |
| if 'sessions' not in data: | |
| data = {'sessions': {}, 'analyses': {}} | |
| if 'analyses' not in data: | |
| data['analyses'] = {} | |
| return data | |
| except Exception as e: | |
| print(f"Error loading data: {e}") | |
| return {'sessions': {}, 'analyses': {}} | |
| def save_data(data): | |
| """Save complete data structure to HuggingFace Dataset""" | |
| if not HF_TOKEN: | |
| print("Warning: No HF_TOKEN found") | |
| return | |
| try: | |
| api = HfApi() | |
| with tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', encoding='utf-8') as f: | |
| json.dump(data, f, ensure_ascii=False, indent=2) | |
| temp_path = f.name | |
| api.upload_file( | |
| path_or_fileobj=temp_path, | |
| path_in_repo=FILENAME, | |
| repo_id=DATASET_ID, | |
| repo_type="dataset", | |
| token=HF_TOKEN, | |
| commit_message=f"Update data - {datetime.now().isoformat()}" | |
| ) | |
| os.remove(temp_path) | |
| print("Data saved successfully!") | |
| except Exception as e: | |
| print(f"Error saving data: {e}") | |
| def add_session(session_id, questions, api_key, study_name): | |
| """Add a new session with study identifier""" | |
| data = load_data() | |
| data['sessions'][session_id] = { | |
| 'questions': questions, | |
| 'api_key': api_key, | |
| 'study_name': study_name, | |
| 'created_at': datetime.now().isoformat(), | |
| 'responses': [] | |
| } | |
| save_data(data) | |
| return data['sessions'][session_id] | |
| def add_response(session_id, response_data): | |
| """Add a response to a session""" | |
| data = load_data() | |
| if session_id in data['sessions']: | |
| data['sessions'][session_id]['responses'].append({ | |
| 'timestamp': datetime.now().isoformat(), | |
| 'data': response_data | |
| }) | |
| save_data(data) | |
| return True | |
| return False | |
| def update_global_analysis(analysis, study_name): | |
| """Update analysis for a specific study""" | |
| data = load_data() | |
| if 'analyses' not in data: | |
| data['analyses'] = {} | |
| data['analyses'][study_name] = { | |
| **analysis, | |
| 'updated_at': datetime.now().isoformat() | |
| } | |
| save_data(data) | |
| return True | |
| def get_session(session_id): | |
| """Get a specific session""" | |
| data = load_data() | |
| return data['sessions'].get(session_id, None) | |
| def get_all_responses(): | |
| """Get all responses from all sessions (deprecated - use get_responses_by_study)""" | |
| data = load_data() | |
| all_responses = [] | |
| for session_id, session in data['sessions'].items(): | |
| for response in session['responses']: | |
| all_responses.append(response['data']) | |
| return all_responses, len(data['sessions']) | |
| def get_responses_by_study(study_name): | |
| """Get all responses for a specific study""" | |
| data = load_data() | |
| all_responses = [] | |
| session_count = 0 | |
| for session_id, session in data['sessions'].items(): | |
| if session.get('study_name') == study_name: | |
| session_count += 1 | |
| for response in session['responses']: | |
| all_responses.append(response['data']) | |
| return all_responses, session_count | |
| def get_unique_studies(): | |
| """Get list of all unique study names""" | |
| data = load_data() | |
| studies = set() | |
| for session_id, session in data['sessions'].items(): | |
| study_name = session.get('study_name', 'Unnamed Study') | |
| studies.add(study_name) | |
| return sorted(list(studies)) | |
| def get_global_data(): | |
| """Get complete global data for analysis""" | |
| data = load_data() | |
| total_sessions = len(data['sessions']) | |
| total_responses = sum(len(s['responses']) for s in data['sessions'].values()) | |
| return { | |
| 'sessions': data['sessions'], | |
| 'analyses': data.get('analyses', {}), | |
| 'total_sessions': total_sessions, | |
| 'total_responses': total_responses | |
| } |