from utils import TEMP_DIR, message_dict, api_key_store, model_store
import gradio as gr
import templates.data_file as data_file, templates.sql_db as sql_db, templates.doc_db as doc_db, templates.graphql as graphql
import os
from dotenv import load_dotenv
load_dotenv()
def delete_db(req: gr.Request):
import shutil
dir_path = TEMP_DIR / str(req.session_hash)
if os.path.exists(dir_path):
shutil.rmtree(dir_path)
message_dict[req.session_hash] = {}
api_key_store.pop(req.session_hash, None)
model_store.pop(req.session_hash, None)
def set_api_key(api_key, model, request: gr.Request):
api_key = api_key.strip()
if not api_key:
return (
gr.update(visible=True),
gr.update(visible=True, value="
Please enter your API key.
"),
gr.update(visible=False),
)
api_key_store[request.session_hash] = api_key
model_store[request.session_hash] = model
provider = "Anthropic" if api_key.startswith("sk-ant-") else "OpenAI"
provider_icon = "fa-a" if provider == "Anthropic" else "fa-o"
badge_html = f"""
{provider}
ยท
{model}
Session active โ use the button below to change
"""
return gr.update(visible=False), gr.update(visible=True, value=badge_html), gr.update(visible=True)
def show_api_form():
return gr.update(visible=True), gr.update(visible=False, value=""), gr.update(visible=False)
css = ".file_marker .large{min-height:50px !important;} .padding{padding:0;} .description_component{overflow:visible !important;}"
head = """
Virtual Data Analyst
"""
theme = gr.themes.Base(primary_hue="sky", secondary_hue="slate", font=[gr.themes.GoogleFont("Inter"), "Inter", "sans-serif"]).set(
button_primary_background_fill="#3B82F6",
button_secondary_background_fill="#6B7280",
)
from pathlib import Path
gr.set_static_paths(paths=[Path.cwd().absolute() / "assets"])
_env_api_key = os.getenv("OPENAI_API_KEY", "")
OPENAI_MODELS = [
"gpt-4.1", "gpt-4.1-mini", "gpt-4.1-nano",
"gpt-4o", "gpt-4o-mini",
"o3-mini", "o4-mini",
"gpt-5.4-mini", "gpt-5.4", "gpt-5.5",
]
ANTHROPIC_MODELS = [
"claude-sonnet-4-6",
"claude-opus-4-8",
"claude-haiku-4-5-20251001",
]
def update_models(api_key):
if api_key.strip().startswith("sk-ant-"):
return gr.update(choices=ANTHROPIC_MODELS, value=ANTHROPIC_MODELS[0])
return gr.update(choices=OPENAI_MODELS, value=OPENAI_MODELS[0])
with gr.Blocks(theme=theme, css=css, head=head, delete_cache=(3600, 3600)) as demo:
with gr.Column(visible=True) as api_key_section:
gr.HTML("""
Get Started
Enter your OpenAI
(sk-...)
or Anthropic
(sk-ant-...)
API key. The model list updates automatically. Your key is held in memory only
and cleared when you leave โ never saved or shared.
""")
with gr.Row(equal_height=True):
api_key_input = gr.Textbox(
label="API Key",
placeholder="sk-proj-... or sk-ant-api03-...",
type="password",
value=_env_api_key,
scale=4,
)
model_dropdown = gr.Dropdown(
label="Model",
choices=OPENAI_MODELS,
value=OPENAI_MODELS[0],
scale=2,
)
api_key_btn = gr.Button("Set API Key", variant="primary", scale=1, min_width=120)
api_key_status = gr.HTML("", visible=False)
change_key_btn = gr.Button("๐ Change Key / Model", variant="secondary", visible=False, size="sm")
api_key_input.change(fn=update_models, inputs=api_key_input, outputs=model_dropdown)
api_key_btn.click(
fn=set_api_key,
inputs=[api_key_input, model_dropdown],
outputs=[api_key_section, api_key_status, change_key_btn],
)
change_key_btn.click(fn=show_api_form, outputs=[api_key_section, api_key_status, change_key_btn])
header = gr.HTML("""
Virtual Data Analyst
A powerful tool for data analysis, visualizations, and insights
Advanced Analytics
Run SQL queries, perform regressions, and analyze results with ease
Rich Visualizations
Create scatter plots, line charts, pie charts, and more
Automated Insights
Get instant insights and recommendations for your data
""")
with gr.Tab("๐ Data File"):
data_file.demo.render()
with gr.Tab("๐ SQL Database"):
sql_db.demo.render()
with gr.Tab("๐ MongoDB"):
doc_db.demo.render()
with gr.Tab("โก GraphQL API"):
graphql.demo.render()
footer = gr.HTML("""
This application is under active development. For bugs or feedback, please open a discussion in the community tab.
""")
demo.unload(delete_db)
demo.launch(debug=True, allowed_paths=["temp/", "assets/"])