Update app.py
Browse files
app.py
CHANGED
|
@@ -3,92 +3,71 @@ import gradio as gr
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai.types import GenerateContentConfig, GoogleSearch, Tool
|
| 5 |
|
| 6 |
-
#
|
| 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 |
-
|
| 12 |
-
|
| 13 |
#search-btn {
|
| 14 |
-
background-color: #4f46e5;
|
| 15 |
-
color: white;
|
| 16 |
-
border: none;
|
| 17 |
-
padding: 12px 20px;
|
| 18 |
font-size: 16px;
|
| 19 |
border-radius: 10px;
|
| 20 |
-
|
| 21 |
transition: opacity 0.6s ease, transform 0.2s ease;
|
| 22 |
-
display: flex;
|
| 23 |
-
align-items: center;
|
| 24 |
-
gap: 8px;
|
| 25 |
}
|
| 26 |
|
| 27 |
-
#search-btn:
|
| 28 |
-
background-color: #4338ca;
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
#search-btn.fade {
|
| 32 |
opacity: 0.5;
|
| 33 |
transform: scale(0.97);
|
| 34 |
}
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
<button id="search-btn">
|
| 38 |
-
๐ Search
|
| 39 |
-
</button>
|
| 40 |
-
|
| 41 |
-
<audio id="clap-sound">
|
| 42 |
-
<source src="https://www.soundjay.com/human/applause-8.mp3" type="audio/mpeg">
|
| 43 |
-
</audio>
|
| 44 |
-
|
| 45 |
-
<script>
|
| 46 |
-
const btn = document.getElementById("search-btn");
|
| 47 |
-
const clap = document.getElementById("clap-sound");
|
| 48 |
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
clap.play();
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
setTimeout(() => {
|
| 54 |
-
btn.classList.remove("fade");
|
| 55 |
-
}, 1200);
|
| 56 |
-
});
|
| 57 |
-
</script>
|
| 58 |
"""
|
| 59 |
|
|
|
|
| 60 |
def google_search_query(question):
|
| 61 |
try:
|
| 62 |
-
# Define the Google Search Tool
|
| 63 |
google_search_tool = Tool(google_search=GoogleSearch())
|
| 64 |
-
|
| 65 |
-
# Generate the response
|
| 66 |
response = client.models.generate_content(
|
| 67 |
model=MODEL_ID,
|
| 68 |
contents=question,
|
| 69 |
config=GenerateContentConfig(tools=[google_search_tool]),
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
ai_response = response.text # AI response as plain text
|
| 74 |
search_results = response.candidates[0].grounding_metadata.search_entry_point.rendered_content
|
| 75 |
|
| 76 |
return ai_response, search_results
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
return f"Error: {str(e)}", ""
|
| 79 |
|
| 80 |
-
# Gradio
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
| 94 |
-
app.launch(share=True)
|
|
|
|
| 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;
|
| 15 |
+
color: white !important;
|
|
|
|
|
|
|
| 16 |
font-size: 16px;
|
| 17 |
border-radius: 10px;
|
| 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);
|
| 25 |
}
|
| 26 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
custom_js = """
|
| 29 |
+
() => {
|
| 30 |
+
const clap = new Audio("https://www.soundjay.com/human/applause-8.mp3");
|
| 31 |
clap.play();
|
| 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=question,
|
| 43 |
config=GenerateContentConfig(tools=[google_search_tool]),
|
| 44 |
)
|
| 45 |
|
| 46 |
+
ai_response = response.text
|
|
|
|
| 47 |
search_results = response.candidates[0].grounding_metadata.search_entry_point.rendered_content
|
| 48 |
|
| 49 |
return ai_response, search_results
|
| 50 |
+
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error: {str(e)}", ""
|
| 53 |
|
| 54 |
+
# -------- Gradio App --------
|
| 55 |
+
with gr.Blocks(css=custom_css) as app:
|
| 56 |
+
gr.Markdown("## ๐ Google Search with Gemini AI")
|
| 57 |
+
|
| 58 |
+
question = gr.Textbox(lines=2, label="Ask a Question")
|
| 59 |
+
|
| 60 |
+
search_btn = gr.Button("๐ Search", elem_id="search-btn")
|
| 61 |
+
|
| 62 |
+
ai_output = gr.Textbox(label="AI Response")
|
| 63 |
+
search_output = gr.HTML(label="Search Results")
|
| 64 |
+
|
| 65 |
+
search_btn.click(
|
| 66 |
+
fn=google_search_query,
|
| 67 |
+
inputs=question,
|
| 68 |
+
outputs=[ai_output, search_output],
|
| 69 |
+
js=custom_js
|
| 70 |
+
)
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
+
app.launch(share=True)
|