Spaces:
Build error
Build error
| import json | |
| from datetime import datetime | |
| import os | |
| MAX_LOG_ENTRIES = 100 | |
| log_file = os.path.join(os.path.dirname(__file__), "logs.json") | |
| def log(text, from_lang, to_lang, translated_text): | |
| log_entry = { | |
| "text": text, | |
| "from_lang": from_lang, | |
| "to_lang": to_lang, | |
| "translated_text": translated_text, | |
| "timestamp": datetime.now().strftime("%d-%m-%Y %H:%M:%S") | |
| } | |
| try: | |
| with open(log_file, 'r+') as file: | |
| logs = json.load(file) | |
| if "chats" not in logs: | |
| logs["chats"] = [] | |
| logs["chats"].append(log_entry) | |
| if len(logs["chats"]) > MAX_LOG_ENTRIES: | |
| logs["chats"] = logs["chats"][-MAX_LOG_ENTRIES:] | |
| file.seek(0) | |
| json.dump(logs, file, indent=4) | |
| except (FileNotFoundError, json.JSONDecodeError) as e: | |
| print(f"Error occurred: {e}") | |
| with open(log_file, 'w') as file: | |
| json.dump({"chats": [log_entry]}, file, indent=4) | |