File size: 1,254 Bytes
af4851b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path
from typing import Any


def resolve_uploaded_path(uploaded: Any) -> Path:
    if isinstance(uploaded, Path):
        return uploaded
    if isinstance(uploaded, str):
        return Path(uploaded)
    if isinstance(uploaded, dict):
        for key in ("path", "orig_name", "name"):
            value = uploaded.get(key)
            if isinstance(value, str) and value:
                return Path(value)
    raise TypeError(
        f"Unsupported uploaded file payload: expected path-like value or Gradio file dict, got {type(uploaded).__name__}"
    )


def resolve_uploaded_name(uploaded: Any) -> str:
    if isinstance(uploaded, Path):
        return uploaded.name
    if isinstance(uploaded, str):
        return Path(uploaded).name
    if isinstance(uploaded, dict):
        for key in ("orig_name", "name"):
            value = uploaded.get(key)
            if isinstance(value, str) and value:
                return Path(value).name
        path_value = uploaded.get("path")
        if isinstance(path_value, str) and path_value:
            return Path(path_value).name
    raise TypeError(
        f"Unsupported uploaded file payload: expected path-like value or Gradio file dict, got {type(uploaded).__name__}"
    )