Update app.py
Browse files
app.py
CHANGED
|
@@ -5,19 +5,19 @@ from google.genai import types
|
|
| 5 |
from google.genai.types import GenerateContentConfig, GoogleSearch, Tool
|
| 6 |
|
| 7 |
# =========================================================
|
| 8 |
-
# Gemini Setup
|
| 9 |
# =========================================================
|
| 10 |
API_KEY = os.getenv("Gemini_API_Key")
|
| 11 |
|
| 12 |
if not API_KEY:
|
| 13 |
raise RuntimeError(
|
| 14 |
"β Gemini_API_Key is not set. "
|
| 15 |
-
"
|
| 16 |
)
|
| 17 |
|
| 18 |
client = genai.Client(api_key=API_KEY)
|
| 19 |
|
| 20 |
-
# β
|
| 21 |
MODEL_ID = "gemini-2.0-flash"
|
| 22 |
|
| 23 |
# =========================================================
|
|
@@ -46,12 +46,13 @@ custom_js = """
|
|
| 46 |
"""
|
| 47 |
|
| 48 |
# =========================================================
|
| 49 |
-
# AI Function (
|
| 50 |
# =========================================================
|
| 51 |
def google_search_query(question):
|
| 52 |
try:
|
| 53 |
-
#
|
| 54 |
-
question =question
|
|
|
|
| 55 |
|
| 56 |
if question == "":
|
| 57 |
return "Please type a question above π", ""
|
|
@@ -73,7 +74,6 @@ def google_search_query(question):
|
|
| 73 |
|
| 74 |
ai_response = response.text or "No AI response generated."
|
| 75 |
|
| 76 |
-
# Safe grounding extraction
|
| 77 |
search_results = ""
|
| 78 |
if (
|
| 79 |
response.candidates
|
|
@@ -101,7 +101,7 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 101 |
question = gr.Textbox(
|
| 102 |
lines=2,
|
| 103 |
label="Ask a Question",
|
| 104 |
-
placeholder="e.g.
|
| 105 |
)
|
| 106 |
|
| 107 |
search_btn = gr.Button("π Search", elem_id="search-btn")
|
|
@@ -117,7 +117,7 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 117 |
js=custom_js
|
| 118 |
)
|
| 119 |
|
| 120 |
-
# Enter key submit
|
| 121 |
question.submit(
|
| 122 |
fn=google_search_query,
|
| 123 |
inputs=[question],
|
|
|
|
| 5 |
from google.genai.types import GenerateContentConfig, GoogleSearch, Tool
|
| 6 |
|
| 7 |
# =========================================================
|
| 8 |
+
# Gemini Setup
|
| 9 |
# =========================================================
|
| 10 |
API_KEY = os.getenv("Gemini_API_Key")
|
| 11 |
|
| 12 |
if not API_KEY:
|
| 13 |
raise RuntimeError(
|
| 14 |
"β Gemini_API_Key is not set. "
|
| 15 |
+
"Add it in Hugging Face β Settings β Secrets."
|
| 16 |
)
|
| 17 |
|
| 18 |
client = genai.Client(api_key=API_KEY)
|
| 19 |
|
| 20 |
+
# β
Valid model
|
| 21 |
MODEL_ID = "gemini-2.0-flash"
|
| 22 |
|
| 23 |
# =========================================================
|
|
|
|
| 46 |
"""
|
| 47 |
|
| 48 |
# =========================================================
|
| 49 |
+
# AI Function (BULLETPROOF)
|
| 50 |
# =========================================================
|
| 51 |
def google_search_query(question):
|
| 52 |
try:
|
| 53 |
+
# π Bulletproof input normalization
|
| 54 |
+
question = "" if question is None else str(question)
|
| 55 |
+
question = question.strip()
|
| 56 |
|
| 57 |
if question == "":
|
| 58 |
return "Please type a question above π", ""
|
|
|
|
| 74 |
|
| 75 |
ai_response = response.text or "No AI response generated."
|
| 76 |
|
|
|
|
| 77 |
search_results = ""
|
| 78 |
if (
|
| 79 |
response.candidates
|
|
|
|
| 101 |
question = gr.Textbox(
|
| 102 |
lines=2,
|
| 103 |
label="Ask a Question",
|
| 104 |
+
placeholder="e.g. What are types of machine learning?"
|
| 105 |
)
|
| 106 |
|
| 107 |
search_btn = gr.Button("π Search", elem_id="search-btn")
|
|
|
|
| 117 |
js=custom_js
|
| 118 |
)
|
| 119 |
|
| 120 |
+
# Enter key submit
|
| 121 |
question.submit(
|
| 122 |
fn=google_search_query,
|
| 123 |
inputs=[question],
|