Hammad712 commited on
Commit
0fd3bce
·
verified ·
1 Parent(s): dba0e0a

Update gemini_report.py

Browse files
Files changed (1) hide show
  1. gemini_report.py +71 -71
gemini_report.py CHANGED
@@ -5,77 +5,77 @@ def generate_report_with_gemini(pagespeed_data, gemini_api_key):
5
  genai.configure(api_key=gemini_api_key)
6
  model = genai.GenerativeModel("gemini-2.0-flash")
7
 
8
- prompt = (
9
- "**Role:** You are an **Expert Web Performance Optimization Analyst and Senior Full-Stack Engineer** "
10
- "with deep expertise in interpreting Google PageSpeed Insights data, diagnosing frontend and "
11
- "backend bottlenecks, and devising actionable, high-impact optimization strategies.\n\n"
12
- "**Objective:**\n"
13
- "Analyze the provided Google PageSpeed Insights JSON data for the analyzed website. "
14
- "Your primary goal is to generate a comprehensive, prioritized, and actionable set of strategies "
15
- "to significantly improve its performance. These strategies must directly address the specific "
16
- "metrics and audit findings within the report, aiming to elevate both Core Web Vitals "
17
- "(LCP, INP, CLS) and other key performance indicators (FCP, TTFB, TBT), and ultimately "
18
- "improve the `overall_category` to 'FAST' where possible.\n\n"
19
- "**Input Data:**\n"
20
- "The following JSON object contains the complete PageSpeed Insights report:\n"
21
- f"```json\n{json.dumps(pagespeed_data, indent=2)}\n```\n\n"
22
- "**Analysis and Strategy Formulation - Instructions:**\n\n"
23
- "1. **Executive Performance Summary:**\n"
24
- " * Begin with a concise overview of the website's current performance status based on the provided data.\n"
25
- " * Highlight the `overall_category` for both `loadingExperience` (specific URL) and `originLoadingExperience` (entire origin).\n"
26
- " * Pinpoint the current values and `category` (e.g., FAST, AVERAGE, SLOW) for each key metric:\n"
27
- " * `CUMULATIVE_LAYOUT_SHIFT_SCORE` (CLS)\n"
28
- " * `EXPERIMENTAL_TIME_TO_FIRST_BYTE` (TTFB)\n"
29
- " * `FIRST_CONTENTFUL_PAINT_MS` (FCP)\n"
30
- " * `INTERACTION_TO_NEXT_PAINT` (INP)\n"
31
- " * `LARGEST_CONTENTFUL_PAINT_MS` (LCP)\n"
32
- " * `total-blocking-time` (TBT) from Lighthouse.\n"
33
- " * Identify any significant `metricSavings` opportunities highlighted in the Lighthouse `audits`.\n\n"
34
- "2. **Deep-Dive into Bottlenecks & Audit Failures:**\n"
35
- " * Systematically go through the `loadingExperience`, `originLoadingExperience`, and `lighthouseResult` (especially the `audits` section).\n"
36
- " * For each underperforming metric or failed/suboptimal audit (e.g., Lighthouse scores less than 1, or `notApplicable` audits with clear improvement paths like `lcp-lazy-loaded`, `critical-request-chains`, `dom-size`, `non-composited-animations`), extract the relevant details, display values, and numeric values.\n\n"
37
- "3. **Develop Prioritized, Actionable Optimization Strategies:**\n"
38
- " For *each* identified performance issue or opportunity, provide the following:\n"
39
- " * **A. Issue & Evidence:** Clearly state the problem (e.g., \"High Total Blocking Time,\" \"Suboptimal Largest Contentful Paint due to unoptimized image,\" \"Excessive DOM Size,\" \"Render-blocking resources in critical request chain\"). Refer directly to the JSON data points and audit IDs that support this finding (e.g., `audits['total-blocking-time'].numericValue`, `audits['critical-request-chains'].details.longestChain`).\n"
40
- " * **B. Root Cause Analysis (Inferred):** Briefly explain the likely technical reasons behind the issue based on the data.\n"
41
- " * **C. Specific, Technical Recommendation(s):** Provide detailed, actionable steps a development team can take. Be specific.\n"
42
- " * **D. Targeted Metric Improvement:** Specify which primary and secondary metrics this strategy will positively impact (e.g., \"This will directly reduce LCP and improve FCP,\" or \"This will significantly lower TBT and improve INP.\").\n"
43
- " * **E. Priority Level:** Assign a priority (High, Medium, Low) based on:\n"
44
- " * Impact on Core Web Vitals.\n"
45
- " * Potential for overall score improvement (consider `metricSavings`).\n"
46
- " * Severity of the issue (e.g., 'SLOW' or 'AVERAGE' categories).\n"
47
- " * Estimated implementation effort (favor high-impact, low/medium-effort tasks for higher priority).\n"
48
- " * **F. Justification for Priority:** Briefly explain why this priority was assigned.\n\n"
49
- "4. **Strategic Grouping (Optional but Recommended):**\n"
50
- " If applicable, group recommendations by area (e.g., Asset Optimization, JavaScript Optimization, Server-Side Improvements, Rendering Path Optimization, CSS Enhancements).\n\n"
51
- "5. **Anticipated Overall Impact:**\n"
52
- " Conclude with a statement on the anticipated overall improvement in performance and user experience if the high and medium-priority recommendations are implemented.\n\n"
53
- "**Output Format:**\n"
54
- "Please structure your response clearly. Use headings, subheadings, and bullet points to enhance readability and actionability. For example:\n\n"
55
- "---\n"
56
- "## Executive Performance Summary\n"
57
- "* **Overall URL Loading Experience Category:** [e.g., AVERAGE]\n"
58
- "* **Overall Origin Loading Experience Category:** [e.g., AVERAGE]\n"
59
- "* **Key Metrics:**\n"
60
- " * LCP: [Value] ms ([Category])\n"
61
- " * INP: [Value] ms ([Category])\n"
62
- " * ...etc.\n\n"
63
- "---\n"
64
- "## Prioritized Optimization Strategies\n\n"
65
- "### High Priority\n"
66
- "**1. Issue & Evidence:** [e.g., High Total Blocking Time (TBT) of 1200 ms - `audits['total-blocking-time'].numericValue`]\n"
67
- " * **Root Cause Analysis:** [e.g., Long JavaScript tasks on the main thread during page load, likely from unoptimized third-party scripts or complex component rendering.]\n"
68
- " * **Specific, Technical Recommendation(s):**\n"
69
- " * [Action 1]\n"
70
- " * [Action 2]\n"
71
- " * **Targeted Metric Improvement:** [e.g., TBT, INP, FCP]\n"
72
- " * **Justification for Priority:** [e.g., Directly impacts interactivity (INP) and is a significant contributor to a poor lab score.]\n\n"
73
- "**(Continue with other High, Medium, and Low priority items)**\n"
74
- "---\n\n"
75
- "**Ensure your analysis is based *solely* on the provided JSON data and your expert interpretation of it. "
76
- "Avoid generic advice; all recommendations must be tied to specific findings within the report. "
77
- "Do not add anything irrelevant in the report.**"
78
- )
79
 
80
  try:
81
  response = model.generate_content(prompt)
 
5
  genai.configure(api_key=gemini_api_key)
6
  model = genai.GenerativeModel("gemini-2.0-flash")
7
 
8
+ prompt = (
9
+ "**Role:** You are an **Expert Web Performance Optimization Analyst and Senior Full-Stack Engineer** "
10
+ "with deep expertise in interpreting Google PageSpeed Insights data, diagnosing frontend and "
11
+ "backend bottlenecks, and devising actionable, high-impact optimization strategies.\n\n"
12
+ "**Objective:**\n"
13
+ "Analyze the provided Google PageSpeed Insights JSON data for the analyzed website. "
14
+ "Your primary goal is to generate a comprehensive, prioritized, and actionable set of strategies "
15
+ "to significantly improve its performance. These strategies must directly address the specific "
16
+ "metrics and audit findings within the report, aiming to elevate both Core Web Vitals "
17
+ "(LCP, INP, CLS) and other key performance indicators (FCP, TTFB, TBT), and ultimately "
18
+ "improve the `overall_category` to 'FAST' where possible.\n\n"
19
+ "**Input Data:**\n"
20
+ "The following JSON object contains the complete PageSpeed Insights report:\n"
21
+ f"```json\n{json.dumps(pagespeed_data, indent=2)}\n```\n\n"
22
+ "**Analysis and Strategy Formulation - Instructions:**\n\n"
23
+ "1. **Executive Performance Summary:**\n"
24
+ " * Begin with a concise overview of the website's current performance status based on the provided data.\n"
25
+ " * Highlight the `overall_category` for both `loadingExperience` (specific URL) and `originLoadingExperience` (entire origin).\n"
26
+ " * Pinpoint the current values and `category` (e.g., FAST, AVERAGE, SLOW) for each key metric:\n"
27
+ " * `CUMULATIVE_LAYOUT_SHIFT_SCORE` (CLS)\n"
28
+ " * `EXPERIMENTAL_TIME_TO_FIRST_BYTE` (TTFB)\n"
29
+ " * `FIRST_CONTENTFUL_PAINT_MS` (FCP)\n"
30
+ " * `INTERACTION_TO_NEXT_PAINT` (INP)\n"
31
+ " * `LARGEST_CONTENTFUL_PAINT_MS` (LCP)\n"
32
+ " * `total-blocking-time` (TBT) from Lighthouse.\n"
33
+ " * Identify any significant `metricSavings` opportunities highlighted in the Lighthouse `audits`.\n\n"
34
+ "2. **Deep-Dive into Bottlenecks & Audit Failures:**\n"
35
+ " * Systematically go through the `loadingExperience`, `originLoadingExperience`, and `lighthouseResult` (especially the `audits` section).\n"
36
+ " * For each underperforming metric or failed/suboptimal audit (e.g., Lighthouse scores less than 1, or `notApplicable` audits with clear improvement paths like `lcp-lazy-loaded`, `critical-request-chains`, `dom-size`, `non-composited-animations`), extract the relevant details, display values, and numeric values.\n\n"
37
+ "3. **Develop Prioritized, Actionable Optimization Strategies:**\n"
38
+ " For *each* identified performance issue or opportunity, provide the following:\n"
39
+ " * **A. Issue & Evidence:** Clearly state the problem (e.g., \"High Total Blocking Time,\" \"Suboptimal Largest Contentful Paint due to unoptimized image,\" \"Excessive DOM Size,\" \"Render-blocking resources in critical request chain\"). Refer directly to the JSON data points and audit IDs that support this finding (e.g., `audits['total-blocking-time'].numericValue`, `audits['critical-request-chains'].details.longestChain`).\n"
40
+ " * **B. Root Cause Analysis (Inferred):** Briefly explain the likely technical reasons behind the issue based on the data.\n"
41
+ " * **C. Specific, Technical Recommendation(s):** Provide detailed, actionable steps a development team can take. Be specific.\n"
42
+ " * **D. Targeted Metric Improvement:** Specify which primary and secondary metrics this strategy will positively impact (e.g., \"This will directly reduce LCP and improve FCP,\" or \"This will significantly lower TBT and improve INP.\").\n"
43
+ " * **E. Priority Level:** Assign a priority (High, Medium, Low) based on:\n"
44
+ " * Impact on Core Web Vitals.\n"
45
+ " * Potential for overall score improvement (consider `metricSavings`).\n"
46
+ " * Severity of the issue (e.g., 'SLOW' or 'AVERAGE' categories).\n"
47
+ " * Estimated implementation effort (favor high-impact, low/medium-effort tasks for higher priority).\n"
48
+ " * **F. Justification for Priority:** Briefly explain why this priority was assigned.\n\n"
49
+ "4. **Strategic Grouping (Optional but Recommended):**\n"
50
+ " If applicable, group recommendations by area (e.g., Asset Optimization, JavaScript Optimization, Server-Side Improvements, Rendering Path Optimization, CSS Enhancements).\n\n"
51
+ "5. **Anticipated Overall Impact:**\n"
52
+ " Conclude with a statement on the anticipated overall improvement in performance and user experience if the high and medium-priority recommendations are implemented.\n\n"
53
+ "**Output Format:**\n"
54
+ "Please structure your response clearly. Use headings, subheadings, and bullet points to enhance readability and actionability. For example:\n\n"
55
+ "---\n"
56
+ "## Executive Performance Summary\n"
57
+ "* **Overall URL Loading Experience Category:** [e.g., AVERAGE]\n"
58
+ "* **Overall Origin Loading Experience Category:** [e.g., AVERAGE]\n"
59
+ "* **Key Metrics:**\n"
60
+ " * LCP: [Value] ms ([Category])\n"
61
+ " * INP: [Value] ms ([Category])\n"
62
+ " * ...etc.\n\n"
63
+ "---\n"
64
+ "## Prioritized Optimization Strategies\n\n"
65
+ "### High Priority\n"
66
+ "**1. Issue & Evidence:** [e.g., High Total Blocking Time (TBT) of 1200 ms - `audits['total-blocking-time'].numericValue`]\n"
67
+ " * **Root Cause Analysis:** [e.g., Long JavaScript tasks on the main thread during page load, likely from unoptimized third-party scripts or complex component rendering.]\n"
68
+ " * **Specific, Technical Recommendation(s):**\n"
69
+ " * [Action 1]\n"
70
+ " * [Action 2]\n"
71
+ " * **Targeted Metric Improvement:** [e.g., TBT, INP, FCP]\n"
72
+ " * **Justification for Priority:** [e.g., Directly impacts interactivity (INP) and is a significant contributor to a poor lab score.]\n\n"
73
+ "**(Continue with other High, Medium, and Low priority items)**\n"
74
+ "---\n\n"
75
+ "**Ensure your analysis is based *solely* on the provided JSON data and your expert interpretation of it. "
76
+ "Avoid generic advice; all recommendations must be tied to specific findings within the report. "
77
+ "Do not add anything irrelevant in the report.**"
78
+ )
79
 
80
  try:
81
  response = model.generate_content(prompt)