Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -314,6 +314,50 @@ def follow_up_question(question, bird_name):
|
|
| 314 |
except Exception as e:
|
| 315 |
return f"Error fetching information: {str(e)}"
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
# Create the Gradio interface
|
| 318 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 319 |
gr.Markdown("# Bird Species Identification for Researchers")
|
|
@@ -321,8 +365,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 321 |
|
| 322 |
# Store the current bird for context
|
| 323 |
current_bird = gr.State("")
|
| 324 |
-
|
| 325 |
-
|
|
|
|
| 326 |
|
| 327 |
# Main identification section
|
| 328 |
with gr.Row():
|
|
@@ -330,8 +375,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 330 |
input_image = gr.Image(
|
| 331 |
type="pil",
|
| 332 |
label="Upload Bird Image",
|
| 333 |
-
|
| 334 |
-
webcam_options={"facingMode": "environment"} # Default to back camera
|
| 335 |
)
|
| 336 |
camera_toggle = gr.Button("Switch Camera")
|
| 337 |
submit_btn = gr.Button("Identify Bird", variant="primary")
|
|
@@ -390,11 +434,6 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 390 |
def clear_conversation_history():
|
| 391 |
return ""
|
| 392 |
|
| 393 |
-
def toggle_camera_mode(current_mode):
|
| 394 |
-
# Switch between "user" (front) and "environment" (back) camera
|
| 395 |
-
new_mode = "user" if current_mode == "environment" else "environment"
|
| 396 |
-
return new_mode
|
| 397 |
-
|
| 398 |
submit_btn.click(
|
| 399 |
process_image,
|
| 400 |
inputs=[input_image],
|
|
@@ -415,38 +454,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 415 |
outputs=[conversation_history]
|
| 416 |
)
|
| 417 |
|
| 418 |
-
#
|
| 419 |
-
camera_toggle.click(
|
| 420 |
-
toggle_camera_mode,
|
| 421 |
-
inputs=[camera_mode],
|
| 422 |
-
outputs=[camera_mode],
|
| 423 |
-
_js="""
|
| 424 |
-
function(mode) {
|
| 425 |
-
// Get the webcam element
|
| 426 |
-
const webcamEl = document.querySelector('.webcam video');
|
| 427 |
-
if (webcamEl) {
|
| 428 |
-
// Determine new facing mode (opposite of current)
|
| 429 |
-
const newFacingMode = mode === "environment" ? "user" : "environment";
|
| 430 |
-
|
| 431 |
-
// Stop current stream
|
| 432 |
-
if (webcamEl.srcObject) {
|
| 433 |
-
webcamEl.srcObject.getTracks().forEach(track => track.stop());
|
| 434 |
-
}
|
| 435 |
-
|
| 436 |
-
// Start new stream with different camera
|
| 437 |
-
navigator.mediaDevices.getUserMedia({
|
| 438 |
-
video: { facingMode: newFacingMode }
|
| 439 |
-
}).then(stream => {
|
| 440 |
-
webcamEl.srcObject = stream;
|
| 441 |
-
}).catch(err => {
|
| 442 |
-
console.error('Error switching camera:', err);
|
| 443 |
-
});
|
| 444 |
-
}
|
| 445 |
-
// Return the new mode to update the state
|
| 446 |
-
return newFacingMode;
|
| 447 |
-
}
|
| 448 |
-
"""
|
| 449 |
-
)
|
| 450 |
|
| 451 |
# Launch the app
|
| 452 |
app.launch(share=True)
|
|
|
|
| 314 |
except Exception as e:
|
| 315 |
return f"Error fetching information: {str(e)}"
|
| 316 |
|
| 317 |
+
# Function to add custom JavaScript for camera switching
|
| 318 |
+
def create_camera_switch_js():
|
| 319 |
+
return """
|
| 320 |
+
function switchCamera() {
|
| 321 |
+
// Get all video elements
|
| 322 |
+
const videoElements = document.querySelectorAll('video');
|
| 323 |
+
|
| 324 |
+
if (videoElements.length > 0) {
|
| 325 |
+
const video = videoElements[0];
|
| 326 |
+
|
| 327 |
+
// Stop all tracks on the current stream
|
| 328 |
+
if (video.srcObject) {
|
| 329 |
+
const tracks = video.srcObject.getTracks();
|
| 330 |
+
tracks.forEach(track => track.stop());
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
// Try to determine current camera type
|
| 334 |
+
let usingFrontCamera = true;
|
| 335 |
+
if (window.currentCamera === 'environment') {
|
| 336 |
+
usingFrontCamera = false;
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
// Set the new camera type
|
| 340 |
+
const facingMode = usingFrontCamera ? 'environment' : 'user';
|
| 341 |
+
window.currentCamera = facingMode;
|
| 342 |
+
|
| 343 |
+
// Request new video stream with the different camera
|
| 344 |
+
navigator.mediaDevices.getUserMedia({
|
| 345 |
+
video: { facingMode: facingMode }
|
| 346 |
+
})
|
| 347 |
+
.then(function(stream) {
|
| 348 |
+
video.srcObject = stream;
|
| 349 |
+
// Play the video
|
| 350 |
+
video.play();
|
| 351 |
+
})
|
| 352 |
+
.catch(function(err) {
|
| 353 |
+
console.error('Error accessing the camera: ' + err);
|
| 354 |
+
});
|
| 355 |
+
} else {
|
| 356 |
+
console.warn('No video elements found.');
|
| 357 |
+
}
|
| 358 |
+
}
|
| 359 |
+
"""
|
| 360 |
+
|
| 361 |
# Create the Gradio interface
|
| 362 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 363 |
gr.Markdown("# Bird Species Identification for Researchers")
|
|
|
|
| 365 |
|
| 366 |
# Store the current bird for context
|
| 367 |
current_bird = gr.State("")
|
| 368 |
+
|
| 369 |
+
# Add custom JavaScript for camera switching
|
| 370 |
+
app.load(None, None, None, _js=create_camera_switch_js())
|
| 371 |
|
| 372 |
# Main identification section
|
| 373 |
with gr.Row():
|
|
|
|
| 375 |
input_image = gr.Image(
|
| 376 |
type="pil",
|
| 377 |
label="Upload Bird Image",
|
| 378 |
+
source="webcam", # Enable webcam
|
|
|
|
| 379 |
)
|
| 380 |
camera_toggle = gr.Button("Switch Camera")
|
| 381 |
submit_btn = gr.Button("Identify Bird", variant="primary")
|
|
|
|
| 434 |
def clear_conversation_history():
|
| 435 |
return ""
|
| 436 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
submit_btn.click(
|
| 438 |
process_image,
|
| 439 |
inputs=[input_image],
|
|
|
|
| 454 |
outputs=[conversation_history]
|
| 455 |
)
|
| 456 |
|
| 457 |
+
# Call the JavaScript function when the camera toggle button is clicked
|
| 458 |
+
camera_toggle.click(None, None, None, _js="switchCamera")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
|
| 460 |
# Launch the app
|
| 461 |
app.launch(share=True)
|