rm-rf-humans
commited on
Commit
Β·
778ea45
1
Parent(s):
300b1e7
Add application file
Browse files- .gradio/flagged/dataset1.csv +2 -0
- .gradio/flagged/dataset2.csv +3 -0
- .hfignore +1 -0
- app.py +750 -4
.gradio/flagged/dataset1.csv
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name,output,timestamp
|
| 2 |
+
hey,,2025-06-06 21:41:55.526871
|
.gradio/flagged/dataset2.csv
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Prompt,Video,timestamp
|
| 2 |
+
neural network,,2025-06-06 22:10:14.512553
|
| 3 |
+
neural network,,2025-06-06 22:15:13.380497
|
.hfignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.gradio/flagges
|
app.py
CHANGED
|
@@ -1,8 +1,754 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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| 6 |
+
import google.generativeai as genai
|
| 7 |
+
from anthropic import Anthropic
|
| 8 |
+
import numpy as np
|
| 9 |
+
import plotly.graph_objects as go
|
| 10 |
+
import plotly.express as px
|
| 11 |
+
from plotly.subplots import make_subplots
|
| 12 |
+
import pandas as pd
|
| 13 |
+
import base64
|
| 14 |
+
from io import BytesIO
|
| 15 |
+
import json
|
| 16 |
+
import re
|
| 17 |
+
import traceback
|
| 18 |
+
import uuid
|
| 19 |
+
import onnx
|
| 20 |
+
import onnxruntime as ort
|
| 21 |
+
from onnx import helper, numpy_helper
|
| 22 |
+
import networkx as nx
|
| 23 |
+
from collections import defaultdict, Counter
|
| 24 |
|
| 25 |
+
# --- 1. INITIALIZATION & API KEY SETUP ---
|
| 26 |
+
load_dotenv()
|
| 27 |
|
| 28 |
+
# Securely get API keys from environment variables
|
| 29 |
+
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
|
| 30 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 31 |
+
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY") or os.getenv("GEMINI_API_KEY")
|
| 32 |
+
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
|
| 33 |
|
| 34 |
+
# Initialize the LLM clients
|
| 35 |
+
anthropic_client = None
|
| 36 |
+
openai_client = None
|
| 37 |
+
mistral_client = None
|
| 38 |
+
|
| 39 |
+
try:
|
| 40 |
+
if ANTHROPIC_API_KEY:
|
| 41 |
+
anthropic_client = Anthropic(api_key=ANTHROPIC_API_KEY)
|
| 42 |
+
if OPENAI_API_KEY:
|
| 43 |
+
openai_client = openai.OpenAI(api_key=OPENAI_API_KEY)
|
| 44 |
+
if GEMINI_API_KEY:
|
| 45 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 46 |
+
if MISTRAL_API_KEY:
|
| 47 |
+
mistral_client = MistralClient(api_key=MISTRAL_API_KEY)
|
| 48 |
+
except Exception as e:
|
| 49 |
+
print(f"Error initializing clients: {e}. Please check your API keys.")
|
| 50 |
+
|
| 51 |
+
# Create a directory to store the plot images
|
| 52 |
+
os.makedirs("temp_plots", exist_ok=True)
|
| 53 |
+
|
| 54 |
+
# --- 2. ONNX MODEL ANALYSIS FUNCTIONS ---
|
| 55 |
+
|
| 56 |
+
def analyze_onnx_model(file_path: str) -> dict:
|
| 57 |
+
"""
|
| 58 |
+
Analyzes an ONNX model file and extracts comprehensive information.
|
| 59 |
+
Returns a dictionary with model structure, operators, and metadata.
|
| 60 |
+
"""
|
| 61 |
+
try:
|
| 62 |
+
# Load the ONNX model
|
| 63 |
+
model = onnx.load(file_path)
|
| 64 |
+
|
| 65 |
+
# Basic model info
|
| 66 |
+
model_info = {
|
| 67 |
+
'ir_version': model.ir_version,
|
| 68 |
+
'producer_name': model.producer_name,
|
| 69 |
+
'producer_version': model.producer_version,
|
| 70 |
+
'domain': model.domain,
|
| 71 |
+
'model_version': model.model_version,
|
| 72 |
+
'doc_string': model.doc_string
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
# Graph analysis
|
| 76 |
+
graph = model.graph
|
| 77 |
+
|
| 78 |
+
# Node analysis
|
| 79 |
+
nodes = []
|
| 80 |
+
op_types = Counter()
|
| 81 |
+
for i, node in enumerate(graph.node):
|
| 82 |
+
node_info = {
|
| 83 |
+
'index': i,
|
| 84 |
+
'op_type': node.op_type,
|
| 85 |
+
'name': node.name or f"{node.op_type}_{i}",
|
| 86 |
+
'inputs': list(node.input),
|
| 87 |
+
'outputs': list(node.output),
|
| 88 |
+
'attributes': {}
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
# Parse attributes
|
| 92 |
+
for attr in node.attribute:
|
| 93 |
+
if attr.type == onnx.AttributeProto.INT:
|
| 94 |
+
node_info['attributes'][attr.name] = attr.i
|
| 95 |
+
elif attr.type == onnx.AttributeProto.FLOAT:
|
| 96 |
+
node_info['attributes'][attr.name] = attr.f
|
| 97 |
+
elif attr.type == onnx.AttributeProto.STRING:
|
| 98 |
+
node_info['attributes'][attr.name] = attr.s.decode('utf-8')
|
| 99 |
+
elif attr.type == onnx.AttributeProto.INTS:
|
| 100 |
+
node_info['attributes'][attr.name] = list(attr.ints)
|
| 101 |
+
elif attr.type == onnx.AttributeProto.FLOATS:
|
| 102 |
+
node_info['attributes'][attr.name] = list(attr.floats)
|
| 103 |
+
|
| 104 |
+
nodes.append(node_info)
|
| 105 |
+
op_types[node.op_type] += 1
|
| 106 |
+
|
| 107 |
+
# Input/Output analysis
|
| 108 |
+
inputs = []
|
| 109 |
+
for inp in graph.input:
|
| 110 |
+
input_info = {
|
| 111 |
+
'name': inp.name,
|
| 112 |
+
'type': inp.type.tensor_type.elem_type,
|
| 113 |
+
'shape': [dim.dim_value if dim.dim_value > 0 else dim.dim_param
|
| 114 |
+
for dim in inp.type.tensor_type.shape.dim]
|
| 115 |
+
}
|
| 116 |
+
inputs.append(input_info)
|
| 117 |
+
|
| 118 |
+
outputs = []
|
| 119 |
+
for out in graph.output:
|
| 120 |
+
output_info = {
|
| 121 |
+
'name': out.name,
|
| 122 |
+
'type': out.type.tensor_type.elem_type,
|
| 123 |
+
'shape': [dim.dim_value if dim.dim_value > 0 else dim.dim_param
|
| 124 |
+
for dim in out.type.tensor_type.shape.dim]
|
| 125 |
+
}
|
| 126 |
+
outputs.append(output_info)
|
| 127 |
+
|
| 128 |
+
# Value info (intermediate tensors)
|
| 129 |
+
value_info = []
|
| 130 |
+
for val in graph.value_info:
|
| 131 |
+
val_info = {
|
| 132 |
+
'name': val.name,
|
| 133 |
+
'type': val.type.tensor_type.elem_type,
|
| 134 |
+
'shape': [dim.dim_value if dim.dim_value > 0 else dim.dim_param
|
| 135 |
+
for dim in val.type.tensor_type.shape.dim]
|
| 136 |
+
}
|
| 137 |
+
value_info.append(val_info)
|
| 138 |
+
|
| 139 |
+
# Initializers (weights/constants)
|
| 140 |
+
initializers = []
|
| 141 |
+
for init in graph.initializer:
|
| 142 |
+
init_info = {
|
| 143 |
+
'name': init.name,
|
| 144 |
+
'data_type': init.data_type,
|
| 145 |
+
'dims': list(init.dims),
|
| 146 |
+
'size': np.prod(init.dims) if init.dims else 0
|
| 147 |
+
}
|
| 148 |
+
initializers.append(init_info)
|
| 149 |
+
|
| 150 |
+
return {
|
| 151 |
+
'model_info': model_info,
|
| 152 |
+
'nodes': nodes,
|
| 153 |
+
'op_types': dict(op_types),
|
| 154 |
+
'inputs': inputs,
|
| 155 |
+
'outputs': outputs,
|
| 156 |
+
'value_info': value_info,
|
| 157 |
+
'initializers': initializers,
|
| 158 |
+
'total_nodes': len(nodes),
|
| 159 |
+
'total_parameters': sum(init['size'] for init in initializers)
|
| 160 |
+
}
|
| 161 |
+
|
| 162 |
+
except Exception as e:
|
| 163 |
+
return {'error': f"Failed to analyze ONNX model: {str(e)}"}
|
| 164 |
+
|
| 165 |
+
def create_onnx_description(onnx_analysis: dict) -> str:
|
| 166 |
+
"""
|
| 167 |
+
Creates a comprehensive description from ONNX analysis for LLM processing.
|
| 168 |
+
"""
|
| 169 |
+
if 'error' in onnx_analysis:
|
| 170 |
+
return f"ONNX Analysis Error: {onnx_analysis['error']}"
|
| 171 |
+
|
| 172 |
+
description = f"""
|
| 173 |
+
# ONNX Model Analysis Report
|
| 174 |
+
|
| 175 |
+
## Model Information
|
| 176 |
+
- Producer: {onnx_analysis['model_info']['producer_name']} v{onnx_analysis['model_info']['producer_version']}
|
| 177 |
+
- IR Version: {onnx_analysis['model_info']['ir_version']}
|
| 178 |
+
- Domain: {onnx_analysis['model_info']['domain']}
|
| 179 |
+
- Total Nodes: {onnx_analysis['total_nodes']}
|
| 180 |
+
- Total Parameters: {onnx_analysis['total_parameters']:,}
|
| 181 |
+
|
| 182 |
+
## Architecture Overview
|
| 183 |
+
The model contains {len(onnx_analysis['op_types'])} different operation types:
|
| 184 |
+
{chr(10).join([f"- {op}: {count} nodes" for op, count in onnx_analysis['op_types'].items()])}
|
| 185 |
+
|
| 186 |
+
## Input/Output Specification
|
| 187 |
+
### Inputs:
|
| 188 |
+
{chr(10).join([f"- {inp['name']}: shape {inp['shape']}, type {inp['type']}" for inp in onnx_analysis['inputs']])}
|
| 189 |
+
|
| 190 |
+
### Outputs:
|
| 191 |
+
{chr(10).join([f"- {out['name']}: shape {out['shape']}, type {out['type']}" for out in onnx_analysis['outputs']])}
|
| 192 |
+
|
| 193 |
+
## Detailed Node Structure
|
| 194 |
+
{chr(10).join([f"Node {i}: {node['op_type']} ({node['name']})" for i, node in enumerate(onnx_analysis['nodes'][:10])])}
|
| 195 |
+
{'...' if len(onnx_analysis['nodes']) > 10 else ''}
|
| 196 |
+
|
| 197 |
+
## Key Architectural Patterns
|
| 198 |
+
Based on the operation types and structure, this appears to be a {_infer_model_type(onnx_analysis['op_types'])} model.
|
| 199 |
+
"""
|
| 200 |
+
return description
|
| 201 |
+
|
| 202 |
+
def _infer_model_type(op_types: dict) -> str:
|
| 203 |
+
"""Infers the model type based on operation types."""
|
| 204 |
+
if any(op in op_types for op in ['Conv', 'ConvTranspose', 'MaxPool', 'AveragePool']):
|
| 205 |
+
if any(op in op_types for op in ['LSTM', 'GRU', 'RNN']):
|
| 206 |
+
return "Hybrid CNN-RNN"
|
| 207 |
+
elif 'Attention' in op_types or 'MatMul' in op_types:
|
| 208 |
+
return "CNN with Attention/Transformer components"
|
| 209 |
+
else:
|
| 210 |
+
return "Convolutional Neural Network (CNN)"
|
| 211 |
+
elif any(op in op_types for op in ['LSTM', 'GRU', 'RNN']):
|
| 212 |
+
return "Recurrent Neural Network (RNN/LSTM/GRU)"
|
| 213 |
+
elif any(op in op_types for op in ['Attention', 'MatMul']) and 'Reshape' in op_types:
|
| 214 |
+
return "Transformer/Attention-based model"
|
| 215 |
+
elif 'MatMul' in op_types and 'Add' in op_types:
|
| 216 |
+
return "Feed-forward Neural Network"
|
| 217 |
+
else:
|
| 218 |
+
return "Custom/Mixed architecture"
|
| 219 |
+
|
| 220 |
+
# --- 3. ENHANCED VISUALIZATION PROMPTS ---
|
| 221 |
+
|
| 222 |
+
def get_visualization_prompt(analysis_type: str) -> str:
|
| 223 |
+
"""Returns the appropriate system prompt for generating visualization code."""
|
| 224 |
+
prompts = {
|
| 225 |
+
"shap": """You are an expert data scientist. Based on the provided model description, generate a Python function `generate_shap_plot()` that creates a SHAP-style feature importance bar chart using only the Plotly library.
|
| 226 |
+
|
| 227 |
+
**Constraints:**
|
| 228 |
+
- The function must take no arguments.
|
| 229 |
+
- It must return a Plotly Figure object (`go.Figure`).
|
| 230 |
+
- **Crucially, do NOT import any external libraries like `shap`.** Use `plotly.graph_objects` (`go`) and `numpy` (`np`).
|
| 231 |
+
- Create realistic placeholder data (e.g., feature names and importance values) within the function. Do not assume access to a live model object.
|
| 232 |
+
- Make the chart visually appealing with proper titles and labels.
|
| 233 |
+
- For ONNX models, base feature names on the actual input/output tensor names if provided.
|
| 234 |
+
|
| 235 |
+
The output should ONLY be the complete Python code for the function.
|
| 236 |
+
""",
|
| 237 |
+
|
| 238 |
+
"lime": """You are an expert data scientist. Based on the model description, generate a Python function `generate_lime_plot()` that creates a LIME-style local interpretation horizontal bar chart using only Plotly.
|
| 239 |
+
|
| 240 |
+
**Constraints:**
|
| 241 |
+
- The function must take no arguments and return a `go.Figure` object.
|
| 242 |
+
- **Do not import external libraries like `lime`.**
|
| 243 |
+
- Generate realistic placeholder data for component contributions (both positive and negative) inside the function.
|
| 244 |
+
- For ONNX models, consider the actual model operations and create relevant feature contributions.
|
| 245 |
+
- Create visually appealing charts with proper titles and colors.
|
| 246 |
+
|
| 247 |
+
The output should ONLY be the Python code for the function.
|
| 248 |
+
""",
|
| 249 |
+
|
| 250 |
+
"attention": """You are an expert in transformer architectures. Based on the model description, generate a Python function `generate_attention_plot()` that creates an attention heatmap using Plotly Express.
|
| 251 |
+
|
| 252 |
+
**Constraints:**
|
| 253 |
+
- The function must take no arguments and return a `px.imshow` Figure object.
|
| 254 |
+
- Generate a realistic placeholder 2D numpy array for the attention weights inside the function.
|
| 255 |
+
- For ONNX models with attention mechanisms, create appropriate attention patterns.
|
| 256 |
+
- For non-attention models, generate a plausible feature correlation matrix.
|
| 257 |
+
- Use proper labels and titles for the heatmap.
|
| 258 |
+
|
| 259 |
+
The output should ONLY be the complete Python code for the function.
|
| 260 |
+
""",
|
| 261 |
+
|
| 262 |
+
"architecture": """You are a neural network architect. Based on the model description, generate a Python function `generate_architecture_plot()` that creates a complex, detailed architecture flow diagram using Plotly.
|
| 263 |
+
|
| 264 |
+
**Enhanced Requirements for Complex Architecture:**
|
| 265 |
+
- The function must take no arguments and return a `go.Figure` object.
|
| 266 |
+
- Create a multi-layered, hierarchical visualization showing different layer types
|
| 267 |
+
- Use different colors and shapes for different layer types
|
| 268 |
+
- Show connections between layers with arrows
|
| 269 |
+
- Include layer dimensions/parameters as annotations
|
| 270 |
+
- For ONNX models, use the actual operation types and create a detailed flow
|
| 271 |
+
- Make it visually impressive with proper spacing and professional styling
|
| 272 |
+
|
| 273 |
+
The output should ONLY be the complete Python code for the function.
|
| 274 |
+
""",
|
| 275 |
+
|
| 276 |
+
"parameter": """You are a deep learning engineer. Based on the model description, generate a Python function `generate_parameter_plot()` that creates a comprehensive parameter distribution visualization using Plotly.
|
| 277 |
+
|
| 278 |
+
**Enhanced Requirements:**
|
| 279 |
+
- The function must take no arguments and return a `go.Figure` object.
|
| 280 |
+
- Create a subplot with multiple visualizations:
|
| 281 |
+
* Donut chart for parameter distribution across layers
|
| 282 |
+
* Bar chart for layer-wise parameter counts
|
| 283 |
+
* Histogram of parameter magnitudes (simulated)
|
| 284 |
+
- For ONNX models, use actual initializer information if available
|
| 285 |
+
- Make it visually impressive with proper styling
|
| 286 |
+
|
| 287 |
+
The output should ONLY be the complete Python code for the function.
|
| 288 |
+
"""
|
| 289 |
+
}
|
| 290 |
+
return prompts.get(analysis_type, "")
|
| 291 |
+
|
| 292 |
+
# --- 4. LLM COMMUNICATION FUNCTIONS ---
|
| 293 |
+
|
| 294 |
+
def get_llm_response(prompt: str, model_description: str, client_name: str) -> str:
|
| 295 |
+
"""Generic function to get a response from the selected LLM."""
|
| 296 |
+
try:
|
| 297 |
+
if client_name == "Claude (Anthropic)" and anthropic_client:
|
| 298 |
+
message = anthropic_client.messages.create(
|
| 299 |
+
model="claude-3-5-sonnet-20240620",
|
| 300 |
+
max_tokens=3000,
|
| 301 |
+
temperature=0.1,
|
| 302 |
+
system=prompt + "\n\nIMPORTANT: Respond with ONLY the Python code, no explanations or markdown formatting.",
|
| 303 |
+
messages=[{"role": "user", "content": model_description}]
|
| 304 |
+
)
|
| 305 |
+
return message.content[0].text
|
| 306 |
+
|
| 307 |
+
elif client_name == "GPT (OpenAI)" and openai_client:
|
| 308 |
+
response = openai_client.chat.completions.create(
|
| 309 |
+
model="gpt-4o",
|
| 310 |
+
messages=[
|
| 311 |
+
{"role": "system", "content": prompt + "\n\nIMPORTANT: Respond with ONLY the Python code, no explanations or markdown formatting."},
|
| 312 |
+
{"role": "user", "content": model_description}
|
| 313 |
+
],
|
| 314 |
+
max_tokens=3000,
|
| 315 |
+
temperature=0.1
|
| 316 |
+
)
|
| 317 |
+
return response.choices[0].message.content
|
| 318 |
+
|
| 319 |
+
elif client_name == "Gemini (Google)" and GEMINI_API_KEY:
|
| 320 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 321 |
+
full_prompt = f"{prompt}\n\nIMPORTANT: Respond with ONLY the Python code, no explanations or markdown formatting.\n\nModel Description: {model_description}"
|
| 322 |
+
response = model.generate_content(full_prompt)
|
| 323 |
+
return response.text
|
| 324 |
+
|
| 325 |
+
elif client_name == "Mistral (Mistral)" and mistral_client:
|
| 326 |
+
messages = [
|
| 327 |
+
{"role": "system", "content": prompt + "\n\nIMPORTANT: Respond with ONLY the Python code, no explanations or markdown formatting."},
|
| 328 |
+
{"role": "user", "content": model_description}
|
| 329 |
+
]
|
| 330 |
+
response = mistral_client.chat(
|
| 331 |
+
model="mistral-large-latest",
|
| 332 |
+
messages=messages,
|
| 333 |
+
temperature=0.1
|
| 334 |
+
)
|
| 335 |
+
return response.choices[0].message.content
|
| 336 |
+
else:
|
| 337 |
+
return f"Error: {client_name} API key not configured or client unavailable."
|
| 338 |
+
|
| 339 |
+
except Exception as e:
|
| 340 |
+
return f"Error communicating with {client_name}: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
| 341 |
+
|
| 342 |
+
def extract_python_code(response: str) -> str:
|
| 343 |
+
"""Extracts Python code from a markdown-formatted string."""
|
| 344 |
+
# Remove markdown code blocks
|
| 345 |
+
pattern = r'```python\s*\n(.*?)\n```'
|
| 346 |
+
match = re.search(pattern, response, re.DOTALL)
|
| 347 |
+
if match:
|
| 348 |
+
return match.group(1).strip()
|
| 349 |
+
|
| 350 |
+
pattern = r'```python(.*?)```'
|
| 351 |
+
match = re.search(pattern, response, re.DOTALL)
|
| 352 |
+
if match:
|
| 353 |
+
return match.group(1).strip()
|
| 354 |
+
|
| 355 |
+
pattern = r'```\s*(.*?)\s*```'
|
| 356 |
+
match = re.search(pattern, response, re.DOTALL)
|
| 357 |
+
if match:
|
| 358 |
+
code = match.group(1).strip()
|
| 359 |
+
if 'def ' in code or 'import' in code:
|
| 360 |
+
return code
|
| 361 |
+
|
| 362 |
+
# If no code blocks found, try to extract function definition
|
| 363 |
+
lines = response.split('\n')
|
| 364 |
+
code_lines = []
|
| 365 |
+
in_code = False
|
| 366 |
+
|
| 367 |
+
for line in lines:
|
| 368 |
+
if (line.strip().startswith('import ') or
|
| 369 |
+
line.strip().startswith('from ') or
|
| 370 |
+
line.strip().startswith('def ')):
|
| 371 |
+
in_code = True
|
| 372 |
+
|
| 373 |
+
if in_code:
|
| 374 |
+
code_lines.append(line)
|
| 375 |
+
|
| 376 |
+
if code_lines:
|
| 377 |
+
extracted_code = '\n'.join(code_lines).strip()
|
| 378 |
+
if 'def ' in extracted_code:
|
| 379 |
+
return extracted_code
|
| 380 |
+
|
| 381 |
+
# Last resort: return the whole response if it contains function definition
|
| 382 |
+
if 'def ' in response and ('import' in response or 'plotly' in response):
|
| 383 |
+
return response.strip()
|
| 384 |
+
|
| 385 |
+
return ""
|
| 386 |
+
|
| 387 |
+
def safely_execute_visualization_code(code: str, plot_type: str, save_dir: str) -> str or None:
|
| 388 |
+
"""
|
| 389 |
+
Executes LLM-generated code to create a Plotly viz and saves it as a PNG.
|
| 390 |
+
Returns the file path of the generated image, or None on failure.
|
| 391 |
+
"""
|
| 392 |
+
if not code:
|
| 393 |
+
print(f"Warning: No {plot_type} visualization code was generated.")
|
| 394 |
+
return None
|
| 395 |
+
|
| 396 |
+
try:
|
| 397 |
+
# Define the execution environment
|
| 398 |
+
exec_globals = {
|
| 399 |
+
"go": go, "px": px, "np": np, "pd": pd,
|
| 400 |
+
"make_subplots": make_subplots, "__builtins__": __builtins__
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
# Execute the function definition
|
| 404 |
+
exec(code, exec_globals)
|
| 405 |
+
|
| 406 |
+
# Find the function name
|
| 407 |
+
func_name_match = re.search(r'def (\w+)\(', code)
|
| 408 |
+
if not func_name_match:
|
| 409 |
+
raise NameError("Could not find function definition in the generated code.")
|
| 410 |
+
func_name = func_name_match.group(1)
|
| 411 |
+
|
| 412 |
+
if func_name not in exec_globals:
|
| 413 |
+
raise NameError(f"Function {func_name} was not properly defined.")
|
| 414 |
+
|
| 415 |
+
# Call the generated function to get the figure object
|
| 416 |
+
fig = exec_globals[func_name]()
|
| 417 |
+
|
| 418 |
+
if not hasattr(fig, 'write_image'):
|
| 419 |
+
raise ValueError("Generated function did not return a valid Plotly figure object.")
|
| 420 |
+
|
| 421 |
+
# Define the save path and save the image
|
| 422 |
+
file_path = os.path.join(save_dir, f"{plot_type}_plot.png")
|
| 423 |
+
fig.write_image(file_path, width=1200, height=800, scale=2)
|
| 424 |
+
|
| 425 |
+
print(f"Successfully generated and saved image: {file_path}")
|
| 426 |
+
return file_path
|
| 427 |
+
|
| 428 |
+
except Exception as e:
|
| 429 |
+
print(f"ERROR executing or saving {plot_type} plot: {str(e)}")
|
| 430 |
+
print(f"Full traceback:\n{traceback.format_exc()}")
|
| 431 |
+
return None
|
| 432 |
+
|
| 433 |
+
# --- 5. MAIN PROCESSING FUNCTIONS ---
|
| 434 |
+
|
| 435 |
+
def process_comprehensive_analysis(model_input: str, onnx_file, llms_to_query: list, analysis_types: list) -> tuple:
|
| 436 |
+
"""
|
| 437 |
+
Processes both text/code descriptions and ONNX files for comprehensive analysis.
|
| 438 |
+
"""
|
| 439 |
+
if not llms_to_query:
|
| 440 |
+
return "Please select at least one LLM to query.", None, None, None, None, None
|
| 441 |
+
|
| 442 |
+
# Determine input type and create description
|
| 443 |
+
model_description = ""
|
| 444 |
+
|
| 445 |
+
if onnx_file is not None:
|
| 446 |
+
# Process ONNX file
|
| 447 |
+
print(f"Processing ONNX file: {onnx_file.name}")
|
| 448 |
+
try:
|
| 449 |
+
onnx_analysis = analyze_onnx_model(onnx_file.name)
|
| 450 |
+
model_description = create_onnx_description(onnx_analysis)
|
| 451 |
+
print("ONNX analysis completed successfully")
|
| 452 |
+
except Exception as e:
|
| 453 |
+
return f"Error processing ONNX file: {str(e)}", None, None, None, None, None
|
| 454 |
+
|
| 455 |
+
elif model_input.strip():
|
| 456 |
+
# Use text input
|
| 457 |
+
model_description = model_input.strip()
|
| 458 |
+
|
| 459 |
+
else:
|
| 460 |
+
return "Please provide either a model description/code or upload an ONNX file.", None, None, None, None, None
|
| 461 |
+
|
| 462 |
+
# Create a unique directory for this run's plots
|
| 463 |
+
run_dir = os.path.join("temp_plots", str(uuid.uuid4()))
|
| 464 |
+
os.makedirs(run_dir, exist_ok=True)
|
| 465 |
+
print(f"Created temporary directory for plots: {run_dir}")
|
| 466 |
+
|
| 467 |
+
# Text Analysis Generation
|
| 468 |
+
text_analysis_prompt = """You are a model analysis expert. Provide a comprehensive technical analysis of the provided model. Cover:
|
| 469 |
+
|
| 470 |
+
1. **Model Architecture**: Type, key components, and design patterns
|
| 471 |
+
2. **Technical Specifications**: Layer types, parameters, complexity
|
| 472 |
+
3. **Operational Analysis**: Key operations, computational requirements
|
| 473 |
+
4. **Performance Characteristics**: Strengths, limitations, optimization opportunities
|
| 474 |
+
5. **Use Case Assessment**: Suitable applications, domain-specific considerations
|
| 475 |
+
6. **ONNX-Specific Analysis** (if applicable): Export quality, operator support, optimization potential
|
| 476 |
+
|
| 477 |
+
Provide detailed, technical insights with proper markdown formatting."""
|
| 478 |
+
|
| 479 |
+
full_response = ""
|
| 480 |
+
if "comprehensive" in analysis_types:
|
| 481 |
+
full_response += "# π§ Comprehensive AI Model Analysis\n\n"
|
| 482 |
+
|
| 483 |
+
if onnx_file is not None:
|
| 484 |
+
full_response += "## π ONNX Model Summary\n"
|
| 485 |
+
full_response += f"**File:** {onnx_file.name}\n"
|
| 486 |
+
full_response += f"**Analysis Status:** Successfully processed\n\n"
|
| 487 |
+
|
| 488 |
+
for llm in llms_to_query:
|
| 489 |
+
print(f"Getting text analysis from {llm}...")
|
| 490 |
+
full_response += f"## π€ {llm} - Technical Analysis\n\n"
|
| 491 |
+
interpretation = get_llm_response(text_analysis_prompt, model_description, llm)
|
| 492 |
+
full_response += f"{interpretation}\n\n---\n\n"
|
| 493 |
+
|
| 494 |
+
# Visualization Generation
|
| 495 |
+
viz_llm = llms_to_query[0]
|
| 496 |
+
print(f"Using {viz_llm} for visualization generation...")
|
| 497 |
+
|
| 498 |
+
viz_outputs = {}
|
| 499 |
+
viz_types = ["shap", "lime", "attention", "architecture", "parameter"]
|
| 500 |
+
|
| 501 |
+
for viz_type in viz_types:
|
| 502 |
+
try:
|
| 503 |
+
prompt = get_visualization_prompt(viz_type)
|
| 504 |
+
if not prompt:
|
| 505 |
+
viz_outputs[viz_type] = None
|
| 506 |
+
continue
|
| 507 |
+
|
| 508 |
+
print(f"Generating {viz_type} visualization...")
|
| 509 |
+
generated_code_response = get_llm_response(prompt, model_description, viz_llm)
|
| 510 |
+
|
| 511 |
+
if "Error" in generated_code_response:
|
| 512 |
+
print(f"LLM Error for {viz_type}: {generated_code_response}")
|
| 513 |
+
viz_outputs[viz_type] = None
|
| 514 |
+
continue
|
| 515 |
+
|
| 516 |
+
cleaned_code = extract_python_code(generated_code_response)
|
| 517 |
+
|
| 518 |
+
if not cleaned_code:
|
| 519 |
+
print(f"Could not extract valid code for {viz_type}.")
|
| 520 |
+
viz_outputs[viz_type] = None
|
| 521 |
+
continue
|
| 522 |
+
|
| 523 |
+
# Execute code, save plot, and get the file path
|
| 524 |
+
image_path = safely_execute_visualization_code(cleaned_code, viz_type, run_dir)
|
| 525 |
+
viz_outputs[viz_type] = image_path
|
| 526 |
+
|
| 527 |
+
except Exception as e:
|
| 528 |
+
print(f"Unexpected error in main loop for {viz_type}: {e}")
|
| 529 |
+
viz_outputs[viz_type] = None
|
| 530 |
+
|
| 531 |
+
return (
|
| 532 |
+
full_response,
|
| 533 |
+
viz_outputs.get("shap"),
|
| 534 |
+
viz_outputs.get("lime"),
|
| 535 |
+
viz_outputs.get("attention"),
|
| 536 |
+
viz_outputs.get("architecture"),
|
| 537 |
+
viz_outputs.get("parameter")
|
| 538 |
+
)
|
| 539 |
+
|
| 540 |
+
def process_text_comprehensive_analysis(model_input: str, llms_to_query, analysis_types: list) -> tuple:
|
| 541 |
+
"""
|
| 542 |
+
Processes only text/code descriptions for comprehensive analysis (no ONNX support).
|
| 543 |
+
Accepts llms_to_query as a string or a list for compatibility.
|
| 544 |
+
"""
|
| 545 |
+
# Ensure llms_to_query is a list
|
| 546 |
+
if isinstance(llms_to_query, str):
|
| 547 |
+
llms_to_query = [llms_to_query]
|
| 548 |
+
if not llms_to_query:
|
| 549 |
+
return "Please select at least one LLM to query.", None, None, None, None, None
|
| 550 |
+
|
| 551 |
+
if not model_input.strip():
|
| 552 |
+
return "Please provide a model description or code.", None, None, None, None, None
|
| 553 |
+
|
| 554 |
+
model_description = model_input.strip()
|
| 555 |
+
|
| 556 |
+
# Create a unique directory for this run's plots
|
| 557 |
+
run_dir = os.path.join("temp_plots", str(uuid.uuid4()))
|
| 558 |
+
os.makedirs(run_dir, exist_ok=True)
|
| 559 |
+
print(f"Created temporary directory for plots: {run_dir}")
|
| 560 |
+
|
| 561 |
+
# Text Analysis Generation
|
| 562 |
+
text_analysis_prompt = """You are a model analysis expert. Provide a comprehensive technical analysis of the provided model. Cover:
|
| 563 |
+
|
| 564 |
+
1. **Model Architecture**: Type, key components, and design patterns
|
| 565 |
+
2. **Technical Specifications**: Layer types, parameters, complexity
|
| 566 |
+
3. **Operational Analysis**: Key operations, computational requirements
|
| 567 |
+
4. **Performance Characteristics**: Strengths, limitations, optimization opportunities
|
| 568 |
+
5. **Use Case Assessment**: Suitable applications, domain-specific considerations
|
| 569 |
+
|
| 570 |
+
Provide detailed, technical insights with proper markdown formatting."""
|
| 571 |
+
|
| 572 |
+
full_response = ""
|
| 573 |
+
if "comprehensive" in analysis_types:
|
| 574 |
+
full_response += "# π§ Comprehensive AI Model Analysis\n\n"
|
| 575 |
+
for llm in llms_to_query:
|
| 576 |
+
print(f"Getting text analysis from {llm}...")
|
| 577 |
+
full_response += f"## π€ {llm} - Technical Analysis\n\n"
|
| 578 |
+
interpretation = get_llm_response(text_analysis_prompt, model_description, llm)
|
| 579 |
+
full_response += f"{interpretation}\n\n---\n\n"
|
| 580 |
+
|
| 581 |
+
# Visualization Generation
|
| 582 |
+
viz_llm = llms_to_query[0]
|
| 583 |
+
print(f"Using {viz_llm} for visualization generation...")
|
| 584 |
+
|
| 585 |
+
viz_outputs = {}
|
| 586 |
+
viz_types = ["shap", "lime", "attention", "architecture", "parameter"]
|
| 587 |
+
|
| 588 |
+
for viz_type in viz_types:
|
| 589 |
+
try:
|
| 590 |
+
prompt = get_visualization_prompt(viz_type)
|
| 591 |
+
if not prompt:
|
| 592 |
+
viz_outputs[viz_type] = None
|
| 593 |
+
continue
|
| 594 |
+
|
| 595 |
+
print(f"Generating {viz_type} visualization...")
|
| 596 |
+
generated_code_response = get_llm_response(prompt, model_description, viz_llm)
|
| 597 |
+
|
| 598 |
+
if "Error" in generated_code_response:
|
| 599 |
+
print(f"LLM Error for {viz_type}: {generated_code_response}")
|
| 600 |
+
viz_outputs[viz_type] = None
|
| 601 |
+
continue
|
| 602 |
+
|
| 603 |
+
cleaned_code = extract_python_code(generated_code_response)
|
| 604 |
+
|
| 605 |
+
if not cleaned_code:
|
| 606 |
+
print(f"Could not extract valid code for {viz_type}.")
|
| 607 |
+
viz_outputs[viz_type] = None
|
| 608 |
+
continue
|
| 609 |
+
|
| 610 |
+
# Execute code, save plot, and get the file path
|
| 611 |
+
image_path = safely_execute_visualization_code(cleaned_code, viz_type, run_dir)
|
| 612 |
+
viz_outputs[viz_type] = image_path
|
| 613 |
+
|
| 614 |
+
except Exception as e:
|
| 615 |
+
print(f"Unexpected error in main loop for {viz_type}: {e}")
|
| 616 |
+
viz_outputs[viz_type] = None
|
| 617 |
+
|
| 618 |
+
return (
|
| 619 |
+
full_response,
|
| 620 |
+
viz_outputs.get("shap"),
|
| 621 |
+
viz_outputs.get("lime"),
|
| 622 |
+
viz_outputs.get("attention"),
|
| 623 |
+
viz_outputs.get("architecture"),
|
| 624 |
+
viz_outputs.get("parameter")
|
| 625 |
+
)
|
| 626 |
+
|
| 627 |
+
# --- 6. ENHANCED GRADIO USER INTERFACE ---
|
| 628 |
+
|
| 629 |
+
with gr.Blocks(theme=gr.themes.Soft(), css="""
|
| 630 |
+
.gradio-container {max-width: 1400px !important;}
|
| 631 |
+
.tab-nav button {font-size: 16px; font-weight: bold;}
|
| 632 |
+
.output-image {border: 2px solid #e0e0e0; border-radius: 10px; padding: 10px;}
|
| 633 |
+
.analysis-output {background: #f8f9fa; border-radius: 10px; padding: 20px;}
|
| 634 |
+
.header-text {text-align: center; color: #2c3e50; margin-bottom: 20px;}
|
| 635 |
+
""", title="π§ AI Model Analysis Suite") as app:
|
| 636 |
+
|
| 637 |
+
gr.HTML("""
|
| 638 |
+
<div class="header-text">
|
| 639 |
+
<h1>π§ AI Model Analysis Suite</h1>
|
| 640 |
+
<p><strong>Comprehensive AI model analysis using multiple LLMs with advanced visualizations</strong></p>
|
| 641 |
+
<p><em>Upload ONNX models or describe your model architecture for deep technical analysis</em></p>
|
| 642 |
+
</div>
|
| 643 |
+
""")
|
| 644 |
+
|
| 645 |
+
with gr.Row():
|
| 646 |
+
with gr.Column(scale=1):
|
| 647 |
+
gr.HTML("<h3>π Model Input</h3>")
|
| 648 |
+
|
| 649 |
+
with gr.Tabs():
|
| 650 |
+
with gr.Tab("Text Description"):
|
| 651 |
+
model_input = gr.Textbox(
|
| 652 |
+
label="Model Description or Code",
|
| 653 |
+
placeholder="Describe your model architecture, paste PyTorch/TensorFlow code, or provide technical specifications...",
|
| 654 |
+
lines=10,
|
| 655 |
+
max_lines=20
|
| 656 |
+
)
|
| 657 |
+
|
| 658 |
+
with gr.Tab("ONNX Upload"):
|
| 659 |
+
onnx_file = gr.File(
|
| 660 |
+
label="Upload ONNX Model File",
|
| 661 |
+
file_types=[".onnx"],
|
| 662 |
+
file_count="single"
|
| 663 |
+
)
|
| 664 |
+
|
| 665 |
+
gr.HTML("<h3>π€ Analysis Configuration</h3>")
|
| 666 |
+
|
| 667 |
+
llm_selection = gr.CheckboxGroup(
|
| 668 |
+
choices=["Claude (Anthropic)", "GPT (OpenAI)", "Gemini (Google)", "Mistral (Mistral)"],
|
| 669 |
+
label="Select LLMs for Analysis",
|
| 670 |
+
value=["Claude (Anthropic)"],
|
| 671 |
+
info="Choose which LLMs to use for model interpretation"
|
| 672 |
+
)
|
| 673 |
+
|
| 674 |
+
analysis_types = gr.CheckboxGroup(
|
| 675 |
+
choices=["comprehensive", "visualizations"],
|
| 676 |
+
label="Analysis Types",
|
| 677 |
+
value=["comprehensive", "visualizations"],
|
| 678 |
+
info="Select the types of analysis to perform"
|
| 679 |
+
)
|
| 680 |
+
|
| 681 |
+
analyze_btn = gr.Button("π Analyze Model", variant="primary", size="lg")
|
| 682 |
+
|
| 683 |
+
with gr.Column(scale=2):
|
| 684 |
+
gr.HTML("<h3>π Analysis Results</h3>")
|
| 685 |
+
|
| 686 |
+
with gr.Tabs():
|
| 687 |
+
with gr.Tab("π Technical Analysis"):
|
| 688 |
+
analysis_output = gr.Markdown(
|
| 689 |
+
label="Comprehensive Analysis",
|
| 690 |
+
elem_classes=["analysis-output"]
|
| 691 |
+
)
|
| 692 |
+
|
| 693 |
+
with gr.Tab("π SHAP Analysis"):
|
| 694 |
+
shap_plot = gr.Image(
|
| 695 |
+
label="SHAP Feature Importance",
|
| 696 |
+
elem_classes=["output-image"]
|
| 697 |
+
)
|
| 698 |
+
|
| 699 |
+
with gr.Tab("π LIME Interpretation"):
|
| 700 |
+
lime_plot = gr.Image(
|
| 701 |
+
label="LIME Local Interpretation",
|
| 702 |
+
elem_classes=["output-image"]
|
| 703 |
+
)
|
| 704 |
+
|
| 705 |
+
with gr.Tab("π― Attention Visualization"):
|
| 706 |
+
attention_plot = gr.Image(
|
| 707 |
+
label="Attention Heatmap",
|
| 708 |
+
elem_classes=["output-image"]
|
| 709 |
+
)
|
| 710 |
+
|
| 711 |
+
with gr.Tab("ποΈ Architecture Diagram"):
|
| 712 |
+
architecture_plot = gr.Image(
|
| 713 |
+
label="Model Architecture",
|
| 714 |
+
elem_classes=["output-image"]
|
| 715 |
+
)
|
| 716 |
+
|
| 717 |
+
with gr.Tab("π Parameter Analysis"):
|
| 718 |
+
parameter_plot = gr.Image(
|
| 719 |
+
label="Parameter Distribution",
|
| 720 |
+
elem_classes=["output-image"]
|
| 721 |
+
)
|
| 722 |
+
|
| 723 |
+
# Event handlers
|
| 724 |
+
analyze_btn.click(
|
| 725 |
+
fn=process_text_comprehensive_analysis,
|
| 726 |
+
inputs=[model_input, llm_selection, analysis_types],
|
| 727 |
+
outputs=[analysis_output, shap_plot, lime_plot, attention_plot, architecture_plot, parameter_plot],
|
| 728 |
+
show_progress=True
|
| 729 |
+
)
|
| 730 |
+
|
| 731 |
+
# Add examples
|
| 732 |
+
gr.HTML("<h3>π‘ Example Inputs</h3>")
|
| 733 |
+
|
| 734 |
+
with gr.Row():
|
| 735 |
+
with gr.Column():
|
| 736 |
+
gr.Examples(
|
| 737 |
+
examples=[
|
| 738 |
+
["ResNet-50 convolutional neural network with 50 layers, batch normalization, and residual connections for image classification"],
|
| 739 |
+
["BERT transformer model with 12 layers, 768 hidden dimensions, and multi-head attention for NLP tasks"],
|
| 740 |
+
["LSTM recurrent neural network with 256 hidden units for time series prediction"],
|
| 741 |
+
["U-Net architecture with encoder-decoder structure and skip connections for image segmentation"]
|
| 742 |
+
],
|
| 743 |
+
inputs=model_input,
|
| 744 |
+
label="Model Description Examples"
|
| 745 |
+
)
|
| 746 |
+
|
| 747 |
+
if __name__ == "__main__":
|
| 748 |
+
app.launch(
|
| 749 |
+
server_name="0.0.0.0",
|
| 750 |
+
server_port=7860,
|
| 751 |
+
share=False,
|
| 752 |
+
debug=True,
|
| 753 |
+
show_error=True,
|
| 754 |
+
mcp_server=True)
|