Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,25 @@ import json
|
|
5 |
# API URL
|
6 |
API_URL = "https://startrz-devi.hf.space/api/v1/prediction/e54adffc-ae77-42e5-9fc0-c4584e081093"
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def parse_tool_details(tool):
|
9 |
"""
|
10 |
Parse tool details with robust handling of different input types
|
@@ -15,17 +34,30 @@ def parse_tool_details(tool):
|
|
15 |
Returns:
|
16 |
dict: Parsed tool details with consistent structure
|
17 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Parse toolInput
|
19 |
input_value = ""
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
# Fallback to full input dict as string if no 'input' key
|
23 |
if not input_value:
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
27 |
else:
|
28 |
-
input_value = str(
|
29 |
|
30 |
# Parse toolOutput
|
31 |
output_value = tool.get("toolOutput")
|
@@ -71,10 +103,14 @@ def query(payload):
|
|
71 |
tool_details = []
|
72 |
|
73 |
# Handle different potential response structures
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
parsed_tool = parse_tool_details(tool)
|
79 |
tool_details.append(parsed_tool)
|
80 |
|
@@ -129,7 +165,7 @@ def main():
|
|
129 |
if tool_details:
|
130 |
# Create tabs for each resource
|
131 |
tabs = st.tabs([
|
132 |
-
f"{idx+1}. {tool
|
133 |
for idx, tool in enumerate(tool_details)
|
134 |
])
|
135 |
|
@@ -137,11 +173,11 @@ def main():
|
|
137 |
for idx, (tool, tab) in enumerate(zip(tool_details, tabs)):
|
138 |
with tab:
|
139 |
st.subheader("Research Input")
|
140 |
-
st.code(tool
|
141 |
|
142 |
st.subheader("Research Findings")
|
143 |
# Use st.code for better formatting
|
144 |
-
st.code(tool
|
145 |
|
146 |
else:
|
147 |
st.info("No resources found for this query.")
|
|
|
5 |
# API URL
|
6 |
API_URL = "https://startrz-devi.hf.space/api/v1/prediction/e54adffc-ae77-42e5-9fc0-c4584e081093"
|
7 |
|
8 |
+
def safe_get(obj, *keys, default=None):
|
9 |
+
"""
|
10 |
+
Safely navigate through nested dictionaries
|
11 |
+
|
12 |
+
Args:
|
13 |
+
obj: Starting object
|
14 |
+
*keys: Sequence of keys to navigate
|
15 |
+
default: Value to return if navigation fails
|
16 |
+
|
17 |
+
Returns:
|
18 |
+
Value at the specified path or default
|
19 |
+
"""
|
20 |
+
try:
|
21 |
+
for key in keys:
|
22 |
+
obj = obj[key]
|
23 |
+
return obj
|
24 |
+
except (TypeError, KeyError, IndexError):
|
25 |
+
return default
|
26 |
+
|
27 |
def parse_tool_details(tool):
|
28 |
"""
|
29 |
Parse tool details with robust handling of different input types
|
|
|
34 |
Returns:
|
35 |
dict: Parsed tool details with consistent structure
|
36 |
"""
|
37 |
+
# Ensure tool is a dictionary
|
38 |
+
if not isinstance(tool, dict):
|
39 |
+
return {
|
40 |
+
"tool": "Unknown Tool",
|
41 |
+
"toolInput": "Invalid tool data",
|
42 |
+
"toolOutput": "No output available"
|
43 |
+
}
|
44 |
+
|
45 |
# Parse toolInput
|
46 |
input_value = ""
|
47 |
+
tool_input = tool.get("toolInput", {})
|
48 |
+
|
49 |
+
if isinstance(tool_input, dict):
|
50 |
+
input_value = tool_input.get("input", "")
|
51 |
# Fallback to full input dict as string if no 'input' key
|
52 |
if not input_value:
|
53 |
+
try:
|
54 |
+
input_value = json.dumps(tool_input, indent=2)
|
55 |
+
except Exception:
|
56 |
+
input_value = str(tool_input)
|
57 |
+
elif isinstance(tool_input, str):
|
58 |
+
input_value = tool_input
|
59 |
else:
|
60 |
+
input_value = str(tool_input) if tool_input is not None else "No input details"
|
61 |
|
62 |
# Parse toolOutput
|
63 |
output_value = tool.get("toolOutput")
|
|
|
103 |
tool_details = []
|
104 |
|
105 |
# Handle different potential response structures
|
106 |
+
agent_reasoning = safe_get(data, "agentReasoning", default=[])
|
107 |
+
|
108 |
+
for reasoning in agent_reasoning:
|
109 |
+
# Safely extract used tools
|
110 |
+
used_tools = safe_get(reasoning, "usedTools", default=[])
|
111 |
+
|
112 |
+
for tool in used_tools:
|
113 |
+
if tool is not None:
|
114 |
parsed_tool = parse_tool_details(tool)
|
115 |
tool_details.append(parsed_tool)
|
116 |
|
|
|
165 |
if tool_details:
|
166 |
# Create tabs for each resource
|
167 |
tabs = st.tabs([
|
168 |
+
f"{idx+1}. {tool.get('tool', 'Unknown')}"
|
169 |
for idx, tool in enumerate(tool_details)
|
170 |
])
|
171 |
|
|
|
173 |
for idx, (tool, tab) in enumerate(zip(tool_details, tabs)):
|
174 |
with tab:
|
175 |
st.subheader("Research Input")
|
176 |
+
st.code(tool.get('toolInput', 'No input'), language=None)
|
177 |
|
178 |
st.subheader("Research Findings")
|
179 |
# Use st.code for better formatting
|
180 |
+
st.code(tool.get('toolOutput', 'No output'), language=None)
|
181 |
|
182 |
else:
|
183 |
st.info("No resources found for this query.")
|