Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,99 +1,157 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
|
|
3 |
|
4 |
# API URL
|
5 |
API_URL = "https://startrz-devi.hf.space/api/v1/prediction/e54adffc-ae77-42e5-9fc0-c4584e081093"
|
6 |
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
def query(payload):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
try:
|
|
|
10 |
response = requests.post(API_URL, json=payload)
|
11 |
-
response.raise_for_status()
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
tool_details = []
|
|
|
|
|
16 |
if "agentReasoning" in data:
|
17 |
for reasoning in data.get("agentReasoning", []):
|
|
|
18 |
for tool in reasoning.get("usedTools", []):
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
tool_output = tool.get("toolOutput", "No output available")
|
28 |
-
|
29 |
-
tool_details.append({
|
30 |
-
"tool": tool.get("tool", "Unknown Tool"),
|
31 |
-
"toolInput": tool_input,
|
32 |
-
"toolOutput": tool_output
|
33 |
-
})
|
34 |
-
|
35 |
-
# Return the full response and tool details
|
36 |
-
return {"raw_response": data, "tool_details": tool_details}
|
37 |
except requests.exceptions.RequestException as e:
|
38 |
-
return {"error": str(e)}
|
|
|
|
|
39 |
except Exception as e:
|
40 |
-
return {"error": f"Unexpected
|
41 |
|
42 |
-
# Streamlit app
|
43 |
def main():
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
#
|
48 |
-
user_input = st.text_input(
|
|
|
|
|
|
|
49 |
|
50 |
-
#
|
51 |
-
if st.button("
|
52 |
if user_input:
|
53 |
-
#
|
54 |
-
with st.spinner("
|
55 |
-
response = query({
|
56 |
-
"question": user_input,
|
57 |
-
})
|
58 |
|
59 |
-
#
|
60 |
if "error" in response:
|
61 |
-
st.error(
|
62 |
return
|
63 |
|
64 |
-
# Display Online Resources
|
65 |
-
st.
|
|
|
66 |
tool_details = response.get("tool_details", [])
|
67 |
|
68 |
if tool_details:
|
69 |
-
# Create
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
if isinstance(tool_output, (list, dict)):
|
81 |
-
st.json(tool_output)
|
82 |
-
elif isinstance(tool_output, str):
|
83 |
-
st.write(tool_output)
|
84 |
-
else:
|
85 |
-
st.write(str(tool_output))
|
86 |
|
87 |
-
|
88 |
-
st.
|
|
|
|
|
89 |
else:
|
90 |
-
st.
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
# Display the raw JSON response in the sidebar
|
93 |
-
st.sidebar.header("Raw JSON Response")
|
94 |
-
st.sidebar.json(response.get("raw_response", {}))
|
95 |
else:
|
96 |
-
st.warning("Please enter a question
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
+
import json
|
4 |
|
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
|
11 |
+
|
12 |
+
Args:
|
13 |
+
tool (dict): A single tool dictionary from the API response
|
14 |
+
|
15 |
+
Returns:
|
16 |
+
dict: Parsed tool details with consistent structure
|
17 |
+
"""
|
18 |
+
# Parse toolInput
|
19 |
+
input_value = ""
|
20 |
+
if isinstance(tool.get("toolInput"), dict):
|
21 |
+
input_value = tool["toolInput"].get("input", "")
|
22 |
+
# Fallback to full input dict as string if no 'input' key
|
23 |
+
if not input_value:
|
24 |
+
input_value = json.dumps(tool["toolInput"], indent=2)
|
25 |
+
elif isinstance(tool.get("toolInput"), str):
|
26 |
+
input_value = tool["toolInput"]
|
27 |
+
else:
|
28 |
+
input_value = str(tool.get("toolInput", "No input details"))
|
29 |
+
|
30 |
+
# Parse toolOutput
|
31 |
+
output_value = tool.get("toolOutput")
|
32 |
+
|
33 |
+
# Flexible output handling
|
34 |
+
if output_value is None:
|
35 |
+
output_value = "No output available"
|
36 |
+
elif isinstance(output_value, (list, dict)):
|
37 |
+
# Convert to formatted JSON string for better readability
|
38 |
+
try:
|
39 |
+
output_value = json.dumps(output_value, indent=2)
|
40 |
+
except Exception:
|
41 |
+
output_value = str(output_value)
|
42 |
+
else:
|
43 |
+
# Convert to string for any other type
|
44 |
+
output_value = str(output_value)
|
45 |
+
|
46 |
+
return {
|
47 |
+
"tool": tool.get("tool", "Unknown Tool"),
|
48 |
+
"toolInput": input_value,
|
49 |
+
"toolOutput": output_value
|
50 |
+
}
|
51 |
+
|
52 |
def query(payload):
|
53 |
+
"""
|
54 |
+
Query the API and process the response
|
55 |
+
|
56 |
+
Args:
|
57 |
+
payload (dict): Question payload to send to the API
|
58 |
+
|
59 |
+
Returns:
|
60 |
+
dict: Processed response with tool details
|
61 |
+
"""
|
62 |
try:
|
63 |
+
# Send POST request to the API
|
64 |
response = requests.post(API_URL, json=payload)
|
65 |
+
response.raise_for_status()
|
66 |
+
|
67 |
+
# Parse the JSON response
|
68 |
+
data = response.json()
|
69 |
+
|
70 |
+
# Extract tool details
|
71 |
tool_details = []
|
72 |
+
|
73 |
+
# Handle different potential response structures
|
74 |
if "agentReasoning" in data:
|
75 |
for reasoning in data.get("agentReasoning", []):
|
76 |
+
# Extract used tools from each reasoning step
|
77 |
for tool in reasoning.get("usedTools", []):
|
78 |
+
parsed_tool = parse_tool_details(tool)
|
79 |
+
tool_details.append(parsed_tool)
|
80 |
+
|
81 |
+
return {
|
82 |
+
"raw_response": data,
|
83 |
+
"tool_details": tool_details
|
84 |
+
}
|
85 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
except requests.exceptions.RequestException as e:
|
87 |
+
return {"error": f"API Request Error: {str(e)}"}
|
88 |
+
except json.JSONDecodeError as e:
|
89 |
+
return {"error": f"JSON Parsing Error: {str(e)}"}
|
90 |
except Exception as e:
|
91 |
+
return {"error": f"Unexpected Error: {str(e)}"}
|
92 |
|
|
|
93 |
def main():
|
94 |
+
"""
|
95 |
+
Main Streamlit application function
|
96 |
+
"""
|
97 |
+
st.set_page_config(
|
98 |
+
page_title="DEVI Research Assistant",
|
99 |
+
page_icon="π",
|
100 |
+
layout="wide"
|
101 |
+
)
|
102 |
+
|
103 |
+
st.title("π¬ DEVI RESEARCH ASSISTANT")
|
104 |
+
st.write("Explore insights by asking a research question!")
|
105 |
|
106 |
+
# User input section
|
107 |
+
user_input = st.text_input(
|
108 |
+
"What would you like to research?",
|
109 |
+
placeholder="Enter your research query here..."
|
110 |
+
)
|
111 |
|
112 |
+
# Submit button
|
113 |
+
if st.button("Explore Insights", type="primary"):
|
114 |
if user_input:
|
115 |
+
# Progress spinner during API call
|
116 |
+
with st.spinner("Gathering research insights..."):
|
117 |
+
response = query({"question": user_input})
|
|
|
|
|
118 |
|
119 |
+
# Error handling
|
120 |
if "error" in response:
|
121 |
+
st.error(response["error"])
|
122 |
return
|
123 |
|
124 |
+
# Display Online Resources
|
125 |
+
st.header("π Online Resources")
|
126 |
+
|
127 |
tool_details = response.get("tool_details", [])
|
128 |
|
129 |
if tool_details:
|
130 |
+
# Create tabs for each resource
|
131 |
+
tabs = st.tabs([
|
132 |
+
f"{idx+1}. {tool['tool']}"
|
133 |
+
for idx, tool in enumerate(tool_details)
|
134 |
+
])
|
135 |
+
|
136 |
+
# Populate each tab with resource details
|
137 |
+
for idx, (tool, tab) in enumerate(zip(tool_details, tabs)):
|
138 |
+
with tab:
|
139 |
+
st.subheader("Research Input")
|
140 |
+
st.code(tool['toolInput'], language=None)
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
+
st.subheader("Research Findings")
|
143 |
+
# Use st.code for better formatting
|
144 |
+
st.code(tool['toolOutput'], language=None)
|
145 |
+
|
146 |
else:
|
147 |
+
st.info("No resources found for this query.")
|
148 |
+
|
149 |
+
# Raw response in expander for advanced users
|
150 |
+
with st.expander("π Advanced: Full API Response"):
|
151 |
+
st.json(response.get("raw_response", {}))
|
152 |
|
|
|
|
|
|
|
153 |
else:
|
154 |
+
st.warning("Please enter a research question!")
|
155 |
|
156 |
if __name__ == "__main__":
|
157 |
main()
|