Spaces:
Runtime error
Runtime error
import nest_asyncio | |
import json | |
from scrapegraphai.graphs import SearchGraph | |
import streamlit as st # This import is necessary to access secrets in Streamlit | |
# Apply necessary settings for asyncio compatibility in Jupyter/Streamlit environments | |
nest_asyncio.apply() | |
# Access your API keys securely | |
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"] | |
# Define the configuration for the graph | |
graph_config = { | |
"llm": { | |
"api_key": OPENAI_API_KEY, # Use the secure variable to pass the actual API key | |
"model": "gpt-3.5-turbo", | |
"temperature": 0, | |
}, | |
} | |
# Create the SearchGraph instance with a prompt | |
search_graph = SearchGraph( | |
prompt="List me all the attributes of 'cannabis strain'.", | |
config=graph_config | |
) | |
try: | |
# Run the graph to fetch results | |
result = search_graph.run() | |
# Convert the result to a JSON string with indentation for better readability | |
output = json.dumps(result, indent=2) | |
# Print each line of the JSON output | |
for line in output.split("\n"): | |
print(line) | |
except Exception as e: | |
print(f"An error occurred: {e}") |