Nolan Zandi
Claude Sonnet 4.6
chore: promote gpt-4.1 to default model in dropdown
980f6cb
Raw History Blame Contribute Delete
9.16 kB
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="<p style='color:#b91c1c;text-align:center;margin:6px 0;font-size:14px;'>Please enter your API key.</p>"),
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"""
<div style="display:flex;flex-direction:column;align-items:center;gap:6px;padding:10px 0 4px;">
<div style="display:inline-flex;align-items:center;gap:10px;background:#f0fdf4;border:1px solid #86efac;
padding:8px 20px;border-radius:9999px;font-size:13px;font-weight:500;color:#15803d;
box-shadow:0 1px 3px rgba(0,0,0,0.06);">
<i class="fas fa-circle-check" style="font-size:14px;"></i>
<span>{provider}</span>
<span style="color:#86efac;">ยท</span>
<span style="font-weight:600;">{model}</span>
</div>
<p style="margin:0;font-size:11px;color:#9ca3af;letter-spacing:0.02em;">
Session active โ€” use the button below to change
</p>
</div>
"""
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 = """<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Virtual Data Analyst</title>
<!-- Tailwind CSS -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<!-- Custom Styles -->
<link rel="stylesheet" href="/gradio_api/file=assets/styles.css">
"""
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("""
<div style="max-width:640px;margin:28px auto 12px;padding:22px 28px;
background:linear-gradient(135deg,#eff6ff 0%,#e0f2fe 100%);
border:1px solid #bfdbfe;border-radius:14px;
box-shadow:0 2px 8px rgba(59,130,246,0.08);">
<div style="display:flex;align-items:flex-start;gap:16px;">
<div style="width:42px;height:42px;flex-shrink:0;background:#3B82F6;
border-radius:10px;display:flex;align-items:center;
justify-content:center;box-shadow:0 2px 6px rgba(59,130,246,0.35);">
<i class="fas fa-key" style="color:white;font-size:16px;"></i>
</div>
<div>
<h3 style="color:#1e40af;margin:0 0 6px;font-size:16px;font-weight:700;letter-spacing:-0.01em;">
Get Started
</h3>
<p style="color:#3730a3;font-size:13.5px;margin:0;line-height:1.6;">
Enter your <strong>OpenAI</strong>
(<code style="background:rgba(255,255,255,0.7);padding:1px 6px;border-radius:4px;font-size:12px;">sk-...</code>)
or <strong>Anthropic</strong>
(<code style="background:rgba(255,255,255,0.7);padding:1px 6px;border-radius:4px;font-size:12px;">sk-ant-...</code>)
API key. The model list updates automatically. Your key is held in memory only
and cleared when you leave โ€” never saved or shared.
</p>
</div>
</div>
</div>
""")
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("""
<header class="max-w-4xl mx-auto mb-12 text-center">
<h1 class="text-4xl font-bold text-gray-900 mb-4">Virtual Data Analyst</h1>
<p class="text-lg text-gray-600 mb-6">
A powerful tool for data analysis, visualizations, and insights
</p>
</header>
<main class="max-w-4xl mx-auto">
<div class="mt-12 grid md:grid-cols-3 gap-6" style="margin-bottom:3px !important;">
<div class="feature-card bg-white p-6 rounded-lg shadow-md">
<i class="feature-icon fas fa-chart-line text-primary text-2xl mb-4"></i>
<h3 class="font-semibold text-gray-800 mb-2">Advanced Analytics</h3>
<p class="text-gray-600 text-sm">Run SQL queries, perform regressions, and analyze results with ease</p>
</div>
<div class="feature-card bg-white p-6 rounded-lg shadow-md">
<i class="feature-icon fas fa-chart-pie text-primary text-2xl mb-4"></i>
<h3 class="font-semibold text-gray-800 mb-2">Rich Visualizations</h3>
<p class="text-gray-600 text-sm">Create scatter plots, line charts, pie charts, and more</p>
</div>
<div class="feature-card bg-white p-6 rounded-lg shadow-md">
<i class="feature-icon fas fa-magic text-primary text-2xl mb-4"></i>
<h3 class="font-semibold text-gray-800 mb-2">Automated Insights</h3>
<p class="text-gray-600 text-sm">Get instant insights and recommendations for your data</p>
</div>
</div>
</main>""")
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("""
<footer class="max-w-4xl mx-auto mt-12 text-center text-gray-500 text-sm">
<p>This application is under active development. For bugs or feedback, please open a discussion in the community tab.</p>
</footer>""")
demo.unload(delete_db)
demo.launch(debug=True, allowed_paths=["temp/", "assets/"])