import gradio as gr from google import genai from google.genai import types import os from dotenv import load_dotenv # Load environment variables load_dotenv() # Configure the Gemini client # NOTE: Make sure your GEMINI_API_KEY is set in your .env file client = genai.Client(api_key=os.environ["GEMINI_API_KEY"]) css = 'footer {visibility: hidden;} #header {display: flex; justify-content: space-between; align-items: center; font-size: 24px; font-weight: bold;} #logo {width: 50px; height: 50px;}' # Define the grounding tool grounding_tool = types.Tool( google_search=types.GoogleSearch() ) template = """ PwC大手町オフィス アクセス案内

Access Guide

PwC大手町オフィスへのご案内

亀有駅 START
約25-30分
大手町 GOAL
推奨出発時刻
10:30 頃 (亀有発)
到着予定時刻
11:00 (オフィス着)
PwC大手町オフィス 東京都千代田区大手町1-1-1
大手町パークビルディング
Googleマップで経路を見る
""" ''' google_maps_tool = types.Tool( google_maps=types.GoogleMaps() ) # チャットモデルにツールを渡して、場所に関する質問をする response = client.chat( model="google/gemini-pro", messages=[ { "role": "user", "content": "東京タワーの住所はどこですか?" } ], tools=[google_maps_tool] ) # 応答を出力 print(response.text) ''' # Define the generation settings config = types.GenerateContentConfig( tools=[grounding_tool] ) def change_input(prompt): if len(prompt) > 0: return gr.update(visible=True) else: return gr.update(visible=False) # --- Python function for the Gradio app --- def generate_response(prompt): """ This function takes a user's prompt, calls the Gemini API with the Google Search tool, and returns the response text. """ try: final_prompt = prompt + f'尚、訪問先の住所を調べ、必ず、到着時刻を含めて、訪問先までのgoogle mapの経路のURL目的地形式で教えてください。urlはhttps://www.google.com/maps/dir/%E4%BA%80%E6%9C%89%E9%A7%85/%E6%9D%B1%E4%BA%AC%E9%83%BD%E5%8D%83%E4%BB%A3%E7%94%B0%E5%8C%BA%E5%A4%A7%E6%89%8B%E7%94%BA1-1-1+%E5%A4%A7%E6%89%8B%E7%94%BA%E3%83%91%E3%83%BC%E3%82%AF%E3%83%93%E3%83%AB%E3%83%87%E3%82%A3%E3%83%B3%E3%82%B0/@35.7223676,139.7267896,12z/data=!3m1!4b1!4m14!4m13!1m5!1m1!1s0x60188fc1f92c3cf3:0x1b439c2c8f8d689b!2m2!1d139.845832!2d35.760278!1m5!1m1!1s0x60188c0e2719d3df:0x8544d6731e84a275!2m2!1d139.762585!2d35.686749!3e3?entry=ttu&g_ep=EgoyMDI1MDgxMi4wIKXMDSoASAFQAw%3D%3D形式にしてください。必ず、答えは{template}に基づいてHTML形式にしてください' response = client.models.generate_content( model="gemini-2.5-flash", contents=final_prompt, config=config, ) #print(response.text) final_html = response.text final_html = final_html.replace("```html", "") final_html = final_html.replace("```", "") return final_html except Exception as e: return f"An error occurred: {e}" # --- Gradio UI Block --- with gr.Blocks(title="Gemini API with Google Search") as gsearch: gr.Markdown( """ # 🗾 Map Search Tool 日本語で質問すると、Gemini は Google 検索を使用して根拠のある回答を提供します。 Ask a question in Japanese, and Gemini will use Google Search to provide a grounded response. """ ) # Input component for the user's question user_input = gr.Textbox( label="あなたの質問:", info="例: PWC大手町オフィスを11時に訪問する予定です。自由が丘からの経路を教えてください。", lines=2 ) output_text = gr.HTML( label="目的地までの経路:", value='
結果
' ) # Button to trigger the API call submit_button = gr.Button("生成", visible=False) user_input.change(fn=change_input, inputs=[user_input], outputs=[submit_button]) # Connect the components: when the button is clicked, # the generate_response function is called with the user_input, # and the result is displayed in the output_text box. submit_button.click( fn=generate_response, inputs=user_input, outputs=output_text ) # Launch the Gradio app if __name__ == "__main__": gsearch.launch(favicon_path="path.ico", css=css, ssr_mode=False)