gkg-sandbox / main.py
blazingbunny's picture
Create main.py
a125bf7
raw
history blame
1.03 kB
import streamlit as st
import requests
import json
# Function to fetch data from Google Knowledge Graph
def fetch_data(query, api_key):
url = f"https://kgsearch.googleapis.com/v1/entities:search?query={query}&key={api_key}&limit=1"
response = requests.get(url)
json_data = json.loads(response.text)
return json_data
# Streamlit UI
st.title('Google Knowledge Graph Search')
api_key = st.text_input('Enter your Google API Key:', type="password")
search_query = st.text_input('Enter Search Query:', 'Python Programming')
if st.button('Search'):
if api_key:
result = fetch_data(search_query, api_key)
try:
item = result['itemListElement'][0]['result']
st.subheader(f"Name: {item['name']}")
st.write(f"Description: {item.get('description', 'N/A')}")
st.write(f"Detail: {item.get('detailedDescription', {}).get('articleBody', 'N/A')}")
except:
st.write('No results found.')
else:
st.write('Please enter an API key.')