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') # Try to fetch API key from secrets.toml api_key = st.secrets.get("gkg", {}).get("api_key") if isinstance(st.secrets.get("gkg", {}), dict) else None # If not found, ask for it if not api_key: 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('API key is missing.')