Coerce a string param value (from the TTSRequest.params map, which is string-typed on the wire) into the most specific Python type the model generation kwargs expect: bool, int, float, else the original string.
(value)
| 48 | |
| 49 | |
| 50 | def coerce_param_value(value): |
| 51 | """Coerce a string param value (from the TTSRequest.params map, which is |
| 52 | string-typed on the wire) into the most specific Python type the model |
| 53 | generation kwargs expect: bool, int, float, else the original string.""" |
| 54 | if not isinstance(value, str): |
| 55 | return value |
| 56 | lowered = value.strip().lower() |
| 57 | if lowered in ("true", "false"): |
| 58 | return lowered == "true" |
| 59 | try: |
| 60 | return int(value) |
| 61 | except ValueError: |
| 62 | pass |
| 63 | try: |
| 64 | return float(value) |
| 65 | except ValueError: |
| 66 | pass |
| 67 | return value |
| 68 | |
| 69 | |
| 70 | _ONE_DAY_IN_SECONDS = 60 * 60 * 24 |