Spaces:
Sleeping
Sleeping
File size: 2,065 Bytes
be9dc3f 6ffe322 199b1c7 be9dc3f 6abbe60 4783874 be9dc3f 199b1c7 591ec6f 199b1c7 be9dc3f 199b1c7 591ec6f 199b1c7 591ec6f be9dc3f 199b1c7 be9dc3f 199b1c7 |
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 |
import gradio as gr
import requests
import os
import logging
# ๋ก๊น
์ค์
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Google Custom Search API ์ ๋ณด
API_KEY = os.getenv("JSONKEY")
CX = "c01abc75e1b95483d" # ์ฌ์ฉ์ ์ปค์คํ
๊ฒ์ ์์ง ID
def google_search(query):
logger.info(f"Searching for query: {query}")
url = f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={CX}&q={query}&num=10"
logger.debug(f"Request URL: {url}")
try:
response = requests.get(url)
response.raise_for_status() # HTTP ์ค๋ฅ๊ฐ ์์ผ๋ฉด ์์ธ๋ฅผ ๋ฐ์์ํต๋๋ค.
search_results = response.json()
logger.debug(f"API Response: {search_results}")
results = []
if 'items' in search_results:
for item in search_results['items'][:10]: # ์ต๋ 10๊ฐ์ ๊ฒฐ๊ณผ๋ง ์ฒ๋ฆฌ
title = item['title']
link = item['link']
snippet = item.get('snippet', '')
results.append(f"Title: {title}\nLink: {link}\nSnippet: {snippet}\n\n")
else:
logger.warning("No items found in search results")
if 'error' in search_results:
error_message = search_results['error']['message']
logger.error(f"API Error: {error_message}")
results.append(f"Error: {error_message}")
else:
results.append("No results found")
return '\n'.join(results[:10]) # ์ต๋ 10๊ฐ์ ๊ฒฐ๊ณผ๋ง ๋ฐํ
except requests.exceptions.RequestException as e:
logger.error(f"Request failed: {e}")
return f"An error occurred: {e}"
# Gradio ์ธํฐํ์ด์ค ์ค์
iface = gr.Interface(
fn=google_search,
inputs="text",
outputs="text",
title="Google Custom Search",
description="Enter a search query to get results from Google Custom Search API."
)
# ์ธํฐํ์ด์ค ์คํ
if __name__ == "__main__":
logger.info("Starting Gradio interface")
iface.launch() |