SyedZaid-Bin-Haris commited on
Commit
fc59df6
·
verified ·
1 Parent(s): a0ad921

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +300 -0
app.py ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import time
4
+ import json
5
+ from datetime import datetime
6
+ import random
7
+ import string
8
+
9
+ def main():
10
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as demo:
11
+ # Header
12
+ gr.Markdown("# AI Synthesis Manager")
13
+
14
+ # ID-based process handling
15
+ with gr.Row():
16
+ with gr.Column(scale=2):
17
+ # New synthesis section
18
+ gr.Markdown("## Create New Synthesis")
19
+ query_input = gr.Textbox(
20
+ label="Enter Query:",
21
+ placeholder="What topic would you like to generate a synthesis for?",
22
+ lines=2
23
+ )
24
+
25
+ with gr.Row():
26
+ create_button = gr.Button("Generate New Synthesis ID", variant="primary")
27
+ synthesis_id = gr.Textbox(label="Synthesis ID", placeholder="ID will appear here...")
28
+
29
+ with gr.Column(scale=2):
30
+ # Continue existing synthesis section
31
+ gr.Markdown("## Continue Existing Synthesis")
32
+ existing_id_input = gr.Textbox(
33
+ label="Enter Existing Synthesis ID:",
34
+ placeholder="Enter ID to continue from a previous session..."
35
+ )
36
+
37
+ with gr.Row():
38
+ load_button = gr.Button("Load Synthesis State")
39
+
40
+ # Synthesis status display
41
+ with gr.Row(visible=False) as status_panel:
42
+ gr.Markdown("## Current Synthesis Status")
43
+
44
+ with gr.Column():
45
+ status_query = gr.Textbox(label="Query")
46
+ status_id = gr.Textbox(label="Synthesis ID")
47
+
48
+ with gr.Row():
49
+ status_created = gr.Textbox(label="Created", scale=1)
50
+ status_updated = gr.Textbox(label="Last Updated", scale=1)
51
+
52
+ with gr.Row():
53
+ status_progress = gr.Slider(
54
+ minimum=0, maximum=8, step=1, value=3,
55
+ label="Progress (Steps Completed)"
56
+ )
57
+ status_cost = gr.Number(label="Cost So Far ($)", value=0.00)
58
+
59
+ # Step Status Cards
60
+ steps_container = gr.Row(visible=False)
61
+ with steps_container:
62
+ gr.Markdown("## Pipeline Steps")
63
+
64
+ with gr.Column():
65
+ # Step 1: Preoutline
66
+ with gr.Group():
67
+ gr.Markdown("### 1. Preoutline")
68
+ step1_status = gr.Markdown("✅ **Completed**")
69
+ with gr.Row():
70
+ step1_view = gr.Button("View Results", size="sm")
71
+ step1_download = gr.Button("Download", size="sm")
72
+
73
+ # Step 2: Themes Generator
74
+ with gr.Group():
75
+ gr.Markdown("### 2. Themes Generator")
76
+ step2_status = gr.Markdown("✅ **Completed**")
77
+ with gr.Row():
78
+ step2_view = gr.Button("View Results", size="sm")
79
+ step2_download = gr.Button("Download", size="sm")
80
+
81
+ # Step 3: Outline
82
+ with gr.Group():
83
+ gr.Markdown("### 3. Outline")
84
+ step3_status = gr.Markdown("❌ **Failed** (Connection timeout)")
85
+ with gr.Row():
86
+ step3_retry = gr.Button("Retry Step", size="sm", variant="primary")
87
+ step3_view = gr.Button("View Partial Results", size="sm")
88
+ step3_download = gr.Button("Download Partial", size="sm")
89
+
90
+ # Step 4: Research
91
+ with gr.Group():
92
+ gr.Markdown("### 4. Research")
93
+ step4_status = gr.Markdown("⏳ **Waiting** (Depends on Outline)")
94
+ with gr.Row():
95
+ step4_force = gr.Button("Force Start", size="sm")
96
+
97
+ with gr.Column():
98
+ # Step 5: Thinking Ladder
99
+ with gr.Group():
100
+ gr.Markdown("### 5. Thinking Ladder")
101
+ step5_status = gr.Markdown("⏳ **Waiting** (Depends on Research)")
102
+ with gr.Row():
103
+ step5_force = gr.Button("Force Start", size="sm")
104
+
105
+ # Step 6: Enhanced Outline
106
+ with gr.Group():
107
+ gr.Markdown("### 6. Enhanced Outline")
108
+ step6_status = gr.Markdown("⏳ **Waiting** (Depends on Thinking Ladder)")
109
+ with gr.Row():
110
+ step6_force = gr.Button("Force Start", size="sm")
111
+
112
+ # Step 7: Synthesis
113
+ with gr.Group():
114
+ gr.Markdown("### 7. Synthesis")
115
+ step7_status = gr.Markdown("⏳ **Waiting** (Depends on Enhanced Outline)")
116
+ with gr.Row():
117
+ step7_force = gr.Button("Force Start", size="sm")
118
+
119
+ # Step 8: Accountability
120
+ with gr.Group():
121
+ gr.Markdown("### 8. Accountability")
122
+ step8_status = gr.Markdown("⏳ **Waiting** (Depends on Synthesis)")
123
+ with gr.Row():
124
+ step8_force = gr.Button("Force Start", size="sm")
125
+
126
+ # Result Preview
127
+ with gr.Row(visible=False) as result_panel:
128
+ gr.Markdown("## Results Preview")
129
+ # Removed 'interactive=False' as it's not a valid parameter for gr.JSON
130
+ result_json = gr.JSON(label="Synthesis Results")
131
+
132
+ with gr.Row():
133
+ refresh_results = gr.Button("Refresh Results")
134
+ download_final = gr.Button("Download Final Results")
135
+
136
+ # Log panel
137
+ with gr.Row(visible=False) as log_panel:
138
+ gr.Markdown("## Process Log")
139
+ log_output = gr.Textbox(
140
+ label="",
141
+ lines=8,
142
+ max_lines=15,
143
+ placeholder="Process logs will appear here..."
144
+ )
145
+
146
+ # Mock function for generating a new synthesis ID
147
+ def generate_new_id(query):
148
+ if not query.strip():
149
+ return "Please enter a query first", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), ""
150
+
151
+ # Generate a random ID (in real implementation, this would be more robust)
152
+ synthesis_id = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8))
153
+
154
+ # Update UI
155
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
156
+ return (
157
+ synthesis_id,
158
+ gr.update(visible=True),
159
+ gr.update(visible=True),
160
+ gr.update(visible=True),
161
+ f"[{timestamp}] Created new synthesis with ID: {synthesis_id}\n[{timestamp}] Starting preoutline generation for query: {query}...\n",
162
+ query,
163
+ synthesis_id,
164
+ datetime.now().strftime("%Y-%m-%d %H:%M"),
165
+ datetime.now().strftime("%Y-%m-%d %H:%M"),
166
+ 0.00
167
+ )
168
+
169
+ # Mock function for loading existing synthesis
170
+ def load_existing(existing_id):
171
+ if not existing_id.strip():
172
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), "Please enter a valid synthesis ID"
173
+
174
+ # In a real implementation, this would fetch data from AWS based on the ID
175
+ # For demonstration, we'll use mock data
176
+
177
+ # Update UI components
178
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
179
+
180
+ return (
181
+ gr.update(visible=True),
182
+ gr.update(visible=True),
183
+ gr.update(visible=True),
184
+ f"[{timestamp}] Loaded synthesis with ID: {existing_id}\n[{timestamp}] Last step completed: Themes Generator\n[{timestamp}] Next step: Outline (previously failed)\n",
185
+ "Sustainability in urban planning",
186
+ existing_id,
187
+ "2025-04-25 14:30",
188
+ "2025-04-25 14:45",
189
+ 2,
190
+ 0.85
191
+ )
192
+
193
+ # Connect the handlers
194
+ create_button.click(
195
+ fn=generate_new_id,
196
+ inputs=[query_input],
197
+ outputs=[
198
+ synthesis_id,
199
+ status_panel,
200
+ steps_container,
201
+ log_panel,
202
+ log_output,
203
+ status_query,
204
+ status_id,
205
+ status_created,
206
+ status_updated,
207
+ status_cost
208
+ ]
209
+ )
210
+
211
+ load_button.click(
212
+ fn=load_existing,
213
+ inputs=[existing_id_input],
214
+ outputs=[
215
+ status_panel,
216
+ steps_container,
217
+ log_panel,
218
+ log_output,
219
+ status_query,
220
+ status_id,
221
+ status_created,
222
+ status_updated,
223
+ status_progress,
224
+ status_cost
225
+ ]
226
+ )
227
+
228
+ # Mock function for step retry
229
+ def retry_step(id):
230
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
231
+ return (
232
+ "🟡 **In Progress**...",
233
+ f"[{timestamp}] Retrying Outline step for synthesis ID: {id}\n[{timestamp}] Connecting to AWS...\n"
234
+ )
235
+
236
+ # Connect retry button
237
+ step3_retry.click(
238
+ fn=retry_step,
239
+ inputs=[status_id],
240
+ outputs=[step3_status, log_output]
241
+ )
242
+
243
+ # Mock function for force start
244
+ def force_start(step_name, id):
245
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
246
+ return (
247
+ "🟡 **Force Started**...",
248
+ f"[{timestamp}] Force starting {step_name} step for synthesis ID: {id}\n[{timestamp}] Warning: This step depends on previous incomplete steps\n"
249
+ )
250
+
251
+ # Connect force start buttons
252
+ step4_force.click(
253
+ fn=lambda id: force_start("Research", id),
254
+ inputs=[status_id],
255
+ outputs=[step4_status, log_output]
256
+ )
257
+
258
+ step5_force.click(
259
+ fn=lambda id: force_start("Thinking Ladder", id),
260
+ inputs=[status_id],
261
+ outputs=[step5_status, log_output]
262
+ )
263
+
264
+ # Mock view results for steps
265
+ def view_step_results(step_name, id):
266
+ mock_results = {
267
+ "Preoutline": {
268
+ "main_points": ["Sustainable urban development", "Green infrastructure", "Smart city technologies"],
269
+ "key_concepts": ["Energy efficiency", "Waste management", "Public transportation"],
270
+ "potential_structure": "Introduction → Current challenges → Sustainable solutions → Case studies → Conclusion"
271
+ },
272
+ "Themes": {
273
+ "themes": ["Environmental Impact", "Economic Considerations", "Social Aspects", "Technological Integration"],
274
+ "query": "Sustainability in urban planning",
275
+ "focus_areas": ["Renewable energy in cities", "Waste reduction strategies", "Community involvement"]
276
+ }
277
+ }
278
+
279
+ if step_name in mock_results:
280
+ return gr.update(visible=True, value=mock_results[step_name])
281
+ return gr.update(visible=True, value={"message": f"No results available for {step_name} step"})
282
+
283
+ # Connect view buttons
284
+ step1_view.click(
285
+ fn=lambda: view_step_results("Preoutline", ""),
286
+ inputs=[],
287
+ outputs=[result_panel]
288
+ )
289
+
290
+ step2_view.click(
291
+ fn=lambda: view_step_results("Themes", ""),
292
+ inputs=[],
293
+ outputs=[result_panel]
294
+ )
295
+
296
+ return demo
297
+
298
+ if __name__ == "__main__":
299
+ demo = main()
300
+ demo.launch(debug=True)