basantyahya commited on
Commit
d0d8730
Β·
verified Β·
1 Parent(s): 10503f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -4,16 +4,16 @@ from google import genai
4
  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
  client = genai.Client(api_key=API_KEY)
12
  MODEL_ID = "gemini-2.0-flash"
13
 
14
- # -----------------------
15
  # Custom CSS + JS
16
- # -----------------------
17
  custom_css = """
18
  #search-btn {
19
  background-color: #4f46e5 !important;
@@ -36,16 +36,19 @@ custom_js = """
36
  }
37
  """
38
 
39
- # -----------------------
40
  # AI Function
41
- # -----------------------
42
  def google_search_query(question):
43
  try:
 
44
  if not question or question.strip() == "":
45
  return "Please enter a question.", ""
46
 
 
47
  google_search_tool = Tool(google_search=GoogleSearch())
48
 
 
49
  response = client.models.generate_content(
50
  model=MODEL_ID,
51
  contents=[
@@ -59,8 +62,10 @@ def google_search_query(question):
59
  ),
60
  )
61
 
 
62
  ai_response = response.text or "No AI response generated."
63
 
 
64
  search_results = ""
65
  if (
66
  response.candidates
@@ -79,28 +84,41 @@ def google_search_query(question):
79
  except Exception as e:
80
  return f"Error: {str(e)}", ""
81
 
82
- # -----------------------
83
  # Gradio App
84
- # -----------------------
85
  with gr.Blocks(css=custom_css) as app:
86
  gr.Markdown("## πŸ” Google Search with Gemini AI")
87
 
88
- question = gr.Textbox(lines=2, label="Ask a Question")
 
 
 
 
89
 
90
  search_btn = gr.Button("πŸ” Search", elem_id="search-btn")
91
 
92
  ai_output = gr.Textbox(label="AI Response")
93
  search_output = gr.HTML(label="Search Results")
94
 
 
95
  search_btn.click(
96
  fn=google_search_query,
97
- inputs=[question], # βœ… FIXED HERE
98
  outputs=[ai_output, search_output],
99
  js=custom_js
100
  )
101
 
102
- # -----------------------
 
 
 
 
 
 
 
103
  # Launch
104
- # -----------------------
105
  if __name__ == "__main__":
106
  app.launch(share=True)
 
 
4
  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") # Set this in HF Secrets
11
  client = genai.Client(api_key=API_KEY)
12
  MODEL_ID = "gemini-2.0-flash"
13
 
14
+ # =========================================================
15
  # Custom CSS + JS
16
+ # =========================================================
17
  custom_css = """
18
  #search-btn {
19
  background-color: #4f46e5 !important;
 
36
  }
37
  """
38
 
39
+ # =========================================================
40
  # AI Function
41
+ # =========================================================
42
  def google_search_query(question):
43
  try:
44
+ # Guard: empty input
45
  if not question or question.strip() == "":
46
  return "Please enter a question.", ""
47
 
48
+ # Google Search tool
49
  google_search_tool = Tool(google_search=GoogleSearch())
50
 
51
+ # Gemini request (STRICT schema)
52
  response = client.models.generate_content(
53
  model=MODEL_ID,
54
  contents=[
 
62
  ),
63
  )
64
 
65
+ # AI text response
66
  ai_response = response.text or "No AI response generated."
67
 
68
+ # Grounded search results (safe access)
69
  search_results = ""
70
  if (
71
  response.candidates
 
84
  except Exception as e:
85
  return f"Error: {str(e)}", ""
86
 
87
+ # =========================================================
88
  # Gradio App
89
+ # =========================================================
90
  with gr.Blocks(css=custom_css) as app:
91
  gr.Markdown("## πŸ” Google Search with Gemini AI")
92
 
93
+ question = gr.Textbox(
94
+ lines=2,
95
+ label="Ask a Question",
96
+ placeholder="e.g. Types of machine learning"
97
+ )
98
 
99
  search_btn = gr.Button("πŸ” Search", elem_id="search-btn")
100
 
101
  ai_output = gr.Textbox(label="AI Response")
102
  search_output = gr.HTML(label="Search Results")
103
 
104
+ # Button click
105
  search_btn.click(
106
  fn=google_search_query,
107
+ inputs=[question],
108
  outputs=[ai_output, search_output],
109
  js=custom_js
110
  )
111
 
112
+ # Press Enter to submit (UX improvement)
113
+ question.submit(
114
+ fn=google_search_query,
115
+ inputs=[question],
116
+ outputs=[ai_output, search_output],
117
+ )
118
+
119
+ # =========================================================
120
  # Launch
121
+ # =========================================================
122
  if __name__ == "__main__":
123
  app.launch(share=True)
124
+