File size: 1,811 Bytes
0921a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from __future__ import annotations

import asyncio.base_events as base_events
import os
from pathlib import Path
from typing import Any


def patch_asyncio_cleanup_warning() -> None:
    """Keeps local Gradio teardown from surfacing a known invalid-fd warning."""
    # Skip patching when this Python runtime does not expose the cleanup hook.
    original_del = getattr(base_events.BaseEventLoop, "__del__", None)
    if original_del is None or getattr(original_del, "_pocket_tutor_patched", False):
        return

    # Preserve normal cleanup while ignoring the harmless file descriptor case.
    def patched_del(self: Any) -> None:
        try:
            original_del(self)
        except ValueError as exc:
            if str(exc) != "Invalid file descriptor: -1":
                raise

    # Mark the patch so repeated imports stay idempotent.
    setattr(patched_del, "_pocket_tutor_patched", True)
    setattr(base_events.BaseEventLoop, "__del__", patched_del)


def load_env() -> None:
    """Loads simple KEY=value pairs from a local .env file when present."""
    # Search the project folder before the parent hackathon workspace.
    for path in [Path(".env"), Path("../.env")]:
        if path.is_file():
            try:
                with open(path, encoding="utf-8") as f:
                    for line in f:
                        line = line.strip()
                        if line and not line.startswith("#") and "=" in line:
                            key, value = line.split("=", 1)
                            os.environ.setdefault(
                                key.strip(), value.strip().strip("'\"")
                            )
                break
            except Exception:
                pass


# Load local secrets before model or Modal clients are used.
load_env()