Skip to content

Gui Vizsualition for SQLite Working #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from datetime import datetime, timedelta
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import os
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect, HTTPException
from pydantic import BaseModel
import pandas as pd
import json
from pathlib import Path
from azure.identity import DefaultAzureCredential
from mlos_bench.storage import from_config
from copy import deepcopy
import subprocess
import logging
import asyncio
from fastapi.middleware.cors import CORSMiddleware
import re
import json5

app = FastAPI()

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


# Load the storage config and connect to the storage
try:
storage = storage = from_config(config="storage/sqlite.jsonc")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Error loading storage configuration: {e}")


@app.get("/experiments")
def get_experiments():
return list(storage.experiments.keys())


@app.get("/experiment_results/{experiment_id}")
def get_experiment_results(experiment_id: str):
try:
exp = storage.experiments[experiment_id]
return exp.results_df.to_dict(orient="records")
except KeyError:
raise HTTPException(status_code=404, detail="Experiment not found")


def count_categorical_values(df: pd.DataFrame) -> str:
categorical_counts = {}
for col in df.select_dtypes(include=["object", "category"]).columns:
counts = df[col].value_counts().to_dict()
categorical_counts[col] = counts

count_str = "Categorical Counts:\n"
for col, counts in categorical_counts.items():
count_str += f"{col}:\n"
for value, count in counts.items():
count_str += f" {value}: {count}\n"

return count_str


# Load credentials from the JSON file
try:
with open("azure_openai_credentials.json", "r") as file:
credentials = json.load(file)
except Exception as e:
print("Error no open ai cred")

# Try to create the AzureOpenAI client
try:
client = AzureOpenAI(
azure_endpoint=credentials["azure_endpoint"],
api_key=credentials["api_key"],
api_version=credentials["api_version"],
)
except Exception as e:
print("Error creating AzureOpenAI client:", e)


class ExperimentExplanationRequest(BaseModel):
experiment_id: str


@app.post("/get_experiment_explanation")
def get_experiment_explanation(request: ExperimentExplanationRequest):
experiment_id = request.experiment_id
try:
exp = storage.experiments[experiment_id]
# Taking only the first 10 rows for simplicity
df = exp.results_df.tail(10)
experiment_data = df.to_dict(orient="records")

df_head = exp.results_df.head(10)
experiment_data_head = df_head.to_dict(orient="records")

df_des = exp.results_df.describe()
experiment_data_des = df_des.to_dict(orient="records")

count_str = count_categorical_values(df)

prompt = f"Explain the following experiment data: First 10 rows {experiment_data_head} last 10 {experiment_data} & descriptive stats {experiment_data_des} & categorical vars counts {count_str}. Give me params to complement config. params present in the data. Also explain what each param does and params for MySQL config that would complement what we have and can boost preformance if tuned. Explain which are dangreous to tune as it might fail the server. Also talk about parameters that are safe to tune. Talk about each in list format so that you are listing all information relevant to a param under its name"

response = client.chat.completions.create(
model="gpt4o", # model = "deployment_name".
messages=[{"role": "assistant", "content": prompt}],
max_tokens=1000,
)

explanation = response.choices[0].message.content.strip()
print(explanation)
return {"explanation": explanation}
except KeyError:
raise HTTPException(status_code=404, detail="Experiment not found")


if __name__ == "__main__":
import uvicorn

uvicorn.run(app, host="0.0.0.0", port=8000, reload=True)
Loading