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

Website LinkedIn GitHub X / Twitter

""" # --------------------------------------------------------------------------- # Build the Gradio app # --------------------------------------------------------------------------- theme = gr.themes.Soft( primary_hue="indigo", secondary_hue="purple", font=[gr.themes.GoogleFont("Inter"), "ui-sans-serif", "system-ui", "sans-serif"], ) with gr.Blocks(theme=theme, title="Prompt Engineering Explorer") as demo: # State for current language lang_state = gr.State("EN") # Header gr.Markdown( """

Prompt Engineering Explorer

Explore prompt engineering techniques in French and English

""" ) # Language toggle with gr.Row(): lang_toggle = gr.Radio( choices=["EN", "FR"], value="EN", label="Language / Langue", interactive=True, ) # ---- Tab: Techniques ---- with gr.Tabs() as tabs: with gr.Tab("Techniques", id="techniques"): with gr.Row(): search_box = gr.Textbox(label="Search...", placeholder="Search techniques...", scale=3) cat_filter = gr.Dropdown( choices=CATEGORIES, value="all", label="Filter by category", scale=1, ) techniques_table = gr.Dataframe( value=filter_techniques("EN", "", "all"), label="Techniques", interactive=False, wrap=True, ) # ---- Tab: Details ---- with gr.Tab("Details", id="details"): technique_selector = gr.Dropdown( choices=get_technique_names("EN"), label="Select a technique", interactive=True, ) detail_description = gr.Textbox(label="Description", lines=3, interactive=False) with gr.Row(): detail_prompt = gr.Textbox(label="Example Prompt", lines=6, interactive=False) detail_output = gr.Textbox(label="Example Output", lines=6, interactive=False) detail_implementation = gr.Textbox(label="Implementation", lines=6, interactive=False) with gr.Row(): detail_usecase = gr.Textbox(label="Use Case", lines=2, interactive=False) detail_when = gr.Textbox(label="When to Use", lines=2, interactive=False) with gr.Row(): detail_compat = gr.Textbox(label="Model Compatibility", lines=1, interactive=False) detail_difficulty = gr.Textbox(label="Difficulty", lines=1, interactive=False) detail_perf = gr.Textbox(label="Performance Impact", lines=1, interactive=False) # ---- Tab: Q&A ---- with gr.Tab("Q & A", id="qa"): qa_selector = gr.Dropdown( choices=get_qa_list("EN"), label="Select a question", interactive=True, ) qa_answer = gr.Textbox(label="Answer", lines=8, interactive=False) # ---- Tab: Statistics ---- with gr.Tab("Statistics", id="stats"): with gr.Row(): chart_cat = gr.Plot(label="By Category") chart_diff = gr.Plot(label="By Difficulty") chart_eff = gr.Plot(label="Effectiveness") # Footer gr.HTML(FOOTER_HTML) # ------------------------------------------------------------------ # Event handlers # ------------------------------------------------------------------ def on_lang_change(lang): df_table = filter_techniques(lang, "", "all") names = get_technique_names(lang) questions = get_qa_list(lang) c1 = build_category_chart(lang) c2 = build_difficulty_chart(lang) c3 = build_effectiveness_chart(lang) return ( lang, # lang_state df_table, # techniques_table gr.update(choices=names, value=None), # technique_selector gr.update(choices=questions, value=None), # qa_selector "", "", "", "", "", "", "", "", "", # detail fields "", # qa_answer c1, c2, c3, # charts ) lang_toggle.change( fn=on_lang_change, inputs=[lang_toggle], outputs=[ lang_state, techniques_table, technique_selector, qa_selector, detail_description, detail_prompt, detail_output, detail_implementation, detail_usecase, detail_when, detail_compat, detail_difficulty, detail_perf, qa_answer, chart_cat, chart_diff, chart_eff, ], ) # Techniques search / filter def on_search(lang, text, cat): return filter_techniques(lang, text, cat) search_box.change(fn=on_search, inputs=[lang_state, search_box, cat_filter], outputs=[techniques_table]) cat_filter.change(fn=on_search, inputs=[lang_state, search_box, cat_filter], outputs=[techniques_table]) # Details def on_technique_select(lang, name): return get_technique_detail(lang, name) technique_selector.change( fn=on_technique_select, inputs=[lang_state, technique_selector], outputs=[ detail_description, detail_prompt, detail_output, detail_implementation, detail_usecase, detail_when, detail_compat, detail_difficulty, detail_perf, ], ) # Q&A qa_selector.change( fn=lambda lang, q: get_answer(lang, q), inputs=[lang_state, qa_selector], outputs=[qa_answer], ) # Load stats on app start demo.load( fn=lambda: (build_category_chart("EN"), build_difficulty_chart("EN"), build_effectiveness_chart("EN")), outputs=[chart_cat, chart_diff, chart_eff], ) # --------------------------------------------------------------------------- if __name__ == "__main__": demo.launch()