Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -173,7 +173,7 @@ def google_search(query):
|
|
| 173 |
|
| 174 |
'''
|
| 175 |
yield errmsg
|
| 176 |
-
|
| 177 |
else:
|
| 178 |
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={API_KEY}&cx={SEARCH_ENGINE_ID}&sort=date"
|
| 179 |
response = requests.get(url)
|
|
@@ -186,11 +186,7 @@ def google_search(query):
|
|
| 186 |
link = item.get("link", "No Link")
|
| 187 |
snippet = item.get("htmlSnippet", "No Snippet")
|
| 188 |
contents = title + "\n" + snippet
|
| 189 |
-
#print("contents:",contents)
|
| 190 |
results.append(f"{contents}: {link}")
|
| 191 |
-
#results.append(f"{title}: {link}")
|
| 192 |
-
|
| 193 |
-
#print("appended:",results)
|
| 194 |
|
| 195 |
# URLデコードを適用
|
| 196 |
decoded_urls = [urllib.parse.unquote(url) for url in results]
|
|
@@ -199,32 +195,26 @@ def google_search(query):
|
|
| 199 |
|
| 200 |
# 正規表現を使ってタイトルとURLを抽出
|
| 201 |
matches = re.findall(r'(.+?):\s*(https?://[^\s]+)', decoded)
|
| 202 |
-
#print("matches:",matches)
|
| 203 |
|
| 204 |
# 結果を表示
|
| 205 |
finale = ""
|
| 206 |
for title, url in matches:
|
| 207 |
smry = summarize_webpage(url)
|
| 208 |
-
#print("summary:",smry)
|
| 209 |
finale = finale+f'<div style="background:white;padding:15px;margin-bottom:15px;border-radius:8px;box-shadow:02px5pxrgba(0,0,0,0.1);"><a style="color: #007bff; text-decoration: none; font-weight: bold;" href="{url}" target="_blank" rel="noopener">{title}</a><br />{smry}</div>\n'
|
| 210 |
-
#finale = finale+f'<div style="background:white;padding:15px;margin-bottom:15px;border-radius:8px;box-shadow:02px5pxrgba(0,0,0,0.1);"><a style="color: #007bff; text-decoration: none; font-weight: bold;" href="{url}">{title}</a><br />{snippet}</div>\n'
|
| 211 |
-
#print(f"タイトル: {title}\nURL: {url}\n")
|
| 212 |
|
| 213 |
generated = f"""<!DOCTYPE html><html lang="ja"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>{query} - 検結果</title></head><body><div style="max-width: 800px; margin: 40px auto; padding: 20px;"><h1>{query} - 検索結果</h1><ul><li style="background:ghostwhite;border-radius:8px;box-shadow:0 2px 5px rgba(0, 0, 0, 0.1);">{finale}</li></ul></div></body></html>"""
|
| 214 |
|
| 215 |
yield generated
|
| 216 |
-
#return generated
|
| 217 |
|
| 218 |
else:
|
| 219 |
yield f"検索に失敗しました: {response.status_code}"
|
| 220 |
-
|
| 221 |
|
| 222 |
with gr.Blocks(css="footer {visibility: hidden;} #custom_button {width: 400px; margin: 0 auto; background-color: #E0E7FF;}", theme=gr.themes.Soft(), title="Google カスタム検索エージェント") as agent:
|
| 223 |
-
#gr.Markdown("## Google カスタム検索エージェント")
|
| 224 |
gr.HTML('''<div style="display: flex; justify-content: center; align-items: center; font-size: 20px; font-weight: bold; font-family: 'Noto Sans JP', 'Yu Gothic', 'ヒラギノ角ゴシック', 'メイリオ', sans-serif;">Google カスタム検索エージェント</div>''')
|
| 225 |
|
| 226 |
with gr.Column():
|
| 227 |
-
search_input = gr.Textbox(label="検索キーワードを入力",info="例)著名投資家ウォーレン・バフェット氏が米投資会社バークシャー・ハザウェイの最高経営責任者(CEO)から退く
|
| 228 |
search_output = gr.HTML(label="検索結果")
|
| 229 |
search_btn = gr.Button("検索",elem_id="custom_button")
|
| 230 |
search_btn.click(google_search, inputs=search_input, outputs=search_output)
|
|
|
|
| 173 |
|
| 174 |
'''
|
| 175 |
yield errmsg
|
| 176 |
+
|
| 177 |
else:
|
| 178 |
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={API_KEY}&cx={SEARCH_ENGINE_ID}&sort=date"
|
| 179 |
response = requests.get(url)
|
|
|
|
| 186 |
link = item.get("link", "No Link")
|
| 187 |
snippet = item.get("htmlSnippet", "No Snippet")
|
| 188 |
contents = title + "\n" + snippet
|
|
|
|
| 189 |
results.append(f"{contents}: {link}")
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
# URLデコードを適用
|
| 192 |
decoded_urls = [urllib.parse.unquote(url) for url in results]
|
|
|
|
| 195 |
|
| 196 |
# 正規表現を使ってタイトルとURLを抽出
|
| 197 |
matches = re.findall(r'(.+?):\s*(https?://[^\s]+)', decoded)
|
|
|
|
| 198 |
|
| 199 |
# 結果を表示
|
| 200 |
finale = ""
|
| 201 |
for title, url in matches:
|
| 202 |
smry = summarize_webpage(url)
|
|
|
|
| 203 |
finale = finale+f'<div style="background:white;padding:15px;margin-bottom:15px;border-radius:8px;box-shadow:02px5pxrgba(0,0,0,0.1);"><a style="color: #007bff; text-decoration: none; font-weight: bold;" href="{url}" target="_blank" rel="noopener">{title}</a><br />{smry}</div>\n'
|
|
|
|
|
|
|
| 204 |
|
| 205 |
generated = f"""<!DOCTYPE html><html lang="ja"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>{query} - 検結果</title></head><body><div style="max-width: 800px; margin: 40px auto; padding: 20px;"><h1>{query} - 検索結果</h1><ul><li style="background:ghostwhite;border-radius:8px;box-shadow:0 2px 5px rgba(0, 0, 0, 0.1);">{finale}</li></ul></div></body></html>"""
|
| 206 |
|
| 207 |
yield generated
|
|
|
|
| 208 |
|
| 209 |
else:
|
| 210 |
yield f"検索に失敗しました: {response.status_code}"
|
| 211 |
+
|
| 212 |
|
| 213 |
with gr.Blocks(css="footer {visibility: hidden;} #custom_button {width: 400px; margin: 0 auto; background-color: #E0E7FF;}", theme=gr.themes.Soft(), title="Google カスタム検索エージェント") as agent:
|
|
|
|
| 214 |
gr.HTML('''<div style="display: flex; justify-content: center; align-items: center; font-size: 20px; font-weight: bold; font-family: 'Noto Sans JP', 'Yu Gothic', 'ヒラギノ角ゴシック', 'メイリオ', sans-serif;">Google カスタム検索エージェント</div>''')
|
| 215 |
|
| 216 |
with gr.Column():
|
| 217 |
+
search_input = gr.Textbox(label="検索キーワードを入力",info="例)著名投資家ウォーレン・バフェット氏が米投資会社バークシャー・ハザウェイの最高経営責任者(CEO)から退く",placeholder="検索するキーワードを入力します。")
|
| 218 |
search_output = gr.HTML(label="検索結果")
|
| 219 |
search_btn = gr.Button("検索",elem_id="custom_button")
|
| 220 |
search_btn.click(google_search, inputs=search_input, outputs=search_output)
|