blazingbunny commited on
Commit
d111a26
·
verified ·
1 Parent(s): bb352cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -26
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import nest_asyncio
2
  import json
 
 
 
3
  from scrapegraphai.graphs import SearchGraph
4
- import streamlit as st # This import is necessary to access secrets in Streamlit
5
 
6
  # Apply necessary settings for asyncio compatibility in Jupyter/Streamlit environments
7
  nest_asyncio.apply()
@@ -9,28 +11,51 @@ nest_asyncio.apply()
9
  # Access your API keys securely
10
  OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
11
 
12
- # Define the configuration for the graph
13
- graph_config = {
14
- "llm": {
15
- "api_key": OPENAI_API_KEY, # Use the secure variable to pass the actual API key
16
- "model": "gpt-3.5-turbo",
17
- "temperature": 0,
18
- },
19
- }
20
-
21
- # Create the SearchGraph instance with a prompt
22
- search_graph = SearchGraph(
23
- prompt="List me all the attributes of 'cannabis strain'.",
24
- config=graph_config
25
- )
26
-
27
- try:
28
- # Run the graph to fetch results
29
- result = search_graph.run()
30
- # Convert the result to a JSON string with indentation for better readability
31
- output = json.dumps(result, indent=2)
32
- # Print each line of the JSON output
33
- for line in output.split("\n"):
34
- print(line)
35
- except Exception as e:
36
- print(f"An error occurred: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import nest_asyncio
2
  import json
3
+ import streamlit as st # This import is necessary to access secrets and for the web interface
4
+ from selenium import webdriver
5
+ from chromedriver_binary import add_chromedriver_to_path
6
  from scrapegraphai.graphs import SearchGraph
 
7
 
8
  # Apply necessary settings for asyncio compatibility in Jupyter/Streamlit environments
9
  nest_asyncio.apply()
 
11
  # Access your API keys securely
12
  OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
13
 
14
+ # Selenium setup function
15
+ def setup_selenium():
16
+ add_chromedriver_to_path()
17
+ options = webdriver.ChromeOptions()
18
+ options.add_argument("--headless")
19
+ options.add_argument("--no-sandbox")
20
+ options.add_argument("--disable-dev-shm-usage")
21
+ driver = webdriver.Chrome(options=options)
22
+ return driver
23
+
24
+ def get_web_page_title(url):
25
+ driver = setup_selenium()
26
+ driver.get(url)
27
+ title = driver.title
28
+ driver.quit()
29
+ return title
30
+
31
+ st.title("Hybrid AI and Web Scraping Application")
32
+ url = st.text_input("Enter the URL to scrape for title")
33
+
34
+ if st.button("Scrape for Title"):
35
+ title = get_web_page_title(url)
36
+ st.write(f"The title of the page is: {title}")
37
+
38
+ query_prompt = st.text_input("Enter your AI query", value="List me all the attributes of 'cannabis strain'.")
39
+
40
+ if st.button("Fetch Data from AI"):
41
+ # Define the configuration for the graph based on user input
42
+ graph_config = {
43
+ "llm": {
44
+ "api_key": OPENAI_API_KEY,
45
+ "model": "gpt-3.5-turbo",
46
+ "temperature": 0,
47
+ },
48
+ }
49
+
50
+ # Create the SearchGraph instance dynamically
51
+ search_graph = SearchGraph(prompt=query_prompt, config=graph_config)
52
+
53
+ try:
54
+ # Run the graph to fetch results
55
+ result = search_graph.run()
56
+ # Convert the result to a JSON string with indentation for better readability
57
+ output = json.dumps(result, indent=2)
58
+ # Display each line of the JSON output
59
+ st.text_area("Result", value=output, height=300)
60
+ except Exception as e:
61
+ st.error(f"An error occurred: {e}")