-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app_support.py
More file actions
64 lines (53 loc) · 1.92 KB
/
Copy pathstreamlit_app_support.py
File metadata and controls
64 lines (53 loc) · 1.92 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from typing import Any, Dict, Mapping, Sequence
def in_streamlit_context() -> bool:
try:
from streamlit.runtime.scriptrunner import get_script_run_ctx
except Exception:
return False
return get_script_run_ctx(suppress_warning=True) is not None
def normalize_payload(
payload: Any,
*,
question: str,
method: str,
tools: Sequence[str],
service_url: str,
method_labels: Mapping[str, str],
) -> Dict[str, Any]:
method_value = str(method or "")
base: Dict[str, Any] = {
"question": question,
"method": method_value,
"method_label": method_labels.get(method_value, method_value),
"tools": list(tools),
"service_url": service_url,
"final_answer": None,
"steps": [],
"plan_dag": None,
"error": "",
}
if payload is None:
base["error"] = "Pipeline returned no payload."
return base
if not isinstance(payload, dict):
base["error"] = f"Pipeline returned unsupported payload type: {type(payload).__name__}."
return base
if not payload:
base["error"] = "Pipeline returned an empty payload."
return base
base.update(payload)
base["question"] = str(base.get("question") or question)
method_value = str(base.get("method") or method)
base["method"] = method_value
method_label_value = payload.get("method_label") if isinstance(payload, dict) else None
base["method_label"] = str(method_label_value or method_labels.get(method_value, method_value))
tools_value = base.get("tools")
if isinstance(tools_value, (list, tuple)):
base["tools"] = [str(tool) for tool in tools_value]
else:
base["tools"] = list(tools)
base["service_url"] = str(base.get("service_url") or service_url)
base["error"] = str(base.get("error") or "")
if not isinstance(base.get("steps"), list):
base["steps"] = []
return base