File size: 1,377 Bytes
58ceb7d
e54820e
 
 
 
b735389
e54820e
 
dc8f606
e54820e
 
 
 
 
 
b735389
e54820e
b735389
e54820e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc8f606
 
e54820e
 
 
 
 
 
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
import os
import serpapi
import gradio as gr
from langchain_google_genai import ChatGoogleGenerativeAI

from utils.utils import get_top_results, get_prompt


def main(search_query, search_location, serp_api_key):
    search_response = serpapi.search(
        q=search_query,
        location=search_location,
        api_key=serp_api_key
    ) # engine="google", location="Austin, Texas", hl="en", gl="us"
    
    top_results = get_top_results(search_response)

    prompt = get_prompt(search_query, top_results)

    chat_model = ChatGoogleGenerativeAI(
        model="gemini-pro",
        convert_system_message_to_human=True,
        temperature=0.0, # temperature=0.7 (default)
        # top_p=0.5,
        ) 
    response = chat_model.invoke(prompt)

    return response.content


if __name__ == "__main__":
    try:
        app = gr.Interface(
            fn=main,
            inputs=[
                gr.Textbox(label="Search Query (検索クエリ)"),
                gr.Textbox(label="Search Location (検索を行う国を英語で入力してください)"),
                gr.Textbox(label="SERP API Key (https://serpapi.com/manage-api-key)")
            ],
            outputs=[gr.Textbox(label="Search Result (検索結果)")],
            title="Google Search enhanced by LLM"
        )
        app.launch(share=True)
    except Exception as e:
        raise e