Spaces:
Sleeping
Sleeping
Commit
Β·
519bc7d
1
Parent(s):
f217250
everything is is working perfectly
Browse files- app.py +160 -40
- drift_detector.sqlite3 +0 -0
app.py
CHANGED
@@ -6,6 +6,11 @@ import subprocess
|
|
6 |
import time
|
7 |
import signal
|
8 |
import sys
|
|
|
|
|
|
|
|
|
|
|
9 |
import threading
|
10 |
import concurrent.futures
|
11 |
# Add these imports at the top of your Gradio file
|
@@ -388,6 +393,29 @@ def save_new_model(model_name, selected_llm, original_prompt, enhanced_prompt, c
|
|
388 |
]
|
389 |
|
390 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
391 |
# Replace the chatbot_response function in your Gradio file with this:
|
392 |
|
393 |
def chatbot_response(message, history, dropdown_value):
|
@@ -471,20 +499,20 @@ def test_llm_connection():
|
|
471 |
|
472 |
|
473 |
# Add this to your interface initialization to test LLM on startup:
|
474 |
-
|
475 |
-
|
|
|
476 |
global current_model_mapping
|
477 |
|
478 |
-
# Test
|
479 |
-
|
480 |
-
print(f"π
|
481 |
|
482 |
try:
|
483 |
models = get_models_from_db()
|
484 |
formatted_items, model_mapping = format_dropdown_items(models)
|
485 |
current_model_mapping = model_mapping
|
486 |
|
487 |
-
# Safe initialization
|
488 |
if formatted_items:
|
489 |
dropdown_value = formatted_items[0]
|
490 |
first_model_name = extract_model_name_from_dropdown(dropdown_value, model_mapping)
|
@@ -495,10 +523,10 @@ def initialize_interface():
|
|
495 |
dropdown_update = gr.update(choices=[], value=None)
|
496 |
|
497 |
return (
|
498 |
-
dropdown_update,
|
499 |
-
"",
|
500 |
-
first_model_name,
|
501 |
-
first_model_name
|
502 |
)
|
503 |
except Exception as e:
|
504 |
print(f"β Error initializing interface: {e}")
|
@@ -509,36 +537,91 @@ def initialize_interface():
|
|
509 |
""
|
510 |
)
|
511 |
|
|
|
512 |
|
513 |
def calculate_drift(dropdown_value):
|
514 |
-
"""Calculate drift for model -
|
515 |
if not dropdown_value:
|
516 |
return "β Please select a model first"
|
517 |
|
518 |
try:
|
519 |
model_name = extract_model_name_from_dropdown(dropdown_value, current_model_mapping)
|
520 |
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
525 |
|
526 |
-
return f"Drift analysis for {model_name}:\nDrift Score: {drift_score}/100\n{alert}"
|
527 |
except Exception as e:
|
528 |
print(f"β Error calculating drift: {e}")
|
529 |
-
return "β Error calculating drift"
|
530 |
|
531 |
|
532 |
def create_drift_chart(drift_history):
|
533 |
-
"""Create drift chart"""
|
534 |
try:
|
535 |
if not drift_history:
|
536 |
-
#
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
542 |
|
543 |
fig = go.Figure()
|
544 |
fig.add_trace(go.Scatter(
|
@@ -547,41 +630,78 @@ def create_drift_chart(drift_history):
|
|
547 |
mode='lines+markers',
|
548 |
name='Drift Score',
|
549 |
line=dict(color='#ff6b6b', width=3),
|
550 |
-
marker=dict(size=8, color='#ff6b6b')
|
|
|
551 |
))
|
552 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
553 |
fig.update_layout(
|
554 |
-
title='Model Drift Over Time',
|
555 |
xaxis_title='Date',
|
556 |
-
yaxis_title='Drift Score',
|
557 |
template='plotly_white',
|
558 |
height=400,
|
559 |
-
showlegend=True
|
|
|
560 |
)
|
561 |
|
562 |
return fig
|
|
|
563 |
except Exception as e:
|
564 |
print(f"β Error creating drift chart: {e}")
|
565 |
-
|
566 |
-
|
567 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
568 |
def refresh_drift_history(dropdown_value):
|
569 |
-
"""Refresh drift history"""
|
570 |
if not dropdown_value:
|
571 |
return [], gr.update(value=None)
|
572 |
|
573 |
try:
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
582 |
|
583 |
chart = create_drift_chart(history)
|
584 |
return history, chart
|
|
|
585 |
except Exception as e:
|
586 |
print(f"β Error refreshing drift history: {e}")
|
587 |
return [], gr.update(value=None)
|
|
|
6 |
import time
|
7 |
import signal
|
8 |
import sys
|
9 |
+
# Add these imports at the top of your Gradio file
|
10 |
+
from database_module.mcp_tools import (
|
11 |
+
get_drift_history_handler,
|
12 |
+
calculate_drift_handler
|
13 |
+
)
|
14 |
import threading
|
15 |
import concurrent.futures
|
16 |
# Add these imports at the top of your Gradio file
|
|
|
393 |
]
|
394 |
|
395 |
|
396 |
+
# Also add this function to help debug database connection:
|
397 |
+
def test_database_connection():
|
398 |
+
"""Test if database connection is working and has data"""
|
399 |
+
try:
|
400 |
+
if not DATABASE_AVAILABLE:
|
401 |
+
return "β οΈ Database not available - running in demo mode"
|
402 |
+
|
403 |
+
# Test getting models
|
404 |
+
models = get_all_models_handler({})
|
405 |
+
model_count = len(models) if models else 0
|
406 |
+
|
407 |
+
# Test getting drift history for first model if available
|
408 |
+
drift_info = ""
|
409 |
+
if models and len(models) > 0:
|
410 |
+
first_model = models[0]["name"]
|
411 |
+
drift_history = get_drift_history_handler({"model_name": first_model})
|
412 |
+
drift_count = len(drift_history) if drift_history else 0
|
413 |
+
drift_info = f"\nπ Drift records for '{first_model}': {drift_count}"
|
414 |
+
|
415 |
+
return f"β
Database connected\nπ Total models: {model_count}{drift_info}"
|
416 |
+
|
417 |
+
except Exception as e:
|
418 |
+
return f"β Database test failed: {e}"
|
419 |
# Replace the chatbot_response function in your Gradio file with this:
|
420 |
|
421 |
def chatbot_response(message, history, dropdown_value):
|
|
|
499 |
|
500 |
|
501 |
# Add this to your interface initialization to test LLM on startup:
|
502 |
+
# Add this to your interface initialization to show database status
|
503 |
+
def initialize_interface_with_debug():
|
504 |
+
"""Initialize interface with database debug info"""
|
505 |
global current_model_mapping
|
506 |
|
507 |
+
# Test database connection
|
508 |
+
db_status = test_database_connection()
|
509 |
+
print(f"π Database Status: {db_status}")
|
510 |
|
511 |
try:
|
512 |
models = get_models_from_db()
|
513 |
formatted_items, model_mapping = format_dropdown_items(models)
|
514 |
current_model_mapping = model_mapping
|
515 |
|
|
|
516 |
if formatted_items:
|
517 |
dropdown_value = formatted_items[0]
|
518 |
first_model_name = extract_model_name_from_dropdown(dropdown_value, model_mapping)
|
|
|
523 |
dropdown_update = gr.update(choices=[], value=None)
|
524 |
|
525 |
return (
|
526 |
+
dropdown_update,
|
527 |
+
"",
|
528 |
+
first_model_name,
|
529 |
+
first_model_name
|
530 |
)
|
531 |
except Exception as e:
|
532 |
print(f"β Error initializing interface: {e}")
|
|
|
537 |
""
|
538 |
)
|
539 |
|
540 |
+
# Replace your existing functions with these corrected versions:
|
541 |
|
542 |
def calculate_drift(dropdown_value):
|
543 |
+
"""Calculate drift for model - using actual database"""
|
544 |
if not dropdown_value:
|
545 |
return "β Please select a model first"
|
546 |
|
547 |
try:
|
548 |
model_name = extract_model_name_from_dropdown(dropdown_value, current_model_mapping)
|
549 |
|
550 |
+
if not DATABASE_AVAILABLE:
|
551 |
+
# Fallback for demo mode
|
552 |
+
import random
|
553 |
+
drift_score = random.randint(10, 80)
|
554 |
+
alert = "π¨ Significant drift detected!" if drift_score > 50 else "β
Drift within acceptable range"
|
555 |
+
return f"Drift analysis for {model_name}:\nDrift Score: {drift_score}/100\n{alert}"
|
556 |
+
|
557 |
+
# Use actual database function
|
558 |
+
result = calculate_drift_handler({"model_name": model_name})
|
559 |
+
|
560 |
+
if "drift_score" in result:
|
561 |
+
drift_score = result["drift_score"]
|
562 |
+
# Convert to percentage if it's a decimal
|
563 |
+
if isinstance(drift_score, float) and drift_score <= 1.0:
|
564 |
+
drift_score = int(drift_score * 100)
|
565 |
+
|
566 |
+
alert = "π¨ Significant drift detected!" if drift_score > 50 else "β
Drift within acceptable range"
|
567 |
+
return f"Drift analysis for {model_name}:\nDrift Score: {drift_score}/100\n{alert}\n\n{result.get('message', '')}"
|
568 |
+
else:
|
569 |
+
return f"β Error calculating drift: {result.get('message', 'Unknown error')}"
|
570 |
|
|
|
571 |
except Exception as e:
|
572 |
print(f"β Error calculating drift: {e}")
|
573 |
+
return f"β Error calculating drift: {str(e)}"
|
574 |
|
575 |
|
576 |
def create_drift_chart(drift_history):
|
577 |
+
"""Create drift chart from actual data"""
|
578 |
try:
|
579 |
if not drift_history:
|
580 |
+
# Empty chart if no data
|
581 |
+
fig = go.Figure()
|
582 |
+
fig.add_annotation(
|
583 |
+
text="No drift data available",
|
584 |
+
xref="paper", yref="paper",
|
585 |
+
x=0.5, y=0.5,
|
586 |
+
showarrow=False,
|
587 |
+
font=dict(size=16)
|
588 |
+
)
|
589 |
+
fig.update_layout(
|
590 |
+
title='Model Drift Over Time - No Data',
|
591 |
+
template='plotly_white',
|
592 |
+
height=400
|
593 |
+
)
|
594 |
+
return fig
|
595 |
+
|
596 |
+
# Extract dates and scores from actual data
|
597 |
+
dates = []
|
598 |
+
scores = []
|
599 |
+
|
600 |
+
for entry in drift_history:
|
601 |
+
# Handle different date formats
|
602 |
+
date_str = entry.get("date", "")
|
603 |
+
if isinstance(date_str, str):
|
604 |
+
# Parse ISO format or other formats
|
605 |
+
try:
|
606 |
+
from datetime import datetime
|
607 |
+
if "T" in date_str:
|
608 |
+
date_obj = datetime.fromisoformat(date_str.replace("Z", "+00:00"))
|
609 |
+
else:
|
610 |
+
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
611 |
+
dates.append(date_obj.strftime("%Y-%m-%d"))
|
612 |
+
except:
|
613 |
+
dates.append(date_str)
|
614 |
+
else:
|
615 |
+
dates.append(str(date_str))
|
616 |
+
|
617 |
+
# Handle drift score
|
618 |
+
score = entry.get("drift_score", 0)
|
619 |
+
if isinstance(score, str):
|
620 |
+
try:
|
621 |
+
score = float(score)
|
622 |
+
except:
|
623 |
+
score = 0
|
624 |
+
scores.append(score)
|
625 |
|
626 |
fig = go.Figure()
|
627 |
fig.add_trace(go.Scatter(
|
|
|
630 |
mode='lines+markers',
|
631 |
name='Drift Score',
|
632 |
line=dict(color='#ff6b6b', width=3),
|
633 |
+
marker=dict(size=8, color='#ff6b6b'),
|
634 |
+
hovertemplate='<b>Date:</b> %{x}<br><b>Drift Score:</b> %{y}%<extra></extra>'
|
635 |
))
|
636 |
|
637 |
+
# Add threshold line at 50%
|
638 |
+
fig.add_hline(
|
639 |
+
y=50,
|
640 |
+
line_dash="dash",
|
641 |
+
line_color="orange",
|
642 |
+
annotation_text="Drift Threshold (50%)"
|
643 |
+
)
|
644 |
+
|
645 |
fig.update_layout(
|
646 |
+
title=f'Model Drift Over Time ({len(drift_history)} records)',
|
647 |
xaxis_title='Date',
|
648 |
+
yaxis_title='Drift Score (%)',
|
649 |
template='plotly_white',
|
650 |
height=400,
|
651 |
+
showlegend=True,
|
652 |
+
yaxis=dict(range=[0, 100]) # Set Y-axis range from 0 to 100%
|
653 |
)
|
654 |
|
655 |
return fig
|
656 |
+
|
657 |
except Exception as e:
|
658 |
print(f"β Error creating drift chart: {e}")
|
659 |
+
# Return empty chart on error
|
660 |
+
fig = go.Figure()
|
661 |
+
fig.add_annotation(
|
662 |
+
text=f"Error creating chart: {str(e)}",
|
663 |
+
xref="paper", yref="paper",
|
664 |
+
x=0.5, y=0.5,
|
665 |
+
showarrow=False,
|
666 |
+
font=dict(size=14, color="red")
|
667 |
+
)
|
668 |
+
fig.update_layout(
|
669 |
+
title='Error Creating Drift Chart',
|
670 |
+
template='plotly_white',
|
671 |
+
height=400
|
672 |
+
)
|
673 |
+
return fig
|
674 |
def refresh_drift_history(dropdown_value):
|
675 |
+
"""Refresh drift history - using actual database"""
|
676 |
if not dropdown_value:
|
677 |
return [], gr.update(value=None)
|
678 |
|
679 |
try:
|
680 |
+
model_name = extract_model_name_from_dropdown(dropdown_value, current_model_mapping)
|
681 |
+
|
682 |
+
if not DATABASE_AVAILABLE:
|
683 |
+
# Mock data for demo mode only
|
684 |
+
history = [
|
685 |
+
{"date": "2024-01-01", "drift_score": 25},
|
686 |
+
{"date": "2024-01-02", "drift_score": 30},
|
687 |
+
{"date": "2024-01-03", "drift_score": 45},
|
688 |
+
{"date": "2024-01-04", "drift_score": 35},
|
689 |
+
{"date": "2024-01-05", "drift_score": 40}
|
690 |
+
]
|
691 |
+
else:
|
692 |
+
# Get actual drift history from database
|
693 |
+
history_result = get_drift_history_handler({"model_name": model_name})
|
694 |
+
|
695 |
+
if isinstance(history_result, list) and history_result:
|
696 |
+
history = history_result
|
697 |
+
print(f"β
Retrieved {len(history)} drift records for {model_name}")
|
698 |
+
else:
|
699 |
+
history = []
|
700 |
+
print(f"β οΈ No drift history found for {model_name}")
|
701 |
|
702 |
chart = create_drift_chart(history)
|
703 |
return history, chart
|
704 |
+
|
705 |
except Exception as e:
|
706 |
print(f"β Error refreshing drift history: {e}")
|
707 |
return [], gr.update(value=None)
|
drift_detector.sqlite3
CHANGED
Binary files a/drift_detector.sqlite3 and b/drift_detector.sqlite3 differ
|
|