File size: 7,345 Bytes
043c791
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Exploratory Data Analysis (EDA) module for the awesome-chatgpt-prompts dataset.
Generates visualizations for dataset exploration.
"""

import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import pandas as pd
import numpy as np
from datasets import load_dataset
from collections import Counter
import re

# Set style for matplotlib
plt.style.use('seaborn-v0_8-darkgrid')
sns.set_palette("husl")

# Dataset configuration
DATASET_NAME = "fka/awesome-chatgpt-prompts"


def load_prompts_dataset() -> pd.DataFrame:
    """
    Load the awesome-chatgpt-prompts dataset from HuggingFace.
    
    Returns:
        DataFrame with the prompts data
    """
    dataset = load_dataset(DATASET_NAME, split="train")
    df = dataset.to_pandas()
    
    # Add computed columns for analysis
    df['prompt_length'] = df['prompt'].apply(len)
    df['word_count'] = df['prompt'].apply(lambda x: len(x.split()))
    df['sentence_count'] = df['prompt'].apply(lambda x: len(re.split(r'[.!?]+', x)))
    
    return df


def create_prompt_length_histogram() -> go.Figure:
    """
    Create an interactive histogram of prompt lengths.
    
    Returns:
        Plotly Figure object
    """
    df = load_prompts_dataset()
    
    fig = px.histogram(
        df,
        x='prompt_length',
        nbins=50,
        title='πŸ“ Distribution of Prompt Lengths (Characters)',
        labels={'prompt_length': 'Prompt Length (characters)', 'count': 'Frequency'},
        color_discrete_sequence=['#667eea']
    )
    
    fig.update_layout(
        template='plotly_dark',
        title_font_size=20,
        title_x=0.5,
        showlegend=False,
        xaxis_title="Character Count",
        yaxis_title="Number of Prompts"
    )
    
    # Add mean line
    mean_length = df['prompt_length'].mean()
    fig.add_vline(
        x=mean_length,
        line_dash="dash",
        line_color="#ff6b6b",
        annotation_text=f"Mean: {mean_length:.0f}",
        annotation_position="top"
    )
    
    return fig


def create_word_count_boxplot() -> go.Figure:
    """
    Create a boxplot showing word count distribution.
    
    Returns:
        Plotly Figure object
    """
    df = load_prompts_dataset()
    
    fig = go.Figure()
    
    fig.add_trace(go.Box(
        y=df['word_count'],
        name='Word Count',
        marker_color='#764ba2',
        boxmean='sd'
    ))
    
    fig.update_layout(
        title='πŸ“Š Word Count Distribution',
        template='plotly_dark',
        title_font_size=20,
        title_x=0.5,
        yaxis_title="Words per Prompt",
        showlegend=False
    )
    
    return fig


def create_top_words_chart(top_n: int = 20) -> go.Figure:
    """
    Create a bar chart of the most common words in prompts.
    
    Args:
        top_n: Number of top words to display
        
    Returns:
        Plotly Figure object
    """
    df = load_prompts_dataset()
    
    # Tokenize and count words (excluding common stop words)
    stop_words = {
        'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for',
        'of', 'with', 'by', 'from', 'as', 'is', 'was', 'are', 'were', 'been',
        'be', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could',
        'should', 'may', 'might', 'must', 'shall', 'can', 'need', 'dare', 'ought',
        'used', 'i', 'you', 'he', 'she', 'it', 'we', 'they', 'what', 'which',
        'who', 'when', 'where', 'why', 'how', 'all', 'each', 'every', 'both',
        'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not',
        'only', 'own', 'same', 'so', 'than', 'too', 'very', 'just', 'also',
        'your', 'my', 'this', 'that', 'these', 'those', 'me', 'him', 'her',
        'us', 'them', 'if', 'then', 'else', 'while', 'about', 'into', 'through',
        'during', 'before', 'after', 'above', 'below', 'between', 'any', 'their'
    }
    
    all_words = []
    for prompt in df['prompt']:
        words = re.findall(r'\b[a-zA-Z]+\b', prompt.lower())
        all_words.extend([w for w in words if w not in stop_words and len(w) > 2])
    
    word_counts = Counter(all_words).most_common(top_n)
    words, counts = zip(*word_counts)
    
    fig = go.Figure(go.Bar(
        x=list(counts)[::-1],
        y=list(words)[::-1],
        orientation='h',
        marker=dict(
            color=list(range(top_n)),
            colorscale='Viridis'
        )
    ))
    
    fig.update_layout(
        title=f'πŸ”€ Top {top_n} Most Common Words',
        template='plotly_dark',
        title_font_size=20,
        title_x=0.5,
        xaxis_title="Frequency",
        yaxis_title="Word",
        height=600
    )
    
    return fig


def create_length_vs_words_scatter() -> go.Figure:
    """
    Create a scatter plot showing relationship between length and word count.
    
    Returns:
        Plotly Figure object
    """
    df = load_prompts_dataset()
    
    fig = px.scatter(
        df,
        x='word_count',
        y='prompt_length',
        title='πŸ“ˆ Prompt Length vs Word Count',
        labels={
            'word_count': 'Word Count',
            'prompt_length': 'Character Length'
        },
        color='sentence_count',
        color_continuous_scale='Plasma',
        hover_data=['act']
    )
    
    fig.update_layout(
        template='plotly_dark',
        title_font_size=20,
        title_x=0.5
    )
    
    return fig


def create_summary_stats() -> pd.DataFrame:
    """
    Create a summary statistics table for the dataset.
    
    Returns:
        DataFrame with summary statistics
    """
    df = load_prompts_dataset()
    
    stats = {
        'Metric': [
            'Total Prompts',
            'Average Length (chars)',
            'Average Word Count',
            'Max Length (chars)',
            'Min Length (chars)',
            'Median Word Count'
        ],
        'Value': [
            len(df),
            f"{df['prompt_length'].mean():.0f}",
            f"{df['word_count'].mean():.1f}",
            df['prompt_length'].max(),
            df['prompt_length'].min(),
            f"{df['word_count'].median():.0f}"
        ]
    }
    
    return pd.DataFrame(stats)


def create_category_distribution() -> go.Figure:
    """
    Create a pie chart showing distribution of prompt categories (acts).
    
    Returns:
        Plotly Figure object
    """
    df = load_prompts_dataset()
    
    # Get top 10 categories
    top_acts = df['act'].value_counts().head(10)
    
    fig = go.Figure(go.Pie(
        labels=top_acts.index,
        values=top_acts.values,
        hole=0.4,
        marker=dict(colors=px.colors.qualitative.Set3)
    ))
    
    fig.update_layout(
        title='🎭 Top 10 Prompt Categories (Acts)',
        template='plotly_dark',
        title_font_size=20,
        title_x=0.5
    )
    
    return fig


def get_all_eda_figures() -> dict:
    """
    Generate all EDA figures at once.
    
    Returns:
        Dictionary containing all figure objects
    """
    return {
        'prompt_length': create_prompt_length_histogram(),
        'word_count': create_word_count_boxplot(),
        'top_words': create_top_words_chart(),
        'scatter': create_length_vs_words_scatter(),
        'categories': create_category_distribution()
    }