basantyahya commited on
Commit
116a905
·
verified ·
1 Parent(s): 5e485ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -19
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
- question = question.strip()
 
 
 
 
 
 
60
 
61
- # Only block if truly empty
62
- if len(question) == 0:
63
- return "Please type a question above 👆", ""
64
 
65
- google_search_tool = Tool(google_search=GoogleSearch())
 
 
 
 
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
- ai_response = response.text or "No AI response generated."
 
81
 
82
- # Safe grounding extraction
 
 
 
 
 
 
 
 
 
 
83
  search_results = ""
 
84
  try:
85
- search_results = (
86
- response.candidates[0]
87
- .grounding_metadata
88
- .search_entry_point
89
- .rendered_content
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