Commit
Β·
f49753e
1
Parent(s):
2097562
changed subtitle
Browse files
app.py
CHANGED
@@ -22,7 +22,6 @@ else:
|
|
22 |
print("OPENAI_API_KEY not found. Please check your Secrets configuration.")
|
23 |
|
24 |
def extract_leaf_nodes(data, parent_key=''):
|
25 |
-
"""Extract only the leaf nodes (keys without nested key-value pairs)."""
|
26 |
leaf_nodes = {}
|
27 |
for key, value in data.items():
|
28 |
new_key = f"{parent_key}.{key}" if parent_key else key
|
@@ -37,7 +36,7 @@ def extract_leaf_nodes(data, parent_key=''):
|
|
37 |
|
38 |
def agent_response(schema_json: str, topic: str):
|
39 |
try:
|
40 |
-
#
|
41 |
schema = json.loads(schema_json)
|
42 |
except json.JSONDecodeError:
|
43 |
return "Invalid JSON schema.", 0.0
|
@@ -48,30 +47,28 @@ def agent_response(schema_json: str, topic: str):
|
|
48 |
"extraction_schema": schema,
|
49 |
})
|
50 |
|
51 |
-
#
|
52 |
start_time = time.time()
|
53 |
result = asyncio.run(fetch_data(schema, topic))
|
54 |
processing_time = time.time() - start_time
|
55 |
|
56 |
-
#
|
57 |
info = result.get('info', {})
|
58 |
|
59 |
-
# Extract only the leaf nodes for display
|
60 |
leaf_nodes = extract_leaf_nodes(info)
|
61 |
|
62 |
-
#
|
63 |
display_data = "\n\n".join(f"**{key}**: {value}" for key, value in leaf_nodes.items())
|
64 |
|
65 |
return display_data, processing_time
|
66 |
|
67 |
-
# Define the Gradio interface
|
68 |
with gr.Blocks() as demo:
|
69 |
gr.Markdown(
|
70 |
"""
|
71 |
<div style="text-align: center;">
|
72 |
<h1 style="color: #4CAF50;">π Enrichment Agent Interface π</h1>
|
73 |
<p style="font-size: 1.2em; color: #555;">
|
74 |
-
|
75 |
</p>
|
76 |
</div>
|
77 |
"""
|
@@ -108,5 +105,4 @@ with gr.Blocks() as demo:
|
|
108 |
|
109 |
submit_button.click(on_submit, inputs=[schema_input, topic_input], outputs=[output_display, time_display])
|
110 |
|
111 |
-
# Launch the interface
|
112 |
demo.launch(share=True)
|
|
|
22 |
print("OPENAI_API_KEY not found. Please check your Secrets configuration.")
|
23 |
|
24 |
def extract_leaf_nodes(data, parent_key=''):
|
|
|
25 |
leaf_nodes = {}
|
26 |
for key, value in data.items():
|
27 |
new_key = f"{parent_key}.{key}" if parent_key else key
|
|
|
36 |
|
37 |
def agent_response(schema_json: str, topic: str):
|
38 |
try:
|
39 |
+
# parse the schema JSON string
|
40 |
schema = json.loads(schema_json)
|
41 |
except json.JSONDecodeError:
|
42 |
return "Invalid JSON schema.", 0.0
|
|
|
47 |
"extraction_schema": schema,
|
48 |
})
|
49 |
|
50 |
+
# calc processing time
|
51 |
start_time = time.time()
|
52 |
result = asyncio.run(fetch_data(schema, topic))
|
53 |
processing_time = time.time() - start_time
|
54 |
|
55 |
+
# get 'info' dictionary from the result
|
56 |
info = result.get('info', {})
|
57 |
|
|
|
58 |
leaf_nodes = extract_leaf_nodes(info)
|
59 |
|
60 |
+
# format the key-value pairs as Markdown with newlines
|
61 |
display_data = "\n\n".join(f"**{key}**: {value}" for key, value in leaf_nodes.items())
|
62 |
|
63 |
return display_data, processing_time
|
64 |
|
|
|
65 |
with gr.Blocks() as demo:
|
66 |
gr.Markdown(
|
67 |
"""
|
68 |
<div style="text-align: center;">
|
69 |
<h1 style="color: #4CAF50;">π Enrichment Agent Interface π</h1>
|
70 |
<p style="font-size: 1.2em; color: #555;">
|
71 |
+
Gathers information about a topic and shows them in structured format.
|
72 |
</p>
|
73 |
</div>
|
74 |
"""
|
|
|
105 |
|
106 |
submit_button.click(on_submit, inputs=[schema_input, topic_input], outputs=[output_display, time_display])
|
107 |
|
|
|
108 |
demo.launch(share=True)
|