Spaces:
Sleeping
Sleeping
seawolf2357
commited on
Commit
β’
199b1c7
1
Parent(s):
3faab38
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
# Google Custom Search API μ 보
|
5 |
API_KEY = "b1f91174137fc40bdcc7b443aebaa75b985a9f57"
|
6 |
CX = "c01abc75e1b95483d" # μ¬μ©μ 컀μ€ν
κ²μ μμ§ ID
|
7 |
|
8 |
def google_search(query):
|
|
|
9 |
url = f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={CX}&q={query}"
|
10 |
-
|
11 |
-
search_results = response.json()
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
|
|
|
|
24 |
|
25 |
# Gradio μΈν°νμ΄μ€ μ€μ
|
26 |
iface = gr.Interface(
|
@@ -32,4 +54,6 @@ iface = gr.Interface(
|
|
32 |
)
|
33 |
|
34 |
# μΈν°νμ΄μ€ μ€ν
|
35 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import os
|
4 |
+
import logging
|
5 |
+
|
6 |
+
# λ‘κΉ
μ€μ
|
7 |
+
logging.basicConfig(level=logging.DEBUG)
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
# Google Custom Search API μ 보
|
11 |
API_KEY = "b1f91174137fc40bdcc7b443aebaa75b985a9f57"
|
12 |
CX = "c01abc75e1b95483d" # μ¬μ©μ 컀μ€ν
κ²μ μμ§ ID
|
13 |
|
14 |
def google_search(query):
|
15 |
+
logger.info(f"Searching for query: {query}")
|
16 |
url = f"https://www.googleapis.com/customsearch/v1?key={API_KEY}&cx={CX}&q={query}"
|
17 |
+
logger.debug(f"Request URL: {url}")
|
|
|
18 |
|
19 |
+
try:
|
20 |
+
response = requests.get(url)
|
21 |
+
response.raise_for_status() # HTTP μ€λ₯κ° μμΌλ©΄ μμΈλ₯Ό λ°μμν΅λλ€.
|
22 |
+
search_results = response.json()
|
23 |
+
logger.debug(f"API Response: {search_results}")
|
24 |
+
|
25 |
+
results = []
|
26 |
+
if 'items' in search_results:
|
27 |
+
for item in search_results['items']:
|
28 |
+
title = item['title']
|
29 |
+
link = item['link']
|
30 |
+
snippet = item.get('snippet', '')
|
31 |
+
results.append(f"Title: {title}\nLink: {link}\nSnippet: {snippet}\n\n")
|
32 |
+
else:
|
33 |
+
logger.warning("No items found in search results")
|
34 |
+
if 'error' in search_results:
|
35 |
+
error_message = search_results['error']['message']
|
36 |
+
logger.error(f"API Error: {error_message}")
|
37 |
+
results.append(f"Error: {error_message}")
|
38 |
+
else:
|
39 |
+
results.append("No results found")
|
40 |
+
|
41 |
+
return '\n'.join(results)
|
42 |
|
43 |
+
except requests.exceptions.RequestException as e:
|
44 |
+
logger.error(f"Request failed: {e}")
|
45 |
+
return f"An error occurred: {e}"
|
46 |
|
47 |
# Gradio μΈν°νμ΄μ€ μ€μ
|
48 |
iface = gr.Interface(
|
|
|
54 |
)
|
55 |
|
56 |
# μΈν°νμ΄μ€ μ€ν
|
57 |
+
if __name__ == "__main__":
|
58 |
+
logger.info("Starting Gradio interface")
|
59 |
+
iface.launch()
|