Spaces:
Sleeping
Sleeping
| """ | |
| π NAVADA 2.0 - Advanced AI Computer Vision Application (Lite Version) | |
| Streamlit Version for Hugging Face Spaces Deployment | |
| Enhanced Edition by Lee Akpareva | AI Consultant & Computer Vision Specialist | |
| """ | |
| import streamlit as st | |
| import time | |
| from datetime import datetime | |
| import plotly.graph_objects as go | |
| import plotly.express as px | |
| from PIL import Image | |
| import numpy as np | |
| import os | |
| # Configure Streamlit page (MUST be first!) | |
| st.set_page_config( | |
| page_title="π NAVADA 2.0 - AI Computer Vision", | |
| page_icon="π", | |
| layout="wide", | |
| initial_sidebar_state="expanded" | |
| ) | |
| # Backend imports - Lite version (no face recognition) | |
| try: | |
| from backend.yolo import detect_objects | |
| from backend.openai_client import explain_detection | |
| except ImportError as e: | |
| st.error(f"β οΈ Import error: {e}") | |
| st.error("π¦ Please install dependencies: pip install -r requirements.txt") | |
| st.stop() | |
| # Custom CSS for enhanced styling | |
| st.markdown(""" | |
| <style> | |
| .main-header { | |
| background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); | |
| padding: 2rem; | |
| border-radius: 10px; | |
| color: white; | |
| text-align: center; | |
| margin-bottom: 2rem; | |
| } | |
| .feature-card { | |
| background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); | |
| padding: 1.5rem; | |
| border-radius: 10px; | |
| color: white; | |
| margin: 1rem 0; | |
| } | |
| .stats-card { | |
| background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); | |
| padding: 1rem; | |
| border-radius: 8px; | |
| color: white; | |
| text-align: center; | |
| margin: 0.5rem; | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| def create_detection_chart(detected_objects): | |
| """Create an interactive chart showing detection statistics""" | |
| # Count object types | |
| object_counts = {} | |
| for obj in detected_objects: | |
| object_counts[obj] = object_counts.get(obj, 0) + 1 | |
| if not object_counts: | |
| # Create empty chart | |
| fig = go.Figure() | |
| fig.add_annotation( | |
| text="No objects detected", | |
| xref="paper", yref="paper", | |
| x=0.5, y=0.5, showarrow=False, | |
| font=dict(size=20, color="gray") | |
| ) | |
| fig.update_layout( | |
| height=300, | |
| showlegend=False, | |
| paper_bgcolor='rgba(0,0,0,0)', | |
| plot_bgcolor='rgba(0,0,0,0)' | |
| ) | |
| return fig | |
| # Create bar chart | |
| objects = list(object_counts.keys()) | |
| counts = list(object_counts.values()) | |
| fig = go.Figure(data=[ | |
| go.Bar( | |
| x=objects, | |
| y=counts, | |
| marker_color='rgba(50, 171, 96, 0.6)', | |
| marker_line_color='rgba(50, 171, 96, 1.0)', | |
| marker_line_width=2, | |
| text=counts, | |
| textposition='auto' | |
| ) | |
| ]) | |
| fig.update_layout( | |
| title="Detected Objects", | |
| xaxis_title="Object Type", | |
| yaxis_title="Count", | |
| height=400, | |
| showlegend=False, | |
| paper_bgcolor='rgba(0,0,0,0)', | |
| plot_bgcolor='rgba(0,0,0,0)' | |
| ) | |
| return fig | |
| def main(): | |
| # Main header | |
| st.markdown(""" | |
| <div class="main-header"> | |
| <h1>π NAVADA 2.0 - Advanced AI Computer Vision</h1> | |
| <p><strong>Lite Version - Object Detection & AI Analysis</strong></p> | |
| <p>Built with YOLOv8 β’ OpenAI β’ Streamlit</p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Sidebar | |
| with st.sidebar: | |
| st.markdown("### π― Detection Settings") | |
| # Detection confidence threshold | |
| confidence = st.slider( | |
| "Detection Confidence", | |
| min_value=0.1, | |
| max_value=1.0, | |
| value=0.5, | |
| step=0.05, | |
| help="Minimum confidence for object detection" | |
| ) | |
| st.markdown("### π Features") | |
| st.markdown(""" | |
| - π― **Object Detection**: YOLOv8 powered | |
| - π€ **AI Explanations**: OpenAI integration | |
| - π **Interactive Charts**: Real-time analytics | |
| - π¨ **Visual Results**: Annotated images | |
| """) | |
| st.markdown("### βΉοΈ About") | |
| st.markdown(""" | |
| This is the **Lite Version** optimized for Hugging Face Spaces. | |
| **Created by:** Lee Akpareva | |
| **AI Consultant & Computer Vision Specialist** | |
| """) | |
| # Main content | |
| col1, col2 = st.columns([2, 1]) | |
| with col1: | |
| st.markdown("### πΈ Upload Image for Analysis") | |
| uploaded_file = st.file_uploader( | |
| "Choose an image...", | |
| type=['png', 'jpg', 'jpeg'], | |
| help="Upload an image to detect objects and get AI analysis" | |
| ) | |
| if uploaded_file is not None: | |
| # Display uploaded image | |
| image = Image.open(uploaded_file) | |
| st.image(image, caption="Uploaded Image", use_column_width=True) | |
| # Analysis button | |
| if st.button("π Analyze Image", type="primary"): | |
| with st.spinner("π Detecting objects..."): | |
| # Perform object detection | |
| results = detect_objects(image, confidence_threshold=confidence) | |
| if results and len(results['detections']) > 0: | |
| # Extract detected objects | |
| detected_objects = [det['class'] for det in results['detections']] | |
| # Display results | |
| st.success(f"β Detected {len(detected_objects)} objects!") | |
| # Show annotated image | |
| st.markdown("### π― Detection Results") | |
| if 'annotated_image' in results: | |
| st.image(results['annotated_image'], caption="Detected Objects", use_column_width=True) | |
| # Show detection details | |
| st.markdown("### π Detected Objects") | |
| for i, detection in enumerate(results['detections']): | |
| col_a, col_b, col_c = st.columns(3) | |
| with col_a: | |
| st.metric("Object", detection['class']) | |
| with col_b: | |
| st.metric("Confidence", f"{detection['confidence']:.2%}") | |
| with col_c: | |
| st.metric("Count", f"#{i+1}") | |
| # AI Explanation | |
| if os.getenv("OPENAI_API_KEY"): | |
| st.markdown("### π€ AI Analysis") | |
| with st.spinner("π§ Generating AI explanation..."): | |
| try: | |
| explanation = explain_detection(detected_objects) | |
| st.markdown(f"**AI Insight:** {explanation}") | |
| except Exception as e: | |
| st.warning(f"AI analysis unavailable: {str(e)}") | |
| else: | |
| st.warning("π Add OPENAI_API_KEY in settings for AI explanations") | |
| else: | |
| st.warning("β No objects detected. Try adjusting the confidence threshold.") | |
| with col2: | |
| st.markdown("### π Detection Statistics") | |
| # Sample chart (will be updated with real data) | |
| sample_data = { | |
| 'Object': ['Person', 'Car', 'Dog', 'Cat'], | |
| 'Count': [3, 2, 1, 1] | |
| } | |
| fig = px.bar( | |
| sample_data, | |
| x='Object', | |
| y='Count', | |
| title="Sample Detection Results", | |
| color='Count', | |
| color_continuous_scale='Viridis' | |
| ) | |
| fig.update_layout(height=300) | |
| st.plotly_chart(fig, use_container_width=True) | |
| # Feature highlights | |
| st.markdown("### β¨ Key Features") | |
| features = [ | |
| ("π―", "Object Detection", "Advanced YOLOv8 model"), | |
| ("π€", "AI Analysis", "OpenAI explanations"), | |
| ("π", "Real-time Charts", "Interactive visualizations"), | |
| ("π", "Fast Processing", "Optimized for speed") | |
| ] | |
| for icon, title, desc in features: | |
| st.markdown(f""" | |
| <div style="display: flex; align-items: center; margin: 1rem 0; padding: 0.5rem; background: #f0f2f6; border-radius: 5px;"> | |
| <div style="font-size: 1.5rem; margin-right: 1rem;">{icon}</div> | |
| <div> | |
| <strong>{title}</strong><br> | |
| <small>{desc}</small> | |
| </div> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Footer | |
| st.markdown("---") | |
| st.markdown(""" | |
| <div style="text-align: center; padding: 2rem; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 10px; color: white; margin-top: 2rem;"> | |
| <h3>π Experience Advanced Computer Vision</h3> | |
| <p><strong>β Built by Lee Akpareva | AI Consultant & Computer Vision Specialist β</strong></p> | |
| <p>π <em>Powered by YOLOv8 β’ OpenAI β’ Streamlit</em></p> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| if __name__ == "__main__": | |
| main() |