File size: 1,030 Bytes
a125bf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.')