Spaces:
Sleeping
Sleeping
poemsforaphrodite
commited on
Commit
•
f30d64a
1
Parent(s):
c9ec2b5
Update app.py
Browse files
app.py
CHANGED
@@ -32,14 +32,13 @@ if not APIFY_API_TOKEN:
|
|
32 |
st.error("APIFY_API_TOKEN is not set in the environment variables. Please set it and restart the application.")
|
33 |
|
34 |
# Initialize the ApifyClient with the API token
|
35 |
-
|
36 |
-
# Initialize the ApifyClient with the API token
|
37 |
|
38 |
# Initialize OpenAI client
|
39 |
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
40 |
if not OPENAI_API_KEY:
|
41 |
st.error("OPENAI_API_KEY is not set in the environment variables. Please set it and restart the application.")
|
42 |
-
|
43 |
|
44 |
# Configuration: Set to True if running locally, False if running on Streamlit Cloud
|
45 |
IS_LOCAL = False
|
@@ -125,16 +124,11 @@ def get_serp_results(query):
|
|
125 |
}
|
126 |
|
127 |
try:
|
128 |
-
#logger.debug(f"Calling Apify Actor with input: {run_input}")
|
129 |
# Run the Actor and wait for it to finish
|
130 |
-
run =
|
131 |
-
# logger.info(f"Apify Actor run completed. Run ID: {run.get('id')}")
|
132 |
|
133 |
# Fetch results from the run's dataset
|
134 |
-
|
135 |
-
#logger.debug(f"Fetching results from dataset ID: {run.get('defaultDatasetId')}")
|
136 |
-
results = list(client.dataset(run["defaultDatasetId"]).iterate_items())
|
137 |
-
# logger.info(f"Fetched {len(results)} results from Apify dataset")
|
138 |
|
139 |
if results and 'organicResults' in results[0]:
|
140 |
serp_data = []
|
@@ -144,11 +138,9 @@ def get_serp_results(query):
|
|
144 |
serp_data.append({'url': url, 'content': content})
|
145 |
return serp_data
|
146 |
else:
|
147 |
-
# logger.warning("No organic results found in the SERP data.")
|
148 |
st.warning("No organic results found in the SERP data.")
|
149 |
return []
|
150 |
except Exception as e:
|
151 |
-
# logger.exception(f"Error fetching SERP results: {str(e)}")
|
152 |
st.error(f"Error fetching SERP results: {str(e)}")
|
153 |
return []
|
154 |
|
@@ -157,13 +149,13 @@ def get_serp_results(query):
|
|
157 |
|
158 |
def extract_relevant_content(full_content, query):
|
159 |
try:
|
160 |
-
response =
|
161 |
-
model="gpt-
|
162 |
messages=[
|
163 |
{"role": "system", "content": "You are a helpful assistant that extracts the most relevant content from web pages."},
|
164 |
-
{"role": "user", "content": f"Given the following web page content and search query, extract only the most relevant parts of the content that answer or relate to the query.If there's no relevant content, say 'No relevant content found.'\n\nQuery: {query}\n\nContent: {full_content[:4000]}"} # Limit input to 4000 characters
|
165 |
],
|
166 |
-
max_tokens=
|
167 |
)
|
168 |
return response.choices[0].message.content.strip()
|
169 |
except Exception as e:
|
|
|
32 |
st.error("APIFY_API_TOKEN is not set in the environment variables. Please set it and restart the application.")
|
33 |
|
34 |
# Initialize the ApifyClient with the API token
|
35 |
+
apify_client = ApifyClient(APIFY_API_TOKEN)
|
|
|
36 |
|
37 |
# Initialize OpenAI client
|
38 |
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY')
|
39 |
if not OPENAI_API_KEY:
|
40 |
st.error("OPENAI_API_KEY is not set in the environment variables. Please set it and restart the application.")
|
41 |
+
openai_client = OpenAI(api_key=OPENAI_API_KEY)
|
42 |
|
43 |
# Configuration: Set to True if running locally, False if running on Streamlit Cloud
|
44 |
IS_LOCAL = False
|
|
|
124 |
}
|
125 |
|
126 |
try:
|
|
|
127 |
# Run the Actor and wait for it to finish
|
128 |
+
run = apify_client.actor("nFJndFXA5zjCTuudP").call(run_input=run_input)
|
|
|
129 |
|
130 |
# Fetch results from the run's dataset
|
131 |
+
results = list(apify_client.dataset(run["defaultDatasetId"]).iterate_items())
|
|
|
|
|
|
|
132 |
|
133 |
if results and 'organicResults' in results[0]:
|
134 |
serp_data = []
|
|
|
138 |
serp_data.append({'url': url, 'content': content})
|
139 |
return serp_data
|
140 |
else:
|
|
|
141 |
st.warning("No organic results found in the SERP data.")
|
142 |
return []
|
143 |
except Exception as e:
|
|
|
144 |
st.error(f"Error fetching SERP results: {str(e)}")
|
145 |
return []
|
146 |
|
|
|
149 |
|
150 |
def extract_relevant_content(full_content, query):
|
151 |
try:
|
152 |
+
response = openai_client.chat.completions.create(
|
153 |
+
model="gpt-3.5-turbo",
|
154 |
messages=[
|
155 |
{"role": "system", "content": "You are a helpful assistant that extracts the most relevant content from web pages."},
|
156 |
+
{"role": "user", "content": f"Given the following web page content and search query, extract only the most relevant parts of the content that answer or relate to the query. Limit your response to about 1000 characters. If there's no relevant content, say 'No relevant content found.'\n\nQuery: {query}\n\nContent: {full_content[:4000]}"} # Limit input to 4000 characters
|
157 |
],
|
158 |
+
max_tokens=500 # Adjust as needed
|
159 |
)
|
160 |
return response.choices[0].message.content.strip()
|
161 |
except Exception as e:
|