File size: 24,209 Bytes
dfbd641
 
 
 
 
 
eef1ed6
19d88f2
 
eef1ed6
dfbd641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef1ed6
 
19d88f2
 
 
eef1ed6
19d88f2
 
 
 
 
 
dfbd641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef1ed6
 
 
 
 
 
 
19d88f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfbd641
eef1ed6
dfbd641
 
eef1ed6
19d88f2
 
eef1ed6
dfbd641
 
 
19d88f2
dfbd641
 
 
19d88f2
dfbd641
19d88f2
dfbd641
 
 
 
eef1ed6
 
19d88f2
dfbd641
eef1ed6
 
dfbd641
19d88f2
 
dfbd641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef1ed6
 
dfbd641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef1ed6
dfbd641
 
 
 
eef1ed6
dfbd641
 
 
 
 
eef1ed6
 
 
dfbd641
eef1ed6
dfbd641
 
 
 
 
 
eef1ed6
 
 
 
19d88f2
eef1ed6
 
19d88f2
 
eef1ed6
 
19d88f2
eef1ed6
 
 
 
 
 
 
19d88f2
 
 
eef1ed6
 
 
 
 
 
19d88f2
eef1ed6
 
 
 
 
 
 
 
 
19d88f2
 
eef1ed6
 
 
 
 
 
 
19d88f2
eef1ed6
 
 
 
 
19d88f2
 
 
 
 
 
 
 
 
 
eef1ed6
 
19d88f2
 
 
eef1ed6
 
 
 
dfbd641
 
 
eef1ed6
dfbd641
 
 
 
 
 
 
 
 
 
 
eef1ed6
dfbd641
eef1ed6
06bf063
 
eef1ed6
 
 
 
 
 
19d88f2
dfbd641
 
 
 
eef1ed6
19d88f2
eef1ed6
 
 
19d88f2
eef1ed6
 
 
19d88f2
 
 
eef1ed6
 
 
19d88f2
 
dfbd641
 
19d88f2
 
eef1ed6
 
dfbd641
eef1ed6
 
 
19d88f2
 
 
 
 
 
 
eef1ed6
dfbd641
eef1ed6
dfbd641
 
 
 
 
 
 
eef1ed6
 
 
19d88f2
 
 
 
 
 
 
dfbd641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef1ed6
19d88f2
 
 
 
 
 
 
eef1ed6
 
 
19d88f2
dfbd641
 
19d88f2
 
 
 
 
 
 
 
dfbd641
 
eef1ed6
dfbd641
 
 
 
 
 
 
eef1ed6
dfbd641
 
eef1ed6
 
dfbd641
19d88f2
 
 
 
 
 
 
 
 
dfbd641
 
19d88f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef1ed6
 
 
 
 
 
 
 
 
 
 
 
 
 
dfbd641
 
 
 
 
 
 
eef1ed6
 
 
dfbd641
eef1ed6
dfbd641
 
 
 
 
 
 
 
 
 
 
 
 
 
eef1ed6
dfbd641
 
 
 
 
eef1ed6
19d88f2
 
eef1ed6
19d88f2
 
 
 
 
dfbd641
 
 
 
eef1ed6
 
 
 
 
 
dfbd641
 
 
 
eef1ed6
 
 
 
 
dfbd641
 
 
eef1ed6
 
dfbd641
 
 
 
eef1ed6
 
dfbd641
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eef1ed6
19d88f2
 
 
dfbd641
 
eef1ed6
 
dfbd641
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
import gradio as gr
import requests
from bs4 import BeautifulSoup
import os
import json
import logging
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from typing import Optional, List, Dict, Any

# ------------------------
# Configuration
# ------------------------
WORDLIFT_API_URL = "https://api.wordlift.io/content-evaluations"
WORDLIFT_API_KEY = os.getenv("WORDLIFT_API_KEY") # Get API key from environment variable

# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# ------------------------
# Custom CSS & Theme
# ------------------------

css = """
@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600;700&display=swap');
body {
    font-family: 'Open Sans', sans-serif !important;
}
.primary-btn {
    background-color: #3452db !important;
    color: white !important;
}
.primary-btn:hover {
    background-color: #2a41af !important;
}
.gradio-container {
    max-width: 1200px; /* Limit width for better readability */
    margin: auto;
}
.plot-container {
    min-height: 400px; /* Ensure plot area is visible */
    display: flex;
    justify-content: center; /* Center the plot */
    align-items: center; /* Center vertically if needed */
}
/* Specific style for the plot title to potentially reduce overlap */
.plot-container .gradio-html-title {
    text-align: center;
    width: 100%; /* Ensure title centers */
}

"""

theme = gr.themes.Soft(
    primary_hue=gr.themes.colors.Color(
        name="blue",
        c50="#eef1ff",
        c100="#e0e5ff",
        c200="#c3cbff",
        c300="#a5b2ff",
        c400="#8798ff",
        c500="#6a7eff",
        c600="#3452db",
        c700="#2a41af",
        c800="#1f3183",
        c900="#152156",
        c950="#0a102b",
    )
)

# ------------------------
# Content Fetching Logic
# ------------------------

def fetch_content_from_url(url: str, timeout: int = 15) -> str:
    """Fetches main text content from a URL."""
    logger.info(f"Fetching content from: {url}")
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
        }
        # Use stream=True and then process content to handle large files efficiently,
        # though BeautifulSoup will load it all eventually. Timeout is for connection.
        with requests.get(url, headers=headers, timeout=timeout, stream=True) as response:
             response.raise_for_status() # Raise an exception for bad status codes

             # Limit the amount of data read to avoid excessive memory usage
             max_bytes_to_read = 2 * 1024 * 1024 # 2MB limit for initial read
             # Read only up to max_bytes_to_read
             content_bytes = b''
             for chunk in response.iter_content(chunk_size=8192):
                 if not chunk:
                     break
                 content_bytes += chunk
                 if len(content_bytes) >= max_bytes_to_read:
                     logger.warning(f"Content for {url} exceeded {max_bytes_to_read} bytes, stopped reading.")
                     break

        # Use detect_encoding if possible, fallback to utf-8
        try:
            # Attempt to get encoding from headers or detect it
            encoding = requests.utils.get_encoding_from_headers(response.headers) or requests.utils.guess_json_utf(content_bytes)
            content = content_bytes.decode(encoding, errors='replace')
        except Exception as e:
            logger.warning(f"Could not detect encoding for {url}, falling back to utf-8: {e}")
            content = content_bytes.decode('utf-8', errors='replace')


        soup = BeautifulSoup(content, 'html.parser')

        # Attempt to find main content block
        # Prioritize more specific semantic tags
        # Added some common class names as fallback
        main_content = soup.find('article') or soup.find('main') or soup.find(class_=lambda x: x and ('content' in x.lower() or 'article' in x.lower() or 'post' in x.lower() or 'body' in x.lower()))


        if main_content:
             # Extract text from common text-containing tags within the main block
            text_elements = main_content.find_all(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'blockquote', 'figcaption', 'pre', 'code'])
            text = ' '.join([elem.get_text() for elem in text_elements])
        else:
            # Fallback to extracting text from body if no main block found
            text_elements = soup.body.find_all(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'blockquote', 'figcaption', 'pre', 'code'])
            text = ' '.join([elem.get_text() for elem in text_elements])
            logger.warning(f"No specific content tags (<article>, <main>, etc.) or common class names found for {url}, extracting from body.")

        # Clean up extra whitespace
        text = ' '.join(text.split())

        # Limit text length *after* extraction and cleaning
        # Adjust based on API limits/cost. WordLift's typical text APIs handle up to ~1M chars.
        max_text_length = 1000000 # 1 Million characters
        if len(text) > max_text_length:
            logger.warning(f"Extracted text for {url} is too long ({len(text)} chars), truncating to {max_text_length} chars.")
            text = text[:max_text_length]

        return text.strip() if text and text.strip() else None # Return None if text is empty after processing


    except requests.exceptions.RequestException as e:
        logger.error(f"Failed to fetch content from {url}: {e}")
        return None
    except Exception as e:
        logger.error(f"Error processing content from {url}: {e}")
        return None

# ------------------------
# WordLift API Call Logic
# ------------------------

def call_wordlift_api(text: str, keywords: Optional[List[str]] = None) -> Optional[Dict[str, Any]]:
    """Calls the WordLift Content Evaluation API."""
    if not WORDLIFT_API_KEY:
        logger.error("WORDLIFT_API_KEY environment variable not set.")
        return {"error": "API key not configured."}

    if not text or not text.strip():
        return {"error": "No significant content to evaluate."}

    payload = {
        "text": text,
        "keywords": keywords if keywords else []
    }

    headers = {
        'Authorization': f'Key {WORDLIFT_API_KEY}',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }

    logger.info(f"Calling WordLift API with text length {len(text)} and {len(keywords or [])} keywords.")

    try:
        response = requests.post(WORDLIFT_API_URL, headers=headers, json=payload, timeout=90) # Increased timeout again
        response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
        return response.json()

    except requests.exceptions.HTTPError as e:
        logger.error(f"WordLift API HTTP error for {e.request.url}: {e.response.status_code} - {e.response.text}")
        try:
            error_detail = e.response.json()
        except json.JSONDecodeError:
            error_detail = e.response.text
        return {"error": f"API returned status code {e.response.status_code}", "details": error_detail}
    except requests.exceptions.Timeout as e:
         logger.error(f"WordLift API request timed out for {e.request.url}: {e}")
         return {"error": f"API request timed out."}
    except requests.exceptions.RequestException as e:
        logger.error(f"WordLift API request error for {e.request.url}: {e}")
        return {"error": f"API request failed: {e}"}
    except Exception as e:
        logger.error(f"Unexpected error during API call: {e}")
        return {"error": f"An unexpected error occurred: {e}"}


# ------------------------
# Plotting Logic
# ------------------------

def plot_average_radar(average_scores: Dict[str, Optional[float]], avg_overall: Optional[float]) -> Any:
    """Return a radar (spider) plot as a Matplotlib figure showing average scores."""

    # Check if we have any valid scores to plot
    if not average_scores or all(v is None or pd.isna(v) for v in average_scores.values()):
        # Return a placeholder figure if no valid data is available
        fig, ax = plt.subplots(figsize=(6, 6))
        ax.text(0.5, 0.5, "No successful evaluations to plot\naverage scores.", horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, fontsize=12)
        ax.axis('off') # Hide axes
        plt.title("Average Content Quality Scores", size=16, y=1.05)
        plt.tight_layout()
        return fig


    categories = list(average_scores.keys())
    # Convert None/NaN values to 0 for plotting, but keep track of original for annotation
    values_raw = [average_scores[cat] for cat in categories]
    values_for_plot = [float(v) if v is not None and pd.notna(v) else 0 for v in values_raw]


    num_vars = len(categories)
    # Calculate angles for the radar chart
    angles = [n / float(num_vars) * 2 * np.pi for n in range(num_vars)]
    angles += angles[:1] # Complete the circle
    values_for_plot += values_for_plot[:1] # Complete the circle for values

    fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(projection='polar'))

    line_color = '#3452DB'
    fill_color = '#A1A7AF'
    background_color = '#F6F6F7'
    annotation_color = '#191919'

    # Plot data
    ax.plot(angles, values_for_plot, 'o-', linewidth=2, color=line_color, label='Average Scores')
    ax.fill(angles, values_for_plot, alpha=0.4, color=fill_color)

    # Set tick locations and labels
    ax.set_xticks(angles[:-1])
    ax.set_xticklabels(categories, color=line_color, fontsize=10)

    # Set y-axis limits. Max score is 100.
    ax.set_ylim(0, 100)
    ax.set_yticks([0, 20, 40, 60, 80, 100]) # Explicitly set y-ticks

    # Draw grid lines and axes
    ax.grid(True, alpha=0.5, color=fill_color)
    ax.set_facecolor(background_color)

    # Add score annotations next to points - Use raw values if not None/NaN
    for angle, value_raw, value_plotted in zip(angles[:-1], values_raw, values_for_plot[:-1]):
        if value_raw is not None and pd.notna(value_raw):
            # Adjust position slightly based on angle and value
            # More sophisticated positioning needed for perfect placement, simple offset below
            # Let's just add text slightly outside the point along the radial line
            radius = value_plotted + 5 # Offset outward
            # Ensure annotation stays within limits if needed, but 105 should be fine for ylim 100
            ax.text(angle, radius, f'{value_raw:.1f}', color=annotation_color,
                    horizontalalignment='center', verticalalignment='center', fontsize=9)


    # Add title - Only "Overall: XX.X/100" part
    overall_title_text = f'Overall: {avg_overall:.1f}/100' if avg_overall is not None and pd.notna(avg_overall) else 'Overall: -'
    plt.title(overall_title_text, size=16, y=1.1, color=annotation_color) # y=1.1 places it above the plot area

    plt.tight_layout()
    return fig

# ------------------------
# Main Evaluation Batch Function
# ------------------------

def evaluate_urls_batch(url_data: pd.DataFrame):
    """
    Evaluates a batch of URLs using the WordLift API.

    Args:
        url_data: A pandas DataFrame with columns ['URL', 'Target Keywords (comma-separated)'].

    Returns:
        A tuple containing:
        - A pandas DataFrame with the summary results.
        - A dictionary containing the full results (including errors) keyed by URL.
        - A Matplotlib figure for the average radar chart.
    """
    # Check if the DataFrame has any rows (correct way using .empty)
    if url_data.empty:
        logger.info("Input DataFrame is empty. Returning empty results.")
        # Return empty summary DF, empty full results, and an empty placeholder plot
        empty_summary_df = pd.DataFrame(columns=[
             'URL', 'Status', 'Overall Score', 'Content Purpose',
             'Content Accuracy', 'Content Depth', 'Readability Score (API)',
             'Readability Grade Level', 'SEO Score', 'Word Count', 'Error/Details'
        ])
        return empty_summary_df, {}, plot_average_radar(None, None) # Pass None, None to plotting function

    summary_results = []
    full_results = {}

    # Lists to store scores for calculating averages
    # Initialize with None/NaN to correctly handle empty inputs or failures
    purpose_scores = []
    accuracy_scores = []
    depth_scores = []
    readability_scores = [] # Note: API returns float like 2.5
    seo_scores = []
    overall_scores = []

    # Ensure columns exist and handle potential NaNs from the DataFrame input
    urls = url_data.get('URL', pd.Series(dtype=str)).fillna('') # Replace NaN URLs with empty strings
    keywords_col = url_data.get('Target Keywords (comma-separated)', pd.Series(dtype=str)).fillna('') # Replace NaN keywords with empty strings


    for index, url in enumerate(urls):
        url = url.strip()
        keywords_str = keywords_col.iloc[index].strip()
        keywords = [kw.strip() for kw in keywords_str.split(',') if kw.strip()]

        # Generate a unique key for full_results based on index and URL/placeholder
        result_key = f"Row_{index}" + (f": {url}" if url else "")


        if not url:
            summary_results.append(["", "Skipped", "-", "-", "-", "-", "-", "-", "-", "-", "Empty URL"])
            full_results[result_key] = {"status": "Skipped", "error": "Empty URL input."}
            logger.warning(f"Skipping evaluation for row {index}: Empty URL")
            # Append None to scores lists for skipped/failed rows
            purpose_scores.append(np.nan)
            accuracy_scores.append(np.nan)
            depth_scores.append(np.nan)
            readability_scores.append(np.nan)
            seo_scores.append(np.nan)
            overall_scores.append(np.nan)
            continue # Move to next URL

        logger.info(f"Processing URL: {url} (Row {index}) with keywords: {keywords}")

        # 1. Fetch Content
        content = fetch_content_from_url(url)

        if content is None or not content.strip():
            status = "Failed"
            error_msg = "Failed to fetch or extract content."
            summary_results.append([url, status, "-", "-", "-", "-", "-", "-", "-", "-", error_msg])
            full_results[result_key] = {"status": status, "error": error_msg}
            logger.error(f"Processing failed for {url} (Row {index}): {error_msg}")
            # Append None to scores lists for skipped/failed rows
            purpose_scores.append(np.nan)
            accuracy_scores.append(np.nan)
            depth_scores.append(np.nan)
            readability_scores.append(np.nan)
            seo_scores.append(np.nan)
            overall_scores.append(np.nan)
            continue # Move to next URL

        # 2. Call WordLift API
        api_result = call_wordlift_api(content, keywords)

        # 3. Process API Result
        summary_row = [url]
        if api_result and "error" not in api_result:
            status = "Success"
            qs = api_result.get('quality_score', {})
            breakdown = qs.get('breakdown', {})
            content_breakdown = breakdown.get('content', {})
            readability_breakdown = breakdown.get('readability', {})
            seo_breakdown = breakdown.get('seo', {})
            metadata = api_result.get('metadata', {})

            # Append scores for average calculation (only for successful calls)
            # Use .get() with None default, then convert to float, allowing NaN
            purpose_scores.append(float(content_breakdown.get('purpose')) if content_breakdown.get('purpose') is not None else np.nan)
            accuracy_scores.append(float(content_breakdown.get('accuracy')) if content_breakdown.get('accuracy') is not None else np.nan)
            depth_scores.append(float(content_breakdown.get('depth')) if content_breakdown.get('depth') is not None else np.nan)
            readability_scores.append(float(readability_breakdown.get('score')) if readability_breakdown.get('score') is not None else np.nan)
            seo_scores.append(float(seo_breakdown.get('score')) if seo_breakdown.get('score') is not None else np.nan)
            overall_scores.append(float(qs.get('overall')) if qs.get('overall') is not None else np.nan)


            # Append data for the summary table row
            # Use .get() with '-' default for display
            summary_row.extend([
                status,
                f'{qs.get("overall", "-"): .1f}' if qs.get('overall') is not None else "-",
                f'{content_breakdown.get("purpose", "-"): .0f}' if content_breakdown.get('purpose') is not None else "-",
                f'{content_breakdown.get("accuracy", "-"): .0f}' if content_breakdown.get('accuracy') is not None else "-",
                f'{content_breakdown.get("depth", "-"): .0f}' if content_breakdown.get('depth') is not None else "-",
                f'{readability_breakdown.get("score", "-"): .1f}' if readability_breakdown.get('score') is not None else "-",
                f'{readability_breakdown.get("grade_level", "-"): .0f}' if readability_breakdown.get('grade_level') is not None else "-",
                f'{seo_breakdown.get("score", "-"): .1f}' if seo_breakdown.get('score') is not None else "-",
                f'{metadata.get("word_count", "-"): .0f}' if metadata.get('word_count') is not None else "-",
                None # No error
            ])
            full_results[result_key] = api_result # Store full API result

        else:
            status = "Failed"
            error_msg = api_result.get("error", "Unknown API error.") if api_result else "API call failed."
            details = api_result.get("details", "") if api_result else ""
            summary_row.extend([
                status,
                "-", "-", "-", "-", "-", "-", "-", "-",
                f"{error_msg} {details}"
            ])
            full_results[result_key] = {"status": status, "error": error_msg, "details": details}
            logger.error(f"API call failed for {url} (Row {index}): {error_msg} {details}")

            # Append None/NaN to scores lists for failed rows
            purpose_scores.append(np.nan)
            accuracy_scores.append(np.nan)
            depth_scores.append(np.nan)
            readability_scores.append(np.nan)
            seo_scores.append(np.nan)
            overall_scores.append(np.nan)


        summary_results.append(summary_row)

    # Calculate Averages *after* processing all URLs, ignoring NaNs
    avg_purpose = np.nanmean(purpose_scores)
    avg_accuracy = np.nanmean(accuracy_scores)
    avg_depth = np.nanmean(depth_scores)
    avg_readability = np.nanmean(readability_scores)
    avg_seo = np.nanmean(seo_scores)
    avg_overall = np.nanmean(overall_scores)

    # Convert potentially NaN averages to None if there were no valid scores
    avg_purpose = avg_purpose if pd.notna(avg_purpose) else None
    avg_accuracy = avg_accuracy if pd.notna(avg_accuracy) else None
    avg_depth = avg_depth if pd.notna(avg_depth) else None
    avg_readability = avg_readability if pd.notna(avg_readability) else None
    avg_seo = avg_seo if pd.notna(avg_seo) else None
    avg_overall = avg_overall if pd.notna(avg_overall) else None


    # Prepare scores for the radar plot function
    average_scores_dict = {
        'Purpose': avg_purpose,
        'Accuracy': avg_accuracy,
        'Depth': avg_depth,
        'Readability': avg_readability,
        'SEO': avg_seo
    }

    # Generate the average radar plot
    average_radar_fig = plot_average_radar(average_scores_dict, avg_overall)


    # Create pandas DataFrame for summary output
    summary_df = pd.DataFrame(summary_results, columns=[
        'URL', 'Status', 'Overall Score', 'Content Purpose',
        'Content Accuracy', 'Content Depth', 'Readability Score (API)',
        'Readability Grade Level', 'SEO Score', 'Word Count', 'Error/Details'
    ])

    # Note: Formatting is already done when creating the summary_row list above
    # using f-strings like f'{value: .1f}' or f'{value: .0f}', and setting '-' for None.
    # This ensures that pandas DataFrame displays formatted strings directly.

    return summary_df, full_results, average_radar_fig # Return the plot too

# ------------------------
# Gradio Blocks Interface Setup
# ------------------------

with gr.Blocks(css=css, theme=theme) as demo:
    gr.Markdown("# WordLift Multi-URL Content Evaluator")
    gr.Markdown(
        "Enter up to 30 URLs in the table below. "
        "Optionally, provide comma-separated target keywords for each URL. "
        "The app will fetch content from each URL and evaluate it using the WordLift API."
    )

    with gr.Row():
        with gr.Column(scale=1):
            url_input_df = gr.Dataframe(
                headers=["URL", "Target Keywords (comma-separated)"],
                datatype=["str", "str"],
                row_count=(1, 30), # Allow adding rows up to 30
                col_count=(2, "fixed"),
                value=[
                    ["https://wordlift.io/blog/en/query-fan-out-ai-search/", "query fan out, ai search, google, ai"], # Added first URL
                    ["https://wordlift.io/blog/en/entity/google-knowledge-graph/", "google knowledge graph, entity, semantic web, seo"], # Added second URL
                    ["https://www.example.com/non-existent-page", ""], # Example of a failing URL
                    ["", ""], # Example of an empty row
                    ["", ""], # Add some extra empty rows for easier input
                    ["", ""],
                    ["", ""],
                ],
                label="URLs and Keywords"
            )
            submit_button = gr.Button("Evaluate All URLs", elem_classes=["primary-btn"])

        with gr.Column(scale=1, elem_classes="plot-container"):
             # New component for the average radar plot
             average_radar_output = gr.Plot(label="Average Content Quality Scores Radar")


    gr.Markdown("## Detailed Results")

    with gr.Column():
        summary_output_df = gr.DataFrame(
            label="Summary Results",
            # Data types are all string now because we formatted them with f-strings to include '-'
            headers=['URL', 'Status', 'Overall Score', 'Content Purpose',
                     'Content Accuracy', 'Content Depth', 'Readability Score (API)',
                     'Readability Grade Level', 'SEO Score', 'Word Count', 'Error/Details'],
            datatype=["str"] * 11,
            wrap=True # Wrap text in columns
        )
        with gr.Accordion("Full JSON Results", open=False):
             # Changed the output type to gr.JSON
             full_results_json = gr.JSON(label="Raw API Results per URL (or Error)")

    submit_button.click(
        fn=evaluate_urls_batch,
        inputs=[url_input_df],
        # Updated outputs to include the average radar plot
        outputs=[summary_output_df, full_results_json, average_radar_output]
    )

# Launch the app
if __name__ == "__main__":
    if not WORDLIFT_API_KEY:
        logger.error("\n----------------------------------------------------------")
        logger.error("WORDLIFT_API_KEY environment variable is not set.")
        logger.error("Please set it before running the script:")
        logger.error("  export WORDLIFT_API_KEY='YOUR_API_KEY'")
        logger.error("Or if using a .env file and python-dotenv:")
        logger.error("  pip install python-dotenv")
        logger.error("  # Add WORDLIFT_API_KEY=YOUR_API_KEY to a .env file")
        logger.error("  # import dotenv; dotenv.load_dotenv()")
        logger.error("  # in your script before getting the key.")
        logger.error("----------------------------------------------------------\n")
        # You might want to sys.exit(1) here if the API key is mandatory
        # import sys
        # sys.exit(1)


    logger.info("Launching Gradio app...")
    # Consider using share=True for easy sharing, but be mindful of security/costs
    # demo.launch(share=True)
    demo.launch()