Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import shutil
|
|
| 6 |
import wandb
|
| 7 |
import time
|
| 8 |
import psutil
|
|
|
|
| 9 |
|
| 10 |
from huggingface_hub import login
|
| 11 |
|
|
@@ -80,6 +81,12 @@ def format_insights(insights, visuals):
|
|
| 80 |
|
| 81 |
def analyze_data(csv_file, additional_notes=""):
|
| 82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
# Start timing
|
| 84 |
start_time = time.time()
|
| 85 |
|
|
@@ -131,6 +138,55 @@ def analyze_data(csv_file, additional_notes=""):
|
|
| 131 |
}
|
| 132 |
)
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# Measure execution time and memory usage
|
| 135 |
execution_time = time.time() - start_time
|
| 136 |
final_memory = process.memory_info().rss / 1024 ** 2 # Convert to MB
|
|
|
|
| 6 |
import wandb
|
| 7 |
import time
|
| 8 |
import psutil
|
| 9 |
+
import optuna
|
| 10 |
|
| 11 |
from huggingface_hub import login
|
| 12 |
|
|
|
|
| 81 |
|
| 82 |
def analyze_data(csv_file, additional_notes=""):
|
| 83 |
|
| 84 |
+
def objective(trial):
|
| 85 |
+
learning_rate = trial.suggest_loguniform("learning_rate", 1e-5, 5e-3)
|
| 86 |
+
batch_size = trial.suggest_categorical("batch_size", [8, 16, 32])
|
| 87 |
+
num_epochs = trial.suggest_int("num_epochs", 1, 5)
|
| 88 |
+
|
| 89 |
+
|
| 90 |
# Start timing
|
| 91 |
start_time = time.time()
|
| 92 |
|
|
|
|
| 138 |
}
|
| 139 |
)
|
| 140 |
|
| 141 |
+
analysis_result = agent.run(
|
| 142 |
+
f"""Perform comprehensive analysis with:
|
| 143 |
+
- Learning Rate: {learning_rate}
|
| 144 |
+
- Batch Size: {batch_size}
|
| 145 |
+
- Epochs: {num_epochs}
|
| 146 |
+
""",
|
| 147 |
+
additional_args={}
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
def tune_hyperparameters(n_trials: int):
|
| 151 |
+
study = optuna.create_study(direction="minimize")
|
| 152 |
+
study.optimize(objective, n_trials=n_trials)
|
| 153 |
+
|
| 154 |
+
return f"Best Hyperparameters: {study.best_params}"
|
| 155 |
+
|
| 156 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 157 |
+
gr.Markdown("## 📊 AI Data Analysis Agent with Hyperparameter Optimization")
|
| 158 |
+
|
| 159 |
+
with gr.Row():
|
| 160 |
+
with gr.Column():
|
| 161 |
+
file_input = gr.File(label="Upload CSV Dataset", type="filepath")
|
| 162 |
+
notes_input = gr.Textbox(label="Dataset Notes (Optional)", lines=3)
|
| 163 |
+
analyze_btn = gr.Button("Analyze", variant="primary")
|
| 164 |
+
optuna_trials = gr.Number(label="Number of Hyperparameter Tuning Trials", value=10)
|
| 165 |
+
tune_btn = gr.Button("Optimize Hyperparameters", variant="secondary")
|
| 166 |
+
|
| 167 |
+
with gr.Column():
|
| 168 |
+
analysis_output = gr.Markdown("### Analysis results will appear here...")
|
| 169 |
+
optuna_output = gr.Textbox(label="Best Hyperparameters")
|
| 170 |
+
gallery = gr.Gallery(label="Data Visualizations", columns=2)
|
| 171 |
+
|
| 172 |
+
analyze_btn.click(
|
| 173 |
+
fn=analyze_data,
|
| 174 |
+
inputs=[file_input, notes_input],
|
| 175 |
+
outputs=[analysis_output, gallery]
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
tune_btn.click(
|
| 179 |
+
fn=tune_hyperparameters,
|
| 180 |
+
inputs=[optuna_trials],
|
| 181 |
+
outputs=[optuna_output]
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
demo.launch(debug=True)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
# Assume we minimize some loss value (replace with actual metric)
|
| 188 |
+
loss = analysis_result.get("loss", 0.1) # Mock value
|
| 189 |
+
return loss # Optuna minimizes the loss
|
| 190 |
# Measure execution time and memory usage
|
| 191 |
execution_time = time.time() - start_time
|
| 192 |
final_memory = process.memory_info().rss / 1024 ** 2 # Convert to MB
|