Update app.py
Browse files
app.py
CHANGED
|
@@ -49,21 +49,25 @@ custom_js = """
|
|
| 49 |
# AI Function (BULLETPROOF)
|
| 50 |
# =========================================================
|
| 51 |
def google_search_query(question):
|
| 52 |
-
try:
|
| 53 |
-
# Force conversion to string safely
|
| 54 |
-
if question is None:
|
| 55 |
-
question = ""
|
| 56 |
-
else:
|
| 57 |
-
question = str(question)
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
return "Please type a question above 👆", ""
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
| 67 |
response = client.models.generate_content(
|
| 68 |
model=MODEL_ID,
|
| 69 |
contents=[
|
|
@@ -77,17 +81,35 @@ def google_search_query(question):
|
|
| 77 |
),
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
|
|
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
search_results = ""
|
|
|
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
.
|
| 89 |
-
.
|
| 90 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
except Exception:
|
| 92 |
search_results = ""
|
| 93 |
|
|
|
|
| 49 |
# AI Function (BULLETPROOF)
|
| 50 |
# =========================================================
|
| 51 |
def google_search_query(question):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# ---------- 1️⃣ Normalize safely ----------
|
| 54 |
+
if question is None:
|
| 55 |
+
question = ""
|
| 56 |
+
else:
|
| 57 |
+
question = str(question)
|
| 58 |
+
|
| 59 |
+
question = question.strip()
|
| 60 |
|
| 61 |
+
if question == "":
|
| 62 |
+
return "Please type a question above 👆", ""
|
|
|
|
| 63 |
|
| 64 |
+
try:
|
| 65 |
+
# ---------- 2️⃣ Define Search Tool ----------
|
| 66 |
+
google_search_tool = Tool(
|
| 67 |
+
google_search=GoogleSearch()
|
| 68 |
+
)
|
| 69 |
|
| 70 |
+
# ---------- 3️⃣ Generate Response ----------
|
| 71 |
response = client.models.generate_content(
|
| 72 |
model=MODEL_ID,
|
| 73 |
contents=[
|
|
|
|
| 81 |
),
|
| 82 |
)
|
| 83 |
|
| 84 |
+
# ---------- 4️⃣ Extract AI Text Safely ----------
|
| 85 |
+
ai_response = ""
|
| 86 |
|
| 87 |
+
if hasattr(response, "text") and response.text:
|
| 88 |
+
ai_response = response.text
|
| 89 |
+
elif response.candidates:
|
| 90 |
+
try:
|
| 91 |
+
ai_response = response.candidates[0].content.parts[0].text
|
| 92 |
+
except Exception:
|
| 93 |
+
ai_response = "No AI response generated."
|
| 94 |
+
else:
|
| 95 |
+
ai_response = "No AI response generated."
|
| 96 |
+
|
| 97 |
+
# ---------- 5️⃣ Extract Search Grounding Safely ----------
|
| 98 |
search_results = ""
|
| 99 |
+
|
| 100 |
try:
|
| 101 |
+
candidate = response.candidates[0]
|
| 102 |
+
if (
|
| 103 |
+
hasattr(candidate, "grounding_metadata")
|
| 104 |
+
and candidate.grounding_metadata
|
| 105 |
+
and candidate.grounding_metadata.search_entry_point
|
| 106 |
+
):
|
| 107 |
+
search_results = (
|
| 108 |
+
candidate
|
| 109 |
+
.grounding_metadata
|
| 110 |
+
.search_entry_point
|
| 111 |
+
.rendered_content
|
| 112 |
+
)
|
| 113 |
except Exception:
|
| 114 |
search_results = ""
|
| 115 |
|