import gradio as gr import pandas as pd import plotly.express as px import plotly.graph_objects as go from datasets import load_dataset # --------------------------------------------------------------------------- # Data loading # --------------------------------------------------------------------------- DATASETS = { "FR": "AYI-NEDJIMI/prompt-engineering-fr", "EN": "AYI-NEDJIMI/prompt-engineering-en", } COLUMNS = [ "technique", "category", "performance_impact", "description", "name", "example_prompt", "implementation", "example_output", "question", "answer", "model_compatibility", "difficulty", "use_case", "when_to_use", "effectiveness_score", ] CATEGORIES = ["all", "basic", "advanced", "structured", "optimization", "domain_specific"] _cache: dict[str, pd.DataFrame] = {} def load_data(lang: str) -> pd.DataFrame: if lang in _cache: return _cache[lang] try: ds = load_dataset(DATASETS[lang], split="train") df = ds.to_pandas() # Ensure expected columns exist for col in COLUMNS: if col not in df.columns: df[col] = "" # Clean up df = df.fillna("") if "effectiveness_score" in df.columns: df["effectiveness_score"] = pd.to_numeric(df["effectiveness_score"], errors="coerce").fillna(0) _cache[lang] = df except Exception as e: print(f"Error loading {lang} dataset: {e}") _cache[lang] = pd.DataFrame(columns=COLUMNS) return _cache[lang] # Pre-load both datasets at startup for _lang in DATASETS: load_data(_lang) # --------------------------------------------------------------------------- # UI labels per language # --------------------------------------------------------------------------- LABELS = { "FR": { "title": "Explorateur de Prompt Engineering", "subtitle": "Explorez les techniques de prompt engineering en francais et en anglais", "tab_techniques": "Techniques", "tab_details": "Details", "tab_qa": "Questions / Reponses", "tab_stats": "Statistiques", "search": "Rechercher...", "category_filter": "Filtrer par categorie", "select_technique": "Selectionner une technique", "example_prompt": "Exemple de prompt", "example_output": "Exemple de sortie", "implementation": "Implementation", "description": "Description", "use_case": "Cas d'utilisation", "when_to_use": "Quand utiliser", "model_compat": "Compatibilite modele", "difficulty": "Difficulte", "perf_impact": "Impact performance", "effectiveness": "Score d'efficacite", "select_qa": "Selectionner une question", "answer": "Reponse", "chart_category": "Techniques par categorie", "chart_difficulty": "Techniques par difficulte", "chart_effectiveness": "Score d'efficacite par technique", "no_data": "Aucune donnee disponible.", "all": "toutes", }, "EN": { "title": "Prompt Engineering Explorer", "subtitle": "Explore prompt engineering techniques in French and English", "tab_techniques": "Techniques", "tab_details": "Details", "tab_qa": "Q & A", "tab_stats": "Statistics", "search": "Search...", "category_filter": "Filter by category", "select_technique": "Select a technique", "example_prompt": "Example Prompt", "example_output": "Example Output", "implementation": "Implementation", "description": "Description", "use_case": "Use Case", "when_to_use": "When to Use", "model_compat": "Model Compatibility", "difficulty": "Difficulty", "perf_impact": "Performance Impact", "effectiveness": "Effectiveness Score", "select_qa": "Select a question", "answer": "Answer", "chart_category": "Techniques by Category", "chart_difficulty": "Techniques by Difficulty", "chart_effectiveness": "Effectiveness Score by Technique", "no_data": "No data available.", "all": "all", }, } # --------------------------------------------------------------------------- # Helper functions # --------------------------------------------------------------------------- TABLE_COLS = ["name", "technique", "category", "difficulty", "effectiveness_score", "performance_impact"] def filter_techniques(lang, search_text, category): df = load_data(lang) if df.empty: return pd.DataFrame(columns=TABLE_COLS) filtered = df.copy() if category and category not in ("all", "toutes"): filtered = filtered[filtered["category"].str.lower() == category.lower()] if search_text: mask = False for col in ["name", "technique", "description", "category"]: mask = mask | filtered[col].astype(str).str.contains(search_text, case=False, na=False) filtered = filtered[mask] display_cols = [c for c in TABLE_COLS if c in filtered.columns] return filtered[display_cols].reset_index(drop=True) def get_technique_names(lang): df = load_data(lang) if df.empty: return [] names = df["name"].dropna().unique().tolist() return sorted(names) if names else [] def get_technique_detail(lang, name): df = load_data(lang) if df.empty or not name: return "", "", "", "", "", "", "", "", "" row = df[df["name"] == name] if row.empty: return "", "", "", "", "", "", "", "", "" r = row.iloc[0] return ( str(r.get("description", "")), str(r.get("example_prompt", "")), str(r.get("example_output", "")), str(r.get("implementation", "")), str(r.get("use_case", "")), str(r.get("when_to_use", "")), str(r.get("model_compatibility", "")), str(r.get("difficulty", "")), str(r.get("performance_impact", "")), ) def get_qa_list(lang): df = load_data(lang) if df.empty: return [] questions = df[df["question"].astype(str).str.strip() != ""]["question"].tolist() return questions def get_answer(lang, question): df = load_data(lang) if df.empty or not question: return "" row = df[df["question"] == question] if row.empty: return "" return str(row.iloc[0].get("answer", "")) def build_category_chart(lang): df = load_data(lang) L = LABELS[lang] if df.empty: fig = go.Figure() fig.update_layout(title=L["chart_category"]) return fig counts = df["category"].value_counts().reset_index() counts.columns = ["category", "count"] fig = px.bar(counts, x="category", y="count", color="category", title=L["chart_category"], color_discrete_sequence=px.colors.qualitative.Set2) fig.update_layout(template="plotly_white", showlegend=False) return fig def build_difficulty_chart(lang): df = load_data(lang) L = LABELS[lang] if df.empty: fig = go.Figure() fig.update_layout(title=L["chart_difficulty"]) return fig counts = df["difficulty"].value_counts().reset_index() counts.columns = ["difficulty", "count"] fig = px.pie(counts, names="difficulty", values="count", title=L["chart_difficulty"], color_discrete_sequence=px.colors.qualitative.Pastel) fig.update_layout(template="plotly_white") return fig def build_effectiveness_chart(lang): df = load_data(lang) L = LABELS[lang] if df.empty: fig = go.Figure() fig.update_layout(title=L["chart_effectiveness"]) return fig subset = df[df["effectiveness_score"] > 0][["name", "effectiveness_score"]].drop_duplicates() subset = subset.sort_values("effectiveness_score", ascending=True).tail(20) fig = px.bar(subset, y="name", x="effectiveness_score", orientation="h", title=L["chart_effectiveness"], color="effectiveness_score", color_continuous_scale="Viridis") fig.update_layout(template="plotly_white", yaxis_title="", xaxis_title="Score", height=600) return fig # --------------------------------------------------------------------------- # Footer HTML # --------------------------------------------------------------------------- FOOTER_HTML = """
Prompt Engineering Explorer — Built by AYI-NEDJIMI Consultants
Explore prompt engineering techniques in French and English