basantyahya commited on
Commit
96decc0
·
verified ·
1 Parent(s): 1beac75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -14
app.py CHANGED
@@ -1,14 +1,19 @@
1
  import os
2
  import gradio as gr
3
  from google import genai
 
4
  from google.genai.types import GenerateContentConfig, GoogleSearch, Tool
5
 
6
- # -------- Gemini Setup --------
 
 
7
  API_KEY = os.getenv("Gemini_API_Key")
8
  client = genai.Client(api_key=API_KEY)
9
  MODEL_ID = "gemini-2.0-flash"
10
 
11
- # -------- Custom CSS + JS --------
 
 
12
  custom_css = """
13
  #search-btn {
14
  background-color: #4f46e5 !important;
@@ -18,7 +23,6 @@ custom_css = """
18
  padding: 10px 18px;
19
  transition: opacity 0.6s ease, transform 0.2s ease;
20
  }
21
-
22
  #search-btn:active {
23
  opacity: 0.5;
24
  transform: scale(0.97);
@@ -32,32 +36,52 @@ custom_js = """
32
  }
33
  """
34
 
35
- # -------- AI Function --------
 
 
36
  def google_search_query(question):
37
  try:
 
 
 
38
  google_search_tool = Tool(google_search=GoogleSearch())
39
 
40
  response = client.models.generate_content(
41
  model=MODEL_ID,
42
  contents=[
43
- {
44
- "role": "user",
45
- "parts": [{"text": question}]
46
- }
47
- ]
48
- ,
49
- config=GenerateContentConfig(tools=[google_search_tool]),
 
50
  )
51
 
52
- ai_response = response.text
53
- search_results = response.candidates[0].grounding_metadata.search_entry_point.rendered_content
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  return ai_response, search_results
56
 
57
  except Exception as e:
58
  return f"Error: {str(e)}", ""
59
 
60
- # -------- Gradio App --------
 
 
61
  with gr.Blocks(css=custom_css) as app:
62
  gr.Markdown("## 🔍 Google Search with Gemini AI")
63
 
@@ -75,5 +99,8 @@ with gr.Blocks(css=custom_css) as app:
75
  js=custom_js
76
  )
77
 
 
 
 
78
  if __name__ == "__main__":
79
  app.launch(share=True)
 
1
  import os
2
  import gradio as gr
3
  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;
 
23
  padding: 10px 18px;
24
  transition: opacity 0.6s ease, transform 0.2s ease;
25
  }
 
26
  #search-btn:active {
27
  opacity: 0.5;
28
  transform: scale(0.97);
 
36
  }
37
  """
38
 
39
+ # -----------------------
40
+ # AI Function (FIXED)
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=[
52
+ types.Content(
53
+ role="user",
54
+ parts=[types.Part.from_text(question)]
55
+ )
56
+ ],
57
+ config=GenerateContentConfig(
58
+ tools=[google_search_tool]
59
+ ),
60
  )
61
 
62
+ ai_response = response.text or "No AI response generated."
63
+
64
+ search_results = ""
65
+ if (
66
+ response.candidates
67
+ and response.candidates[0].grounding_metadata
68
+ and response.candidates[0].grounding_metadata.search_entry_point
69
+ ):
70
+ search_results = (
71
+ response.candidates[0]
72
+ .grounding_metadata
73
+ .search_entry_point
74
+ .rendered_content
75
+ )
76
 
77
  return ai_response, search_results
78
 
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
 
 
99
  js=custom_js
100
  )
101
 
102
+ # -----------------------
103
+ # Launch
104
+ # -----------------------
105
  if __name__ == "__main__":
106
  app.launch(share=True)