Spaces:
Sleeping
Sleeping
blazingbunny
commited on
Commit
•
a125bf7
1
Parent(s):
7dfb1a4
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
|
5 |
+
# Function to fetch data from Google Knowledge Graph
|
6 |
+
def fetch_data(query, api_key):
|
7 |
+
url = f"https://kgsearch.googleapis.com/v1/entities:search?query={query}&key={api_key}&limit=1"
|
8 |
+
response = requests.get(url)
|
9 |
+
json_data = json.loads(response.text)
|
10 |
+
return json_data
|
11 |
+
|
12 |
+
# Streamlit UI
|
13 |
+
st.title('Google Knowledge Graph Search')
|
14 |
+
|
15 |
+
api_key = st.text_input('Enter your Google API Key:', type="password")
|
16 |
+
search_query = st.text_input('Enter Search Query:', 'Python Programming')
|
17 |
+
|
18 |
+
if st.button('Search'):
|
19 |
+
if api_key:
|
20 |
+
result = fetch_data(search_query, api_key)
|
21 |
+
try:
|
22 |
+
item = result['itemListElement'][0]['result']
|
23 |
+
st.subheader(f"Name: {item['name']}")
|
24 |
+
st.write(f"Description: {item.get('description', 'N/A')}")
|
25 |
+
st.write(f"Detail: {item.get('detailedDescription', {}).get('articleBody', 'N/A')}")
|
26 |
+
except:
|
27 |
+
st.write('No results found.')
|
28 |
+
else:
|
29 |
+
st.write('Please enter an API key.')
|