Spaces:
Build error
Build error
| import os | |
| import json | |
| import uuid | |
| from datetime import datetime | |
| from pathlib import Path | |
| from dotenv import load_dotenv | |
| from typing import Dict, Any, List, Optional | |
| # Load environment variables | |
| load_dotenv() | |
| # Create directory for saving results if it doesn't exist | |
| def ensure_directories(): | |
| """Create necessary directories if they don't exist.""" | |
| os.makedirs("ibfs_results", exist_ok=True) | |
| def generate_user_id() -> str: | |
| """Generate a unique user ID.""" | |
| return str(uuid.uuid4()) | |
| def save_results(user_id: str, query: str, final_answer: str, method: str = "ibfs", | |
| strategy_path: Optional[List[str]] = None) -> str: | |
| """ | |
| Save the results to a file. | |
| Args: | |
| user_id: Unique identifier for the user | |
| query: Original query | |
| final_answer: Final answer generated | |
| method: Method used (ibfs or zero_shot) | |
| strategy_path: Path of selected strategies (for IBFS) | |
| Returns: | |
| Filename where results were saved | |
| """ | |
| timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") | |
| result = { | |
| "user_id": user_id, | |
| "timestamp": timestamp, | |
| "method": method, | |
| "query": query, | |
| "final_answer": final_answer | |
| } | |
| # Add strategy path for IBFS method | |
| if method == "ibfs" and strategy_path: | |
| result["strategy_path"] = strategy_path | |
| filename = f"ibfs_results/{user_id}_{method}_{timestamp}.json" | |
| with open(filename, "w") as f: | |
| json.dump(result, f, indent=2) | |
| return filename | |
| # Initialize directories when module is imported | |
| ensure_directories() |