File size: 10,833 Bytes
509a107 08511e7 509a107 |
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
import { useState, useEffect } from 'react'
import axios from 'axios'
import ScreenCapture from './ScreenCapture'
import './App.css'
const API_BASE = import.meta.env.VITE_API_URL || (window.location.hostname === 'localhost' ? 'http://localhost:8000' : '/api')
function App() {
const [isCapturing, setIsCapturing] = useState(false)
const [analysis, setAnalysis] = useState(null)
const [logs, setLogs] = useState([])
const [includeThumbnail, setIncludeThumbnail] = useState(false)
const [autoCapture, setAutoCapture] = useState(false)
const [captureInterval, setCaptureInterval] = useState(5000)
const [demoStatus, setDemoStatus] = useState('')
useEffect(() => {
const eventSource = new EventSource(`${API_BASE}/logs/stream`)
eventSource.onmessage = (event) => {
try {
const log = JSON.parse(event.data)
setLogs(prev => [...prev, log].slice(-50))
} catch (e) {
console.error('Log parsing error:', e)
}
}
return () => eventSource.close()
}, [])
useEffect(() => {
let intervalId
if (autoCapture) {
intervalId = setInterval(() => {
captureScreen()
}, captureInterval)
}
return () => clearInterval(intervalId)
}, [autoCapture, captureInterval])
const captureScreen = async () => {
setIsCapturing(true)
try {
const response = await axios.post(`${API_BASE}/analyze`, {
capture_screen: true,
include_thumbnail: includeThumbnail
})
// Check if the response indicates an error
if (response.data.risk_flags && response.data.risk_flags.includes('ANALYSIS_ERROR')) {
// Handle model error gracefully
setAnalysis({
summary: 'Model is loading or experiencing memory constraints. The system is configured correctly but requires more RAM for full operation.',
ui_elements: [],
text_snippets: [],
risk_flags: [], // Don't show error as a risk flag
timestamp: response.data.timestamp || new Date().toISOString(),
model_info: response.data.model_info
})
} else {
setAnalysis(response.data)
}
} catch (error) {
console.error('Capture error:', error)
setAnalysis({
summary: 'Error capturing screen',
ui_elements: [],
text_snippets: [],
risk_flags: [],
timestamp: new Date().toISOString()
})
} finally {
setIsCapturing(false)
}
}
const handleScreenCapture = async (captureData) => {
setIsCapturing(true)
try {
// Send the captured image to backend for analysis
const response = await axios.post(`${API_BASE}/analyze`, {
image_data: captureData.dataUrl,
include_thumbnail: includeThumbnail,
width: captureData.width,
height: captureData.height,
timestamp: captureData.timestamp
})
// Check if the response indicates an error
if (response.data.risk_flags && response.data.risk_flags.includes('ANALYSIS_ERROR')) {
// Handle model error gracefully
setAnalysis({
summary: 'Model is loading or experiencing memory constraints. The system is configured correctly but requires more RAM for full operation.',
ui_elements: [],
text_snippets: [],
risk_flags: [], // Don't show error as a risk flag
timestamp: response.data.timestamp || new Date().toISOString(),
model_info: response.data.model_info
})
} else {
setAnalysis(response.data)
}
} catch (error) {
console.error('Analysis error:', error)
setAnalysis({
summary: 'Unable to connect to analysis service. Please ensure the backend is running.',
ui_elements: [],
text_snippets: [],
risk_flags: [],
timestamp: new Date().toISOString()
})
} finally {
setIsCapturing(false)
}
}
const handleCaptureError = (error) => {
console.error('Screen capture error:', error)
setAnalysis({
summary: error.userMessage || 'Screen capture failed',
ui_elements: [],
text_snippets: [],
risk_flags: ['CAPTURE_ERROR'],
error_details: error.technicalDetails,
timestamp: new Date().toISOString()
})
}
const runDemo = async () => {
setDemoStatus('Starting demo...')
try {
const response = await axios.post(`${API_BASE}/demo`, {
url: 'https://example.com',
text_to_type: 'test'
})
setDemoStatus(`Demo ${response.data.status}`)
setTimeout(() => {
setDemoStatus('')
}, 5000)
} catch (error) {
console.error('Demo error:', error)
setDemoStatus('Demo failed')
}
}
const exportLogs = async () => {
try {
const response = await axios.get(`${API_BASE}/export`, {
responseType: 'blob'
})
const url = window.URL.createObjectURL(new Blob([response.data]))
const link = document.createElement('a')
link.href = url
link.setAttribute('download', `screen_observer_export_${Date.now()}.zip`)
document.body.appendChild(link)
link.click()
link.remove()
window.URL.revokeObjectURL(url)
} catch (error) {
console.error('Export error:', error)
}
}
return (
<div className="app">
<header className="app-header">
<h1>FastVLM-7B Screen Observer</h1>
<div className="status">
<span className="status-dot"></span>
<span>Connected to API</span>
</div>
</header>
<div className="main-container">
<div className="control-panel">
<h2>Controls</h2>
<div className="control-section">
<h3>Capture Settings</h3>
<div className="control-group">
<label>
<input
type="checkbox"
checked={includeThumbnail}
onChange={(e) => setIncludeThumbnail(e.target.checked)}
/>
Include Thumbnail in Logs
</label>
</div>
<div className="control-group">
<label>
<input
type="checkbox"
checked={autoCapture}
onChange={(e) => setAutoCapture(e.target.checked)}
/>
Auto Capture
</label>
{autoCapture && (
<div className="interval-control">
<label>
Interval (ms):
<input
type="number"
value={captureInterval}
onChange={(e) => setCaptureInterval(parseInt(e.target.value) || 5000)}
min="1000"
step="1000"
/>
</label>
</div>
)}
</div>
</div>
<div className="control-section">
<h3>Screen Capture</h3>
<ScreenCapture
onCapture={handleScreenCapture}
onError={handleCaptureError}
/>
</div>
<div className="control-section">
<h3>Legacy Capture (Server-side)</h3>
<button
onClick={captureScreen}
disabled={isCapturing}
className="btn btn-secondary"
title="Uses server-side screen capture (captures server's screen, not yours)"
>
{isCapturing ? 'Capturing...' : 'Server Capture'}
</button>
<button
onClick={runDemo}
className="btn btn-secondary"
>
Run Demo
</button>
<button
onClick={exportLogs}
className="btn btn-tertiary"
>
Export Logs
</button>
{demoStatus && (
<div className="demo-status">{demoStatus}</div>
)}
</div>
</div>
<div className="analysis-panel">
<h2>Analysis Results</h2>
{analysis ? (
<div className="analysis-content">
<div className="analysis-section">
<h3>Summary</h3>
<p>{analysis.summary}</p>
<div className="timestamp">{analysis.timestamp}</div>
</div>
<div className="analysis-section">
<h3>UI Elements ({analysis.ui_elements.length})</h3>
<ul className="element-list">
{analysis.ui_elements.map((el, idx) => (
<li key={idx}>
<strong>{el.type}:</strong> {el.text || 'N/A'}
{el.position && (
<span className="position"> ({el.position.x}, {el.position.y})</span>
)}
</li>
))}
</ul>
</div>
<div className="analysis-section">
<h3>Text Snippets ({analysis.text_snippets.length})</h3>
<ul className="snippet-list">
{analysis.text_snippets.map((text, idx) => (
<li key={idx}>{text}</li>
))}
</ul>
</div>
{analysis.risk_flags.length > 0 && (
<div className="analysis-section risk-section">
<h3>Risk Flags</h3>
<ul className="risk-list">
{analysis.risk_flags.map((flag, idx) => (
<li key={idx} className="risk-flag">{flag}</li>
))}
</ul>
</div>
)}
</div>
) : (
<div className="no-analysis">
No analysis yet. Click "Capture Screen" to start.
</div>
)}
</div>
<div className="logs-panel">
<h2>Logs ({logs.length})</h2>
<div className="logs-container">
{logs.length > 0 ? (
logs.slice().reverse().map((log, idx) => (
<div key={idx} className={`log-entry log-${log.type}`}>
<span className="log-timestamp">{log.timestamp}</span>
<span className="log-type">{log.type}</span>
{log.frame_id && <span className="log-frame">Frame: {log.frame_id}</span>}
</div>
))
) : (
<div className="no-logs">No logs yet...</div>
)}
</div>
</div>
</div>
</div>
)
}
export default App
|