Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
|
|
4 |
from datetime import datetime
|
5 |
from dotenv import load_dotenv
|
6 |
from openai import OpenAI
|
|
|
7 |
|
8 |
# Load environment variables
|
9 |
load_dotenv()
|
@@ -22,46 +23,111 @@ class ExplorationPathGenerator:
|
|
22 |
if exploration_parameters is None:
|
23 |
exploration_parameters = {}
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
- Multiple interconnected nodes
|
32 |
-
- Clear relationships between nodes
|
33 |
-
- Depth-based organization
|
34 |
-
- Relevant historical context
|
35 |
-
Response must be valid JSON with this structure:
|
36 |
-
{{
|
37 |
-
"nodes": [
|
38 |
-
{{
|
39 |
-
"id": "unique_string",
|
40 |
-
"title": "node_title",
|
41 |
-
"description": "detailed_description",
|
42 |
-
"depth": number,
|
43 |
-
"connections": [
|
44 |
-
{{
|
45 |
-
"target_id": "connected_node_id",
|
46 |
-
"relationship": "description of relationship"
|
47 |
-
}}
|
48 |
-
]
|
49 |
-
}}
|
50 |
-
]
|
51 |
-
}}"""
|
52 |
|
53 |
response = self.client.chat.completions.create(
|
54 |
model="mixtral-8x7b-32768",
|
55 |
messages=[
|
56 |
-
{"role": "system", "content":
|
57 |
-
{"role": "user", "content":
|
58 |
],
|
59 |
temperature=0.7,
|
60 |
max_tokens=4000
|
61 |
)
|
62 |
|
63 |
result = json.loads(response.choices[0].message.content)
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
except Exception as e:
|
67 |
print(f"Error generating exploration path: {e}")
|
@@ -123,8 +189,8 @@ Response must be valid JSON with this structure:
|
|
123 |
html_content += "<strong>Connections:</strong><ul>"
|
124 |
for conn in node['connections']:
|
125 |
html_content += f"<li>Connected to: {conn['target_id']}"
|
126 |
-
if '
|
127 |
-
html_content += f"
|
128 |
html_content += "</li>"
|
129 |
html_content += "</ul>"
|
130 |
|
@@ -163,10 +229,10 @@ def explore(query: str, path_history: str = "[]", parameters: str = "{}", depth:
|
|
163 |
exploration_parameters=exploration_parameters
|
164 |
)
|
165 |
|
166 |
-
# Create visualization
|
167 |
graph_html = generator.create_visualization_html(result.get('nodes', []))
|
168 |
|
169 |
-
summary = f"
|
170 |
|
171 |
return json.dumps(result), graph_html, summary
|
172 |
|
@@ -186,15 +252,15 @@ def create_interface() -> gr.Blocks:
|
|
186 |
theme=gr.themes.Soft()
|
187 |
) as interface:
|
188 |
gr.Markdown("""
|
189 |
-
#
|
190 |
-
Generate interactive exploration paths through
|
191 |
""")
|
192 |
|
193 |
with gr.Row():
|
194 |
with gr.Column(scale=1):
|
195 |
query_input = gr.Textbox(
|
196 |
label="Exploration Query",
|
197 |
-
placeholder="Enter your
|
198 |
lines=2
|
199 |
)
|
200 |
|
@@ -208,7 +274,7 @@ def create_interface() -> gr.Blocks:
|
|
208 |
|
209 |
domain = gr.Textbox(
|
210 |
label="Domain Context",
|
211 |
-
placeholder="Optional: Specify
|
212 |
lines=1
|
213 |
)
|
214 |
|
@@ -221,18 +287,14 @@ def create_interface() -> gr.Blocks:
|
|
221 |
|
222 |
generate_btn.click(
|
223 |
fn=explore,
|
224 |
-
inputs=[
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
examples=[
|
231 |
-
["Explore the evolution of Renaissance painting techniques", 5, "Renaissance"],
|
232 |
-
["Investigate the influence of Japanese art on Impressionism", 7, "Impressionism"],
|
233 |
-
["Analyze the development of Cubism through Picasso's work", 6, "Cubism"]
|
234 |
],
|
235 |
-
|
236 |
)
|
237 |
|
238 |
return interface
|
|
|
4 |
from datetime import datetime
|
5 |
from dotenv import load_dotenv
|
6 |
from openai import OpenAI
|
7 |
+
from prompt import SYSTEM_PROMPT, format_exploration_prompt
|
8 |
|
9 |
# Load environment variables
|
10 |
load_dotenv()
|
|
|
23 |
if exploration_parameters is None:
|
24 |
exploration_parameters = {}
|
25 |
|
26 |
+
# Use the prompt from prompt.py
|
27 |
+
formatted_prompt = format_exploration_prompt(
|
28 |
+
user_query=query,
|
29 |
+
selected_path=selected_path,
|
30 |
+
exploration_parameters=exploration_parameters
|
31 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
response = self.client.chat.completions.create(
|
34 |
model="mixtral-8x7b-32768",
|
35 |
messages=[
|
36 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
37 |
+
{"role": "user", "content": formatted_prompt}
|
38 |
],
|
39 |
temperature=0.7,
|
40 |
max_tokens=4000
|
41 |
)
|
42 |
|
43 |
result = json.loads(response.choices[0].message.content)
|
44 |
+
|
45 |
+
# Convert exploration response to graph format
|
46 |
+
nodes = []
|
47 |
+
node_id_counter = 0
|
48 |
+
|
49 |
+
# Add meta insights as central node
|
50 |
+
node_id_counter += 1
|
51 |
+
meta_node = {
|
52 |
+
"id": f"meta_{node_id_counter}",
|
53 |
+
"title": "Exploration Summary",
|
54 |
+
"description": result["exploration_summary"]["current_context"],
|
55 |
+
"depth": 0,
|
56 |
+
"connections": []
|
57 |
+
}
|
58 |
+
nodes.append(meta_node)
|
59 |
+
|
60 |
+
# Create nodes from standard axes
|
61 |
+
for axis in result["knowledge_axes"]["standard_axes"]:
|
62 |
+
node_id_counter += 1
|
63 |
+
axis_node = {
|
64 |
+
"id": f"std_{node_id_counter}",
|
65 |
+
"title": axis["name"],
|
66 |
+
"description": f"Current values: {', '.join(axis['current_values'])}",
|
67 |
+
"depth": 1,
|
68 |
+
"connections": []
|
69 |
+
}
|
70 |
+
|
71 |
+
# Connect to meta node
|
72 |
+
meta_node["connections"].append({
|
73 |
+
"target_id": axis_node["id"],
|
74 |
+
"relevance_score": 0.8
|
75 |
+
})
|
76 |
+
|
77 |
+
# Add potential values as nodes
|
78 |
+
for value in axis["potential_values"]:
|
79 |
+
node_id_counter += 1
|
80 |
+
value_node = {
|
81 |
+
"id": f"val_{node_id_counter}",
|
82 |
+
"title": value["value"],
|
83 |
+
"description": value["contextual_rationale"],
|
84 |
+
"depth": 2,
|
85 |
+
"connections": []
|
86 |
+
}
|
87 |
+
nodes.append(value_node)
|
88 |
+
axis_node["connections"].append({
|
89 |
+
"target_id": value_node["id"],
|
90 |
+
"relevance_score": value["relevance_score"] / 100
|
91 |
+
})
|
92 |
+
|
93 |
+
nodes.append(axis_node)
|
94 |
+
|
95 |
+
# Create nodes from emergent axes
|
96 |
+
for axis in result["knowledge_axes"]["emergent_axes"]:
|
97 |
+
node_id_counter += 1
|
98 |
+
emergent_node = {
|
99 |
+
"id": f"emg_{node_id_counter}",
|
100 |
+
"title": f"{axis['name']} (Emergent)",
|
101 |
+
"description": f"Parent axis: {axis['parent_axis']}",
|
102 |
+
"depth": 2,
|
103 |
+
"connections": []
|
104 |
+
}
|
105 |
+
|
106 |
+
# Connect to meta node
|
107 |
+
meta_node["connections"].append({
|
108 |
+
"target_id": emergent_node["id"],
|
109 |
+
"relevance_score": 0.6
|
110 |
+
})
|
111 |
+
|
112 |
+
# Add innovative values
|
113 |
+
for value in axis["innovative_values"]:
|
114 |
+
node_id_counter += 1
|
115 |
+
value_node = {
|
116 |
+
"id": f"inv_{node_id_counter}",
|
117 |
+
"title": value["value"],
|
118 |
+
"description": value["discovery_potential"],
|
119 |
+
"depth": 3,
|
120 |
+
"connections": []
|
121 |
+
}
|
122 |
+
nodes.append(value_node)
|
123 |
+
emergent_node["connections"].append({
|
124 |
+
"target_id": value_node["id"],
|
125 |
+
"relevance_score": value["innovation_score"] / 100
|
126 |
+
})
|
127 |
+
|
128 |
+
nodes.append(emergent_node)
|
129 |
+
|
130 |
+
return {"nodes": nodes}
|
131 |
|
132 |
except Exception as e:
|
133 |
print(f"Error generating exploration path: {e}")
|
|
|
189 |
html_content += "<strong>Connections:</strong><ul>"
|
190 |
for conn in node['connections']:
|
191 |
html_content += f"<li>Connected to: {conn['target_id']}"
|
192 |
+
if 'relevance_score' in conn:
|
193 |
+
html_content += f" (Relevance: {conn['relevance_score']:.2f})"
|
194 |
html_content += "</li>"
|
195 |
html_content += "</ul>"
|
196 |
|
|
|
229 |
exploration_parameters=exploration_parameters
|
230 |
)
|
231 |
|
232 |
+
# Create visualization
|
233 |
graph_html = generator.create_visualization_html(result.get('nodes', []))
|
234 |
|
235 |
+
summary = f"Exploration path generated with {len(result.get('nodes', []))} nodes"
|
236 |
|
237 |
return json.dumps(result), graph_html, summary
|
238 |
|
|
|
252 |
theme=gr.themes.Soft()
|
253 |
) as interface:
|
254 |
gr.Markdown("""
|
255 |
+
# Knowledge Exploration Path Generator
|
256 |
+
Generate interactive exploration paths through complex topics.
|
257 |
""")
|
258 |
|
259 |
with gr.Row():
|
260 |
with gr.Column(scale=1):
|
261 |
query_input = gr.Textbox(
|
262 |
label="Exploration Query",
|
263 |
+
placeholder="Enter your exploration query...",
|
264 |
lines=2
|
265 |
)
|
266 |
|
|
|
274 |
|
275 |
domain = gr.Textbox(
|
276 |
label="Domain Context",
|
277 |
+
placeholder="Optional: Specify domain context",
|
278 |
lines=1
|
279 |
)
|
280 |
|
|
|
287 |
|
288 |
generate_btn.click(
|
289 |
fn=explore,
|
290 |
+
inputs=[
|
291 |
+
query_input,
|
292 |
+
gr.Textbox(value="[]", visible=False),
|
293 |
+
gr.Textbox(value="{}", visible=False),
|
294 |
+
depth,
|
295 |
+
domain
|
|
|
|
|
|
|
|
|
296 |
],
|
297 |
+
outputs=[text_output, graph_output, summary_output]
|
298 |
)
|
299 |
|
300 |
return interface
|