Spaces:
Sleeping
Sleeping
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() |