Spaces:
Build error
Build error
File size: 43,632 Bytes
e5653cf 544d276 e5653cf 544d276 e5653cf 544d276 e5653cf 6a57d27 e5653cf 6a57d27 e5653cf 6a57d27 e5653cf 544d276 e5653cf 6a57d27 d11acd4 544d276 d11acd4 544d276 d11acd4 e5653cf d11acd4 e5653cf fcf9a0e e5653cf fcf9a0e d11acd4 3f0de11 d11acd4 3f0de11 e5653cf 61cca18 d11acd4 3f0de11 d11acd4 61cca18 6a57d27 3f0de11 6a57d27 e5653cf 6a57d27 3f0de11 e5653cf fcf9a0e 3f0de11 e5653cf 3f0de11 f59e016 3f0de11 f59e016 3f0de11 f59e016 3f0de11 527b51e f59e016 3f0de11 f59e016 3f0de11 f59e016 527b51e 3f0de11 527b51e f59e016 3f0de11 f59e016 3f0de11 f59e016 3f0de11 f59e016 3f0de11 f59e016 527b51e e5653cf 44f3e7a e5653cf 6a57d27 527b51e 6a57d27 527b51e 6a57d27 527b51e 6a57d27 fcf9a0e 6a57d27 d11acd4 e5653cf e871326 e5653cf e871326 e5653cf fcf9a0e bd81c6e fcf9a0e 527b51e e871326 6a57d27 e871326 527b51e e871326 527b51e d11acd4 e871326 527b51e e5653cf 6a57d27 e5653cf e871326 527b51e e871326 527b51e e5653cf e871326 2a7cf04 f59e016 e5653cf fcf9a0e e5653cf 527b51e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 | import gradio as gr
import random
import pandas as pd
import numpy as np
import os
from sample_data import generate_sales_data, generate_sql_query
from additional_data import generate_customer_data, generate_product_data, generate_website_analytics, generate_marketing_campaign_data, generate_ml_datasets
# Check if sample data exists, if not generate it
if not os.path.exists('sample_sales_data.csv'):
sample_df = generate_sales_data(500) # Generate a smaller dataset for demo
sample_df.to_csv('sample_sales_data.csv', index=False)
# Check if additional data exists, if not generate it
if not os.path.exists('sample_customer_data.csv'):
# Generate and save all additional datasets
customer_df = generate_customer_data(300)
product_df = generate_product_data(150)
web_analytics_df = generate_website_analytics(300)
campaigns_df = generate_marketing_campaign_data(100)
ml_datasets = generate_ml_datasets()
# Save to CSV
customer_df.to_csv('sample_customer_data.csv', index=False)
product_df.to_csv('sample_product_data.csv', index=False)
web_analytics_df.to_csv('sample_web_analytics.csv', index=False)
campaigns_df.to_csv('sample_marketing_campaigns.csv', index=False)
# Save ML datasets
for name, dataset in ml_datasets.items():
dataset.to_csv(f'sample_{name}_data.csv', index=False)
# Load sample data
sample_sales_data = pd.read_csv('sample_sales_data.csv')
sample_customer_data = pd.read_csv('sample_customer_data.csv')
sample_product_data = pd.read_csv('sample_product_data.csv')
sample_web_analytics = pd.read_csv('sample_web_analytics.csv')
sample_campaigns = pd.read_csv('sample_marketing_campaigns.csv')
# Load ML datasets if they exist
ml_datasets = {}
for name in ['iris', 'wine', 'diabetes']:
file_path = f'sample_{name}_data.csv'
if os.path.exists(file_path):
ml_datasets[name] = pd.read_csv(file_path)
sample_sql_query = generate_sql_query()
# Create relationships between tables for relational queries
# Add customer info to sales data for a sample of orders
customer_ids = sample_customer_data['CustomerID'].sample(len(sample_sales_data)).values
sample_sales_data['CustomerID'] = customer_ids
# Add product info to sales data
product_ids = sample_product_data['ProductID'].sample(len(sample_sales_data), replace=True).values
sample_sales_data['ProductID'] = product_ids
# Connect products to campaigns for marketing analysis
product_category_map = dict(zip(sample_product_data['ProductID'], sample_product_data['Category']))
sample_campaigns['TargetCategory'] = sample_campaigns['CampaignID'].apply(
lambda x: random.choice(list(product_category_map.values()))
)
# Sample relational scenarios with multiple tables
relational_scenarios = [
{
"title": "Customer Purchase Analysis",
"description": "You have customer data and sales transaction data. How would you prompt AI to analyze customer purchasing patterns and segment customers based on their buying behavior?",
"level": "Intermediate",
"category": "Analytics",
"tables": ["customer_data", "sales_data"],
"ideal_prompt": "Analyze the relationship between customer profiles and their purchasing patterns by joining these datasets. Please: 1) Identify which customer segments (by age group and region) have the highest average order value, 2) Determine if there's correlation between customer income level and purchase frequency, 3) Find which product categories are most popular among different customer segments, and 4) Recommend targeted marketing approaches based on these insights.",
"answer": "An effective analysis would show that income levels positively correlate with average order value but not necessarily purchase frequency. VIP customers from Western regions typically have the highest lifetime value. Electronics are popular among higher-income groups, while Clothing has broad appeal across segments.",
"sample_data": {
"customer_data": sample_customer_data.head(10).to_html(),
"sales_data": sample_sales_data.head(10).to_html()
},
},
{
"title": "Product Performance and Inventory Optimization",
"description": "You have product data and sales transaction data. How would you prompt AI to identify best-selling products and optimize inventory levels?",
"level": "Advanced",
"category": "Supply Chain",
"tables": ["product_data", "sales_data"],
"ideal_prompt": "Analyze the product and sales data to optimize inventory management. Please: 1) Calculate the sales velocity for each product category over the past 3 months, 2) Identify products with inventory levels below their reorder point but high sales velocity, 3) Find products with excess inventory relative to their sales velocity, and 4) Create a prioritized restocking list with recommended order quantities based on historical sales patterns.",
"answer": "Analysis should identify Electronics and Home categories as having the highest sales velocity. Several products in the Electronics category are below reorder points and require immediate restocking. Clothing items have the most excess inventory. The optimal restocking strategy uses a tiered approach based on category-specific demand patterns.",
"sample_data": {
"product_data": sample_product_data.head(10).to_html(),
"sales_data": sample_sales_data.head(10).to_html()
},
},
{
"title": "Marketing Campaign ROI and Customer Response",
"description": "You have marketing campaign data, customer profiles, and sales data. How would you prompt AI to evaluate campaign effectiveness across different customer segments?",
"level": "Advanced",
"category": "Marketing",
"tables": ["campaign_data", "customer_data", "sales_data"],
"ideal_prompt": "Analyze how different marketing campaigns perform across customer segments using these three datasets. Please: 1) Join the datasets to connect campaigns to customer segments and resulting sales, 2) Calculate ROI by campaign type and customer segment, 3) Identify which types of customers respond best to which campaign types, 4) Analyze if certain product categories perform better with specific campaign types, and 5) Recommend an optimal marketing channel mix for each major customer segment.",
"answer": "Email campaigns show the highest ROI for existing VIP customers (28% higher than average), while Social campaigns are most effective for new customers. Search campaigns drive the most Electronics sales, and Video campaigns perform best for higher-priced items. The optimal channel mix varies by segment, with a 40/30/20/10 split between Email/Social/Search/Display for VIP customers.",
"sample_data": {
"campaign_data": sample_campaigns.head(10).to_html(),
"customer_data": sample_customer_data.head(10).to_html(),
"sales_data": sample_sales_data.head(10).to_html()
},
},
{
"title": "Website Analytics and Sales Conversion Patterns",
"description": "You have website analytics data and sales transaction data. How would you prompt AI to analyze the customer journey from website visits to purchases?",
"level": "Advanced",
"category": "Web Analytics",
"tables": ["web_analytics", "sales_data"],
"ideal_prompt": "Analyze the customer journey from website behavior to completed purchases using these datasets. Please: 1) Identify which traffic sources lead to the highest conversion rates, 2) Analyze the relationship between page types visited and product categories purchased, 3) Determine if there are device-specific patterns in purchasing behavior, 4) Calculate the average time from first visit to purchase completion across different customer segments, and 5) Recommend website optimization strategies based on these patterns.",
"answer": "Organic Search has the highest overall conversion rate (3.2%), while Email traffic converts best for returning customers (4.7%). Product pages for Electronics items receive the most visits but Category pages have better conversion rates. Mobile users show 20% lower average order values but 15% higher purchase frequency than Desktop users. The average time to purchase is 3.2 days, with significant variation by product category.",
"sample_data": {
"web_analytics": sample_web_analytics.head(10).to_html(),
"sales_data": sample_sales_data.head(10).to_html()
},
}
]
# Sample scenarios based on PRD
scenarios = [
{
"title": "Data Cleaning in Power BI",
"description": "You have a messy sales dataset with duplicates, missing values, and inconsistent date formats. How would you instruct AI to clean this data in Power BI?",
"level": "Beginner",
"category": "Power BI",
"ideal_prompt": "Clean this sales dataset in Power BI by: 1) Removing duplicate rows based on Order ID, 2) Filling missing values in the 'Revenue' column with the average, 3) Standardizing all date columns to MM/DD/YYYY format, and 4) Creating a calculated column for profit margin (Revenue - Cost)/Revenue.",
"answer": "First, I'd use the Remove Duplicates function on the OrderID column. Then, create a measure for the average Revenue and use it in a Column From Examples feature to fill missing values. For dates, I'd apply a custom transformation with Text.From(Date) to standardize the format. Finally, I'd add a calculated column with the formula: ProfitMargin = (Revenue - Cost) / Revenue.",
"sample_data": sample_sales_data.head(10).to_html(),
},
{
"title": "SQL Query Optimization",
"description": "Your dashboard's SQL query is taking too long to execute. The query joins multiple tables and uses several subqueries. How would you ask AI to optimize it?",
"level": "Intermediate",
"category": "SQL",
"ideal_prompt": "Optimize this SQL query that's running slowly: [paste query]. Specifically: 1) Check for missing indexes on join columns, 2) Rewrite any nested subqueries as CTEs, 3) Identify unnecessary JOINs, and 4) Suggest a query plan that would reduce execution time.",
"answer": "The query can be optimized by: 1) Adding indexes on customer_id, order_id columns across all tables, 2) The subquery calculating items_per_order should be converted to a CTE, 3) The LEFT JOIN to returns table is unnecessary unless filtering on return data, 4) Move the blacklist filtering earlier in the query logic, and 5) Add WHERE clauses before joining to large tables to reduce the working set size.",
"sample_data": f"<pre>{sample_sql_query}</pre>",
},
{
"title": "Customer Segmentation Analysis",
"description": "You need to segment customers based on their purchasing behavior, demographics, and value. How would you prompt AI to help create meaningful customer segments?",
"level": "Intermediate",
"category": "Analytics",
"ideal_prompt": "Analyze this customer dataset to create actionable segments. Please: 1) Identify key variables for segmentation (e.g., purchase frequency, total spent, recency), 2) Suggest appropriate clustering methods (e.g., K-means, hierarchical), 3) Recommend optimal number of segments, and 4) Describe characteristics of each segment with marketing recommendations.",
"sample_data": sample_customer_data.head(10).to_html(),
},
{
"title": "Product Inventory Optimization",
"description": "Your e-commerce store has inventory management challenges with some products frequently out of stock while others have excess inventory. How would you ask AI to help optimize your inventory levels?",
"level": "Advanced",
"category": "Supply Chain",
"ideal_prompt": "Analyze this product inventory dataset to optimize stock levels. Please: 1) Calculate optimal reorder points based on historical sales velocity, 2) Identify products with excess inventory that should be discounted, 3) Flag products frequently out of stock that need higher safety stock, and 4) Create a prioritized list of inventory actions needed.",
"sample_data": sample_product_data.head(10).to_html(),
},
{
"title": "Website Analytics Dashboard",
"description": "You have website analytics data and need to create an executive dashboard to track key performance indicators. How would you instruct AI to help design and implement this dashboard?",
"level": "Intermediate",
"category": "Web Analytics",
"ideal_prompt": "Help me design a website analytics dashboard in [tool] using this dataset. Include: 1) Key metrics to track (visits, bounce rate, conversion rate), 2) Trend charts showing performance over time, 3) Segment analysis by device type and traffic source, 4) Anomaly detection for unusual traffic patterns, and 5) A clear layout with the most important KPIs prominently displayed.",
"sample_data": sample_web_analytics.head(10).to_html(),
},
{
"title": "Marketing Campaign ROI Analysis",
"description": "You've run multiple marketing campaigns across different channels and need to analyze their performance and ROI. How would you prompt AI to help with this analysis?",
"level": "Advanced",
"category": "Marketing",
"ideal_prompt": "Analyze the ROI and effectiveness of these marketing campaigns. Please: 1) Calculate and compare key metrics (CTR, conversion rate, CPA) across campaign types, 2) Identify the highest and lowest performing campaigns based on ROI, 3) Analyze which audience segments responded best to which campaign types, 4) Recommend optimal budget allocation for future campaigns based on performance data.",
"sample_data": sample_campaigns.head(10).to_html(),
},
{
"title": "Pandas DataFrame Transformation",
"description": "You need to transform a customer transaction dataset for a churn analysis. The data requires grouping, aggregation, and feature creation. How would you prompt AI to help?",
"level": "Advanced",
"category": "Python",
"ideal_prompt": "Transform this pandas DataFrame of customer transactions for churn analysis. Steps needed: 1) Group transactions by customer_id, 2) Create features for average purchase value, frequency, and recency, 3) Flag customers with declining purchase trends over the last 3 months, 4) Handle outliers using IQR method, and 5) Export the result as a clean CSV file.",
"sample_data": sample_sales_data.head(10).to_html(),
}
]
# Combining relational and regular scenarios
all_scenarios = relational_scenarios + scenarios
# AI-generated scenarios templates
ai_scenario_templates = [
{
"title_prefix": "Data Analysis with ",
"tools": ["Power BI", "Tableau", "Excel", "Python", "R", "SQL"],
"data_types": ["sales", "customer", "financial", "survey", "marketing", "operational"],
"problems": ["missing values", "outliers", "inconsistent formats", "duplicates", "aggregation needs"],
"level_options": ["Beginner", "Intermediate", "Advanced"]
},
{
"title_prefix": "Visualization in ",
"tools": ["Power BI", "Tableau", "Matplotlib", "Seaborn", "D3.js", "Excel"],
"data_types": ["time series", "categorical", "geospatial", "hierarchical", "correlation", "comparison"],
"problems": ["finding the right chart type", "color selection", "handling scale issues", "showing too many variables", "highlighting key insights"],
"level_options": ["Beginner", "Intermediate", "Advanced"]
},
{
"title_prefix": "Query Optimization for ",
"tools": ["SQL Server", "PostgreSQL", "MySQL", "BigQuery", "Snowflake", "DynamoDB"],
"data_types": ["relational", "document", "key-value", "graph", "time series", "data warehouse"],
"problems": ["slow joins", "inefficient filters", "missing indexes", "poor aggregation", "subquery complexity"],
"level_options": ["Beginner", "Intermediate", "Advanced"]
},
{
"title_prefix": "Machine Learning with ",
"tools": ["scikit-learn", "TensorFlow", "PyTorch", "KNIME", "RapidMiner", "AutoML"],
"data_types": ["structured", "unstructured", "image", "text", "time series", "categorical"],
"problems": ["feature selection", "model tuning", "overfitting", "imbalanced classes", "model interpretation"],
"level_options": ["Intermediate", "Advanced"]
},
{
"title_prefix": "Customer Analytics using ",
"tools": ["Python", "R", "Excel", "Power BI", "Tableau", "SQL"],
"data_types": ["purchase history", "demographics", "behavioral", "survey", "engagement", "loyalty"],
"problems": ["segmentation", "lifetime value calculation", "churn prediction", "next purchase prediction", "recommendation systems"],
"level_options": ["Beginner", "Intermediate", "Advanced"]
}
]
# Generate an AI scenario
def generate_ai_scenario():
template = random.choice(ai_scenario_templates)
tool = random.choice(template["tools"])
data_type = random.choice(template["data_types"])
problem = random.choice(template["problems"])
level = random.choice(template["level_options"])
title = f"{template['title_prefix']}{tool}"
description = f"You're working with {data_type} data and encountering issues with {problem}. " + \
f"How would you instruct an AI assistant to help you solve this problem using {tool}?"
ideal_prompt = f"Help me address {problem} in my {data_type} data using {tool}. " + \
f"Please provide a step-by-step approach, including how to identify the issue, " + \
f"resolve it efficiently, and verify the solution is working."
# Select appropriate sample data based on the scenario
if "customer" in data_type or "segmentation" in problem or "churn" in problem:
sample_data = sample_customer_data.head(10).to_html()
elif "product" in data_type or "inventory" in problem:
sample_data = sample_product_data.head(10).to_html()
elif "web" in data_type or "traffic" in problem or "visitor" in problem:
sample_data = sample_web_analytics.head(10).to_html()
elif "campaign" in data_type or "marketing" in data_type or "ROI" in problem:
sample_data = sample_campaigns.head(10).to_html()
elif "model" in problem or "machine learning" in tool.lower() or "classification" in problem:
# Use one of the ML datasets
if ml_datasets:
dataset_name = random.choice(list(ml_datasets.keys()))
sample_data = ml_datasets[dataset_name].head(10).to_html()
else:
sample_data = sample_sales_data.head(10).to_html()
else:
sample_data = sample_sales_data.head(10).to_html() if random.random() > 0.5 else f"<pre>{sample_sql_query}</pre>"
return {
"title": title,
"description": description,
"level": level,
"category": tool,
"ideal_prompt": ideal_prompt,
"sample_data": sample_data
}
# Simulated user progress
user_progress = {
"completed_scenarios": 0,
"skill_level": "Beginner",
"badges": [],
"scores": []
}
# Leaderboard (simulated)
leaderboard = [
{"username": "data_ninja", "score": 4.8, "completed": 15},
{"username": "prompt_master", "score": 4.7, "completed": 12},
{"username": "ai_whisperer", "score": 4.5, "completed": 10},
{"username": "you", "score": 0, "completed": 0}
]
# Tips for effective prompting
prompt_tips = [
"Be specific about the exact steps you want the AI to perform",
"Mention the tools or libraries the AI should use (e.g., Power BI, pandas)",
"Specify how to handle edge cases like missing values or outliers",
"Include context about the data structure and your goal",
"Break complex tasks into numbered steps",
"Use clear, concise language rather than vague instructions",
"Provide examples when possible to clarify your expectations",
"Request explanations when solutions are complex",
"Ask for alternatives to compare different approaches",
"Define success criteria so the AI knows what a good result looks like"
]
# Display random tip
def get_random_tip():
tip = random.choice(prompt_tips)
return f"**Tip:** {tip}"
# Simulate AI evaluation of prompts
def evaluate_prompt(user_prompt, scenario):
"""Evaluate user's prompt quality"""
if not user_prompt.strip():
return {
"score": 0,
"feedback": "You haven't provided any prompt. Please try again."
}
# In a real app, this would use an LLM to evaluate
# For demo, we're using a simple heuristic
# Check prompt length (too short is likely insufficient)
length_score = min(len(user_prompt.split()) / 30, 1) * 5
# Check for keywords related to the scenario
keywords = scenario["ideal_prompt"].lower().split()
keyword_matches = sum(1 for word in user_prompt.lower().split() if word in keywords)
keyword_score = min(keyword_matches / 10, 1) * 5
# Calculate overall score (1-5)
final_score = round((length_score + keyword_score) / 2, 1)
final_score = max(1, min(5, final_score)) # Clamp between 1-5
# Generate feedback
if final_score < 2:
feedback = "Your prompt needs significant improvement. It lacks specificity and clear instructions."
elif final_score < 3:
feedback = "Your prompt is basic but could use more detail. Try specifying exact steps and parameters."
elif final_score < 4:
feedback = "Good prompt, but consider adding more context about the specific tools and methods to use."
elif final_score < 4.5:
feedback = "Very good prompt! Minor improvements could include specifying edge cases."
else:
feedback = "Excellent prompt! Clear, specific, and well-structured."
# Update user progress
user_progress["scores"].append(final_score)
if len(user_progress["scores"]) >= 3 and sum(user_progress["scores"][-3:]) / 3 > 4:
if "Prompt Crafting Expert" not in user_progress["badges"]:
user_progress["badges"].append("Prompt Crafting Expert")
user_progress["completed_scenarios"] += 1
if user_progress["completed_scenarios"] >= 5 and user_progress["skill_level"] == "Beginner":
user_progress["skill_level"] = "Intermediate"
elif user_progress["completed_scenarios"] >= 10 and user_progress["skill_level"] == "Intermediate":
user_progress["skill_level"] = "Advanced"
# Update leaderboard entry
leaderboard[3]["score"] = sum(user_progress["scores"]) / max(1, len(user_progress["scores"]))
leaderboard[3]["completed"] = user_progress["completed_scenarios"]
leaderboard.sort(key=lambda x: x["score"], reverse=True)
return {
"score": final_score,
"feedback": feedback
}
# Show user profile
def show_profile():
avg_score = sum(user_progress["scores"]) / max(1, len(user_progress["scores"]))
response = f"### User Profile\n\n"
response += f"**Skill Level:** {user_progress['skill_level']}\n"
response += f"**Scenarios Completed:** {user_progress['completed_scenarios']}\n"
response += f"**Average Score:** {avg_score:.1f}/5\n"
if user_progress["badges"]:
response += f"**Badges Earned:** {', '.join(user_progress['badges'])}\n"
else:
response += "**Badges Earned:** None yet! Keep practicing to earn badges.\n"
response += "\n*Complete more scenarios with high scores to level up and earn badges!*"
return response
# Show leaderboard
def display_leaderboard():
lb_html = "<table width='100%'>"
lb_html += "<tr><th>Rank</th><th>User</th><th>Avg Score</th><th>Completed</th></tr>"
for i, entry in enumerate(leaderboard):
lb_html += f"<tr><td>{i+1}</td><td>{'<b>' if entry['username'] == 'you' else ''}{entry['username']}{'</b>' if entry['username'] == 'you' else ''}</td><td>{entry['score']:.1f}</td><td>{entry['completed']}</td></tr>"
lb_html += "</table>"
return lb_html
# Generate a new scenario (mix of predefined and AI-generated)
def get_new_scenario(use_ai_generated=None):
# 50% chance to get an AI-generated scenario if not specified
if use_ai_generated is None:
use_ai_generated = random.random() > 0.5
if use_ai_generated:
return load_ai_scenario()
else:
return load_regular_scenario()
# Use the same definition for get_ai_scenario but with the updated return types
def get_ai_scenario():
return load_ai_scenario()
# Use the same definition for get_relational_scenario
def get_relational_scenario():
return load_relational_scenario()
# Evaluate user input and provide feedback
def submit_prompt(prompt, title, description, level, category, answer_text):
try:
# First try to find in predefined scenarios
scenario = next((s for s in all_scenarios if s["title"] == title), None) if isinstance(all_scenarios, list) else None
# If not found, create a temporary scenario object from inputs
if scenario is None:
scenario = {
"title": title,
"description": description,
"level": level,
"category": category,
"ideal_prompt": description, # Use description as a fallback for keywords
"answer": answer_text
}
# Evaluate prompt
evaluation = evaluate_prompt(prompt, scenario)
# Build response
response = f"### Evaluation Results\n\n"
response += f"**Score: {evaluation['score']}/5**\n\n"
response += f"**Feedback:**\n{evaluation['feedback']}\n\n"
# Add progress information
response += f"### Your Progress\n\n"
response += f"**Skill Level:** {user_progress['skill_level']}\n"
response += f"**Scenarios Completed:** {user_progress['completed_scenarios']}\n"
if user_progress["badges"]:
response += f"**Badges Earned:** {', '.join(user_progress['badges'])}\n"
# Include the answer section
response += f"\n### Suggested Solution Approach\n\n"
response += f"{scenario['answer']}\n" if "answer" in scenario else "No specific solution provided for this scenario."
return response
except Exception as e:
# Hata durumunda basit bir yanıt ver
print(f"Error in submit_prompt: {str(e)}")
return f"### Evaluation\n\nYour prompt has been received but couldn't be evaluated due to an error.\n\nPlease try with another scenario."
# Helper function to update tables based on scenario type
def update_tables(tables, sample_data, answer):
try:
table_labels = ["sample_table1_label", "sample_table2_label", "sample_table3_label"]
table_elements = [sample_table1, sample_table2, sample_table3]
table_rows = [table_row, table_row2, table_row3]
# Hide all tables first
for row in table_rows:
row.visible = False
# Then show only the necessary tables
for i, table_name in enumerate(tables[:3]): # Limit to 3 tables
if i < len(tables):
table_rows[i].visible = True
table_labels[i].value = f"### {table_name.replace('_', ' ').title()}"
table_elements[i].value = sample_data.get(table_name, "No data available")
return [
table_rows[0].visible, table_rows[1].visible, table_rows[2].visible,
table_labels[0].value, table_labels[1].value, table_labels[2].value,
table_elements[0].value, table_elements[1].value, table_elements[2].value,
answer
]
except Exception as e:
print(f"Error in update_tables: {str(e)}")
# Hata durumunda varsayılan tabloları döndür
return [
True, False, False,
"### Sample Data", "", "",
"<table><tr><th>Data</th><th>Value</th></tr><tr><td>Sample</td><td>123</td></tr></table>",
"", "",
"Example answer"
]
def load_sample_data():
try:
# Birinci tabloyu yükle
if 'sample_customer_data' in globals() and hasattr(sample_customer_data, 'head'):
sample_html1 = sample_customer_data.head(5).to_html()
else:
sample_html1 = "<table><tr><th>CustomerID</th><th>Age</th></tr><tr><td>C0001</td><td>35</td></tr></table>"
# İkinci tabloyu yükle
if 'sample_sales_data' in globals() and hasattr(sample_sales_data, 'head'):
sample_html2 = sample_sales_data.head(5).to_html()
else:
sample_html2 = "<table><tr><th>OrderID</th><th>Amount</th></tr><tr><td>ORD-001</td><td>120.5</td></tr></table>"
return [
"### Müşteri Verileri",
sample_html1,
"### Satış Verileri",
sample_html2
]
except Exception as e:
# Hata durumunda basit bir örnek göster
print(f"Error in load_sample_data: {str(e)}")
return [
"### Örnek Veriler",
"<table><tr><th>CustomerID</th><th>Age</th></tr><tr><td>C0001</td><td>35</td></tr></table>",
"### Satış Verileri",
"<table><tr><th>OrderID</th><th>Amount</th></tr><tr><td>ORD-001</td><td>120.5</td></tr></table>"
]
def load_regular_scenario():
try:
# Varsayılan normal senaryo
default_scenario = {
"title": "Data Cleaning in Power BI",
"description": "Duplicates, missing values ve inconsistent date formatları içeren karışık bir satış veri setiniz var. Bu veriyi Power BI'da nasıl temizlersiniz?",
"level": "Beginner",
"category": "Power BI",
"sample_data": sample_sales_data.head(10).to_html() if 'sample_sales_data' in globals() and hasattr(sample_sales_data, 'head') else "<table><tr><th>Data</th><th>Value</th></tr><tr><td>Example</td><td>123</td></tr></table>",
"answer": "Power BI'da Remove Duplicates fonksiyonunu OrderID sütununa uygula, ortalama değerle Revenue sütunundaki eksik değerleri doldur, ve tarih sütunlarını MM/DD/YYYY formatına standardize et."
}
# Rastgele senaryo seçmeyi dene, başarısız olursa varsayılanı kullan
if 'scenarios' in globals() and isinstance(scenarios, list) and len(scenarios) > 0:
scenario = random.choice(scenarios)
else:
scenario = default_scenario
if not isinstance(scenario.get("sample_data", ""), str):
scenario["sample_data"] = default_scenario["sample_data"]
return (
scenario["title"],
scenario["description"],
scenario["level"],
scenario["category"],
True, False, False, # Only first table visible
"### Sample Data", "", "", # Only first table has header
scenario["sample_data"] if isinstance(scenario["sample_data"], str) else default_scenario["sample_data"],
"", "", # Empty second and third tables
scenario.get("answer", "No specific answer for this scenario.")
)
except Exception as e:
# Hata durumunda varsayılan senaryo göster
print(f"Error in load_regular_scenario: {str(e)}")
return (
"Data Analysis",
"Error loading scenario. Please try another option.",
"Beginner",
"Analytics",
True, False, False,
"### Sample Data", "", "",
"<table><tr><th>Data</th><th>Value</th></tr><tr><td>Example</td><td>123</td></tr></table>",
"", "",
"Example answer text here."
)
def load_relational_scenario():
try:
# Varsayılan ilişkisel senaryo - bu her zaman çalışacak
default_scenario = {
"title": "Customer Purchase Analysis",
"description": "Müşteri verileriniz ve satış işlem verileriniz var. Müşteri satın alma kalıplarını analiz etmek için nasıl bir yaklaşım önerirsiniz?",
"level": "Intermediate",
"category": "Analytics",
"sample_data": {
"customer_data": sample_customer_data.head(10).to_html() if 'sample_customer_data' in globals() and hasattr(sample_customer_data, 'head') else "<table><tr><th>CustomerID</th><th>Age</th></tr><tr><td>C0001</td><td>35</td></tr></table>",
"sales_data": sample_sales_data.head(10).to_html() if 'sample_sales_data' in globals() and hasattr(sample_sales_data, 'head') else "<table><tr><th>OrderID</th><th>Amount</th></tr><tr><td>ORD-001</td><td>120.5</td></tr></table>"
},
"answer": "Müşteri satın alma kalıplarını analiz etmek için RFM (Recency, Frequency, Monetary) analizi ile demografik özelliklere göre segmentasyon yapılabilir."
}
# Rastgele senaryo seçmeyi dene, başarısız olursa varsayılanı kullan
if 'relational_scenarios' in globals() and isinstance(relational_scenarios, list) and len(relational_scenarios) > 0:
scenario = random.choice(relational_scenarios)
else:
scenario = default_scenario
return (
scenario["title"],
scenario["description"],
scenario["level"],
scenario["category"],
True, True, False, # Table visibility
"### Customer Data", "### Sales Data", "", # Table headers
scenario["sample_data"].get("customer_data", default_scenario["sample_data"]["customer_data"]) if isinstance(scenario["sample_data"], dict) else default_scenario["sample_data"]["customer_data"],
scenario["sample_data"].get("sales_data", default_scenario["sample_data"]["sales_data"]) if isinstance(scenario["sample_data"], dict) else default_scenario["sample_data"]["sales_data"],
"", # Empty third table
scenario.get("answer", "Example answer text here.")
)
except Exception as e:
# Hata durumunda varsayılan senaryo göster
print(f"Error in load_relational_scenario: {str(e)}")
return (
"Relationship Analysis",
"Error loading scenario. Please try another option.",
"Intermediate",
"Analytics",
True, True, False,
"### Sample Data 1", "### Sample Data 2", "",
"<table><tr><th>CustomerID</th><th>Age</th></tr><tr><td>C0001</td><td>35</td></tr></table>",
"<table><tr><th>OrderID</th><th>Amount</th></tr><tr><td>ORD-001</td><td>120.5</td></tr></table>",
"",
"Example answer text here."
)
def load_ai_scenario():
try:
# Varsayılan başlık ve açıklama
default_title = "Data Analysis with Python"
default_description = "Veri setinizdeki eksik değerler için Python kullanarak nasıl bir çözüm önerirsiniz?"
default_category = "Python"
# Tablo verisi için güvenlik kontrolü
if 'sample_sales_data' in globals() and hasattr(sample_sales_data, 'head'):
sample_data = sample_sales_data.head(10).to_html()
else:
sample_data = "<table><tr><th>Data</th><th>Value</th></tr><tr><td>Example</td><td>123</td></tr></table>"
# AI template kullanılabilir mi kontrol et
use_templates = 'ai_scenario_templates' in globals() and isinstance(ai_scenario_templates, list) and len(ai_scenario_templates) > 0
if use_templates:
try:
template = random.choice(ai_scenario_templates)
tool = random.choice(template.get("tools", ["Python"]))
data_type = random.choice(template.get("data_types", ["sample"]))
problem = random.choice(template.get("problems", ["analysis"]))
title = f"{template.get('title_prefix', 'Analysis with ')}{tool}"
description = f"Bu {data_type} veri seti üzerinde {problem} problemini çözmek için {tool} kullanmanız gerekiyor."
category = tool
except Exception as template_error:
print(f"Error creating AI template: {str(template_error)}")
title = default_title
description = default_description
category = default_category
else:
title = default_title
description = default_description
category = default_category
return (
title,
description,
"Intermediate",
category,
True, False, False, # Sadece ilk tablo görünür
"### Sample Data", "", "",
sample_data, "", "",
"Bu senaryoda temel veri temizleme, analiz ve görselleştirme adımlarını takip edin."
)
except Exception as e:
# Hata durumunda varsayılan senaryo göster
print(f"Error in load_ai_scenario: {str(e)}")
return (
"AI Generated Scenario",
"Error creating AI scenario. Please try another option.",
"Intermediate",
"AI",
True, False, False,
"### Sample Data", "", "",
"<table><tr><th>Data</th><th>Value</th></tr><tr><td>Example</td><td>123</td></tr></table>",
"", "",
"Example answer text here."
)
# Build Gradio interface
with gr.Blocks(theme=gr.themes.Soft(), title="PromptMaster for Data Analytics") as app:
gr.Markdown("# PromptMaster for Data Analytics")
gr.Markdown("### Learn to craft effective AI prompts for data analysis tasks")
with gr.Tab("Practice Scenarios"):
# Scenario display
title_output = gr.Textbox(label="Scenario Title")
description_output = gr.Textbox(label="Problem Description", lines=4)
with gr.Row():
level_output = gr.Textbox(label="Difficulty Level")
category_output = gr.Textbox(label="Category")
# Sample data display
gr.Markdown("### Sample Data")
# Create placeholders for up to 3 tables, hidden by default
with gr.Row(visible=True) as table_row:
sample_table1_label = gr.Markdown("### Tablo 1")
sample_table1 = gr.HTML()
with gr.Row(visible=True) as table_row2:
sample_table2_label = gr.Markdown("### Tablo 2")
sample_table2 = gr.HTML()
with gr.Row(visible=False) as table_row3:
sample_table3_label = gr.Markdown("### Tablo 3")
sample_table3 = gr.HTML()
# Hidden answer field to store the model answer
answer_output = gr.Textbox(label="Model Answer", visible=False)
# Switch to toggle showing answers immediately
show_answer_toggle = gr.Checkbox(label="Show Answer Immediately", value=False)
# Prompt tip box
tip_output = gr.Markdown(label="Prompt Tip")
# User input
prompt_input = gr.Textbox(label="Your Prompt", lines=5, placeholder="Write your prompt here...")
# Buttons - sadece submit ve yeni ipucu butonları kaldı
with gr.Row():
submit_btn = gr.Button("Submit Prompt", variant="primary")
new_tip_btn = gr.Button("Get Prompt Tip")
new_scenario_btn = gr.Button("New Scenario")
# Results
evaluation_output = gr.Markdown(label="Evaluation Results")
# Modify the show_answer_toggle functionality
show_answer_toggle.change(
lambda show, answer: gr.update(value=f"### Model Solution\n\n{answer}") if show else gr.update(value=""),
inputs=[show_answer_toggle, answer_output],
outputs=[evaluation_output]
)
# Logic - butonların işlevselliği
new_tip_btn.click(
get_random_tip,
outputs=[tip_output]
)
# Yeni senaryo butonu tek bir işlev kullanır
new_scenario_btn.click(
load_sample_data,
outputs=[
sample_table1_label, sample_table1,
sample_table2_label, sample_table2
]
)
new_scenario_btn.click(
load_regular_scenario,
outputs=[
title_output, description_output, level_output, category_output,
table_row, table_row2, table_row3,
sample_table1_label, sample_table2_label, sample_table3_label,
sample_table1, sample_table2, sample_table3,
answer_output
]
)
submit_btn.click(
submit_prompt,
inputs=[prompt_input, title_output, description_output, level_output, category_output, answer_output],
outputs=[evaluation_output]
)
# Initialize with a scenario and tip
app.load(
load_regular_scenario,
outputs=[
title_output, description_output, level_output, category_output,
table_row, table_row2, table_row3,
sample_table1_label, sample_table2_label, sample_table3_label,
sample_table1, sample_table2, sample_table3,
answer_output
]
)
# Rastgele ipucu yükle
app.load(
get_random_tip,
outputs=[tip_output]
)
with gr.Tab("Your Profile"):
profile_btn = gr.Button("View Profile")
profile_output = gr.Markdown()
profile_btn.click(show_profile, outputs=[profile_output])
with gr.Tab("Leaderboard"):
leaderboard_output = gr.HTML()
refresh_lb_btn = gr.Button("Refresh Leaderboard")
refresh_lb_btn.click(display_leaderboard, outputs=[leaderboard_output])
# Initialize leaderboard
app.load(display_leaderboard, outputs=[leaderboard_output])
with gr.Tab("About"):
gr.Markdown("""
## About PromptMaster
PromptMaster is an AI-powered microlearning platform designed to help data professionals craft effective prompts for AI tools instead of writing manual code Designed by Mustafa Rakım Başar.
### How It Works
1. You'll be presented with real-world data scenarios
2. Craft a prompt as if you're instructing an AI to solve the problem
3. Get instant feedback on your prompt quality
4. Track your progress and earn badges as you improve
### Target Audience
- Data Analysts
- BI Developers
- Data Engineers
- Business Analysts
Made with ❤️ using Gradio and Hugging Face Spaces
""")
# Launch the app - Optimize for Hugging Face Spaces
if __name__ == "__main__":
app.launch(
server_name="0.0.0.0", # Bind to all network interfaces for HF Spaces
server_port=7860, # Default port for HF Spaces
share=False, # No need for sharing links
debug=False # Disable debug mode for production
) |