Update gemini_report.py
Browse files- gemini_report.py +8 -10
gemini_report.py
CHANGED
@@ -1,13 +1,11 @@
|
|
1 |
import json
|
2 |
import google.generativeai as genai
|
3 |
-
from config import GEMINI_API_KEY
|
4 |
-
from models import PageSpeedRequest
|
5 |
|
6 |
-
def generate_report_with_gemini(pagespeed_data
|
7 |
-
genai.configure(api_key=
|
8 |
model = genai.GenerativeModel("gemini-2.0-flash")
|
9 |
|
10 |
-
|
11 |
"**Role:** You are an **Expert Web Performance Optimization Analyst and Senior Full-Stack Engineer** "
|
12 |
"with deep expertise in interpreting Google PageSpeed Insights data, diagnosing frontend and "
|
13 |
"backend bottlenecks, and devising actionable, high-impact optimization strategies.\n\n"
|
@@ -78,13 +76,13 @@ def generate_report_with_gemini(pagespeed_data: dict) -> str:
|
|
78 |
"Avoid generic advice; all recommendations must be tied to specific findings within the report. "
|
79 |
"Do not add anything irrelevant in the report.**"
|
80 |
)
|
|
|
81 |
try:
|
82 |
response = model.generate_content(prompt)
|
83 |
if response and hasattr(response, "text"):
|
84 |
return response.text
|
85 |
-
elif response and
|
86 |
-
|
87 |
-
|
88 |
-
return "Report generation failed or returned an incomplete response."
|
89 |
except Exception as e:
|
90 |
-
return f"An error occurred during report generation: {str(e)}"
|
|
|
1 |
import json
|
2 |
import google.generativeai as genai
|
|
|
|
|
3 |
|
4 |
+
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"
|
|
|
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)
|
82 |
if response and hasattr(response, "text"):
|
83 |
return response.text
|
84 |
+
elif response and response.candidates and response.candidates[0].finish_reason == "SAFETY":
|
85 |
+
return "Report generation was blocked due to safety settings."
|
86 |
+
return "Report generation resulted in a non-text response or was incomplete."
|
|
|
87 |
except Exception as e:
|
88 |
+
return f"An error occurred during report generation: {str(e)}."
|