Spaces:
Sleeping
Sleeping
File size: 4,288 Bytes
a48239c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
import gradio as gr
from google import genai
from smolagents import DuckDuckGoSearchTool
# Initialize tools
search_tool = DuckDuckGoSearchTool()
def search_answer(question: str, api_key: str) -> str:
"""Fetches search results and generates an answer using Gemini AI."""
result = search_tool(question)
prompt = f"""
Based on the search results, please provide a concise answer to the following question:
Question: {question}
Search Results: {result}
Please ensure your response is:
1. Directly based on the search results
2. Brief and to the point
"""
client = genai.Client(api_key=api_key)
response = client.models.generate_content(
model='gemini-2.0-flash',
contents=[prompt]
)
return response.text if response else "No response generated."
# Improved Gradio Interface with Better UI
custom_css = """
.gradio-container {
background: linear-gradient(to bottom right, #1a1a2e, #16213e);
color: #e6e6e6;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.gradio-container h1, .gradio-container h2, .gradio-container h3 {
color: #4cc9f0;
font-weight: 600;
text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.3);
}
.gradio-container .prose p {
color: #b8c1ec;
font-size: 1.1em;
margin-bottom: 1.5em;
}
.gradio-container input, .gradio-container textarea {
background-color: rgba(255, 255, 255, 0.05) !important;
border: 1px solid rgba(255, 255, 255, 0.1) !important;
color: #000000 !important;
border-radius: 8px !important;
transition: all 0.3s ease;
font-weight: 700 !important;
}
.gradio-container input:focus, .gradio-container textarea:focus {
border-color: #4cc9f0 !important;
box-shadow: 0 0 0 2px rgba(76, 201, 240, 0.25) !important;
}
.gradio-container button {
background: linear-gradient(to right, #4361ee, #4cc9f0) !important;
border: none !important;
color: white !important;
border-radius: 8px !important;
font-weight: 600 !important;
transition: all 0.3s ease !important;
}
.gradio-container button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 4px 12px rgba(76, 201, 240, 0.3) !important;
}
.gradio-container .output-text {
background-color: rgba(255, 255, 255, 0.03) !important;
border: 1px solid rgba(255, 255, 255, 0.1) !important;
border-radius: 8px !important;
padding: 15px !important;
color: #ffffff !important;
font-weight: 500 !important;
}
.footer {
margin-top: 20px;
text-align: center;
color: #b8c1ec;
font-size: 0.9em;
}
.container {
max-width: 900px;
margin: 0 auto;
padding: 20px;
}
.header-icon {
font-size: 2.5em;
margin-bottom: 10px;
background: linear-gradient(to right, #4361ee, #4cc9f0);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
"""
# Add API key input field
api_key = gr.Textbox(
placeholder="Enter your Gemini API Key",
label="๐ API Key",
type="password",
interactive=True,
elem_id="api-key-input"
)
demo = gr.Interface(
fn=search_answer,
inputs=[
gr.Textbox(
label="๐ Ask a Question",
placeholder="Type your question here...",
lines=3,
elem_id="question-input"
),
api_key
],
outputs=gr.Textbox(
label="๐ค AI Answer",
interactive=False,
elem_id="answer-output",
elem_classes=["output-text"]
),
title="โจ Smart AI Search Assistant",
description="""<div class='header-icon'>๐ฎ</div>
<p>Enter your question below and get a concise answer based on real-time web search results.</p>
<p>Our AI assistant will analyze the search data and provide you with the most relevant information.</p>""",
article="""<div class='footer'>
<p>Powered by Gemini AI and DuckDuckGo Search | Created with โค๏ธ</p>
</div>""",
css=custom_css,
theme=gr.themes.Base(),
allow_flagging="never"
)
demo.launch(share=True)
|