-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_demo.py
More file actions
246 lines (201 loc) · 9.18 KB
/
final_demo.py
File metadata and controls
246 lines (201 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/env python3
"""
FINAL DEMO - Document Upload + Real AI Chat
Simplified to avoid problematic dialogs
"""
import asyncio
import os
import shutil
from pathlib import Path
from playwright.async_api import async_playwright
OUTPUT_DIR = Path("docs/images")
GIF_PATH = OUTPUT_DIR / "demo.gif"
FRONTEND_URL = "http://localhost:3000"
SAMPLE_DOC = Path("sample_docs/product_guide.txt").absolute()
async def main():
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
frames_dir = OUTPUT_DIR / "frames"
if frames_dir.exists():
shutil.rmtree(frames_dir)
frames_dir.mkdir()
print("=" * 60)
print("FINAL DEMO - Document + AI Chat")
print("=" * 60)
async with async_playwright() as p:
browser = await p.chromium.launch(headless=False, slow_mo=20)
ctx = await browser.new_context(viewport={"width": 1280, "height": 720})
page = await ctx.new_page()
fc = [0]
async def snap(d="", n=1):
for i in range(n):
fc[0] += 1
await page.screenshot(path=str(frames_dir / f"f_{fc[0]:03d}.png"))
if i == 0 and d: print(f" [{fc[0]:2d}] {d}")
await asyncio.sleep(0.06)
try:
# 1. LANDING
print("\n[1] Landing")
await page.goto(FRONTEND_URL, timeout=20000)
await asyncio.sleep(1)
await snap("Info Naut", 3)
# 2. DASHBOARD
print("[2] Dashboard")
await page.goto(f"{FRONTEND_URL}/dashboard", timeout=20000)
await asyncio.sleep(1)
await snap("Dashboard", 3)
# 3. CREATE KB
print("[3] Create Knowledge Base")
await page.goto(f"{FRONTEND_URL}/dashboard/knowledge/new", timeout=20000)
await asyncio.sleep(1)
await snap("New KB", 2)
await page.locator('input').first.fill("AI Documentation")
await asyncio.sleep(0.3)
await snap("Name", 2)
textarea = page.locator('textarea').first
if await textarea.count() > 0:
await textarea.fill("Complete AI product docs")
await snap("Description", 2)
await page.click('button:has-text("Create")')
await asyncio.sleep(2)
await snap("KB Created!", 3)
# 4. UPLOAD DOCUMENT
print("[4] Upload Document")
await page.goto(f"{FRONTEND_URL}/dashboard/knowledge", timeout=20000)
await asyncio.sleep(1.5)
await snap("KB List", 2)
# Click KB
kb_link = page.locator('a[href*="/knowledge/"]').first
if await kb_link.count() > 0:
await kb_link.click()
await asyncio.sleep(2)
await snap("Inside KB", 2)
# Upload
file_input = page.locator('input[type="file"]')
if await file_input.count() > 0:
print(" Uploading document...")
await file_input.set_input_files(str(SAMPLE_DOC))
await asyncio.sleep(1)
await snap("Doc Selected", 2)
await asyncio.sleep(4)
await snap("Doc Uploaded!", 3)
await snap("KB with Doc", 3)
# 5. CREATE CHAT
print("[5] Start Chat")
await page.goto(f"{FRONTEND_URL}/dashboard/chat/new", timeout=20000)
await asyncio.sleep(2)
await snap("New Chat", 2)
# Select KB with radio button
radio = page.locator('input[type="radio"]').first
if await radio.count() > 0:
await radio.check()
await asyncio.sleep(0.5)
await snap("KB Selected", 2)
# Fill title if input exists
title_input = page.locator('input[id="title"], input[name="title"]')
if await title_input.count() > 0:
await title_input.fill("Product Questions")
await asyncio.sleep(0.3)
# Click Start Chat button and wait for navigation
start_btn = page.locator('button:has-text("Start Chat")')
if await start_btn.count() > 0:
print(" Clicking Start Chat...")
await start_btn.click()
# Wait for navigation to chat page
await page.wait_for_url("**/dashboard/chat/*", timeout=10000)
await asyncio.sleep(2)
await snap("Chat Started!", 3)
print(f" Navigated to: {page.url}")
else:
# Try generic submit
submit = page.locator('button[type="submit"]')
if await submit.count() > 0:
await submit.click()
await asyncio.sleep(3)
await snap("Chat Started!", 3)
# 6. CHAT WITH AI
print("[6] Chat with AI")
# Wait for navigation and page load
await asyncio.sleep(5)
print(f" Current URL: {page.url}")
await snap("Chat Interface", 3)
# Wait for message input with specific placeholder
await page.wait_for_selector('input[placeholder="Type your message..."]', timeout=15000)
msg = page.locator('input[placeholder="Type your message..."]')
msg_count = await msg.count()
print(f" Found {msg_count} message inputs")
if msg_count > 0:
# Q1
q1 = "What are the key features of Info Naut?"
print(f" Q1: {q1}")
await msg.click()
await msg.fill(q1)
await asyncio.sleep(0.5)
await snap("Question 1 Typed", 3)
submit = page.locator('button[type="submit"]')
await submit.click()
await snap("Sending...", 2)
print(" Waiting for OpenAI response...")
await asyncio.sleep(15)
await snap("AI Response!", 6)
await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
await asyncio.sleep(1)
await snap("Full AI Response", 4)
# Q2 Follow-up
print(" Q2: Follow-up question")
await asyncio.sleep(2)
msg2 = page.locator('input[placeholder="Type your message..."]')
await msg2.fill("How does the vector search work?")
await asyncio.sleep(0.5)
await snap("Question 2", 3)
await page.locator('button[type="submit"]').click()
print(" Waiting for 2nd response...")
await asyncio.sleep(12)
await snap("AI Response 2!", 5)
await page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
await asyncio.sleep(1)
await snap("Complete Chat", 4)
else:
print(" ERROR: Message input not found! Taking screenshot of current page")
await page.screenshot(path="debug_chat_page.png")
await snap("Chat page", 3)
# 7. API KEYS (just show page)
print("[7] API Keys")
await page.goto(f"{FRONTEND_URL}/dashboard/api-keys", timeout=20000)
await asyncio.sleep(1.5)
await snap("API Keys", 3)
# 8. DONE
print("[8] Complete")
await page.goto(f"{FRONTEND_URL}/dashboard", timeout=20000)
await asyncio.sleep(1)
await snap("Done!", 3)
except Exception as e:
print(f"Error: {e}")
await snap("Error state", 2)
await browser.close()
print(f"\nFrames: {fc[0]}")
# GIF
print("\nCreating GIF...")
from PIL import Image
files = sorted(frames_dir.glob("f_*.png"))
if files:
frames = []
for f in files:
img = Image.open(f).resize((800, 450), Image.LANCZOS)
img = img.convert('P', palette=Image.ADAPTIVE, colors=256)
frames.append(img)
frames[0].save(GIF_PATH, save_all=True, append_images=frames[1:],
duration=250, loop=0, optimize=True)
sz = os.path.getsize(GIF_PATH) / (1024*1024)
print(f"GIF: {sz:.2f} MB ({len(frames)} frames)")
if sz > 5:
opt = [frames[i] for i in range(0, len(frames), 2)]
opt[0].save(GIF_PATH, save_all=True, append_images=opt[1:],
duration=500, loop=0, optimize=True)
shutil.rmtree(frames_dir)
print("\n" + "="*60)
print("SUCCESS!")
print("="*60)
else:
print("No frames!")
if __name__ == "__main__":
asyncio.run(main())