cyberandy commited on
Commit
8c61d2a
·
verified ·
1 Parent(s): c894b08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -102
app.py CHANGED
@@ -71,114 +71,158 @@ elif not openai_api_key:
71
  st.error("The OpenAI API key is not properly configured. Check your environment variables!")
72
  else:
73
  # Generate the report by calling the ChatGPT Turbo API and the WooRank API
 
 
 
 
 
 
 
 
 
 
74
  def analyze_data(_advice, _items, _topics, _issues, _technologies, openai_api_key):
75
  """
76
- :param _advice: A list of strings, each string is a piece of advice
77
- :param _items: a list of items that are being analyzed
78
- :param _topics: a list of topics that the user is interested in
79
- :param _issues: a list of issues that the user has selected
80
- :param _technologies: A list of technologies that the user has selected
81
- """
82
- # Create the system message for ChatGPT Turbo
83
- prefix_messages = [{"role": "system", "content": '''You are a helpful and truthful SEO that is very good at analyzing websites with a specific focus on structured data. /n
84
- You are able to provide a detailed report on the website's structured data and how to improve it. /n
85
- ADD AS LEARN MORE LINKS FOR THE FIRST TEXT BLOCK LINKS TO structured data https://wordlift.io/blog/en/entity/structured-data/ and schema.org https://wordlift.io/blog/en/entity/schema-org/ TO PROVIDE ADDITIONAL HELP./n/n
86
- YOU ARE WRITING THE REPORT IN HTML USING A TEMPLATE.'''}]
87
-
88
- client = OpenAI(api_key=openai_api_key)
89
 
90
- # Construct messages for the chat API
91
- messages = []
92
- messages.extend(prefix_messages)
93
-
94
-
95
- # Create the prompt template and the run statement when there are NOT issues
96
- if not _issues and len(_items) > 0:
97
- template = """
98
- First text block of the report./n
99
- Analyze the: {advice}, consider that the site features the following schema classes: {items}./n/n
100
-
101
- Second text block of the report./n
102
- The website's homepage also references the following entities: {topics} that could be used to improve the SEO of the website further./n/n
103
-
104
- Third text block of the report./n
105
- Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do./n/n
106
-
107
- THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:/n
108
- "first": "First text block with schema classes in <i>italic</i>",
109
- "second": "Second text block with entities in <b>bold</b>",
110
- "third": "Third text block with technologies in <i>italic</i>"
111
- """
112
- prompt = PromptTemplate(template=template, input_variables=[
113
- "advice", "items", "topics", "technologies"])
114
- run_statement = {"advice": _advice, "items": _items,
115
- "topics": _topics, "technologies": _technologies}
116
-
117
- # Create the prompt template and the run statement when there ARE NOT schema classes
118
- elif not _items:
119
- template = """
120
- First text block of the report./n
121
- The website homepage doesn't seem to feature any schema class./n/n
122
-
123
- Second text block of the report./n
124
- The website's homepage also references the following entities: {topics} that can be used to improve the SEO of the website./n/n
125
-
126
- Third text block of the report./n
127
- Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do./n/n
128
-
129
- THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:/n
130
- "first": "First text block",
131
- "second": "Second text block with entities in <b>bold</b>"
132
- "third": "Third text block with technologies in <i>italic</i>"
133
- """
134
- prompt = PromptTemplate(template=template, input_variables=[
135
- "topics", "technologies"])
136
- run_statement = {"topics": _topics, "technologies": _technologies}
137
-
138
- # Create the prompt template and the run statement when there ARE issues
139
- else:
140
- template = """
141
- First text block of the report./n
142
- Analyze the: {advice}, consider that the site features the following schema classes: {items}./n/n
143
-
144
- Second text block of the report. /n
145
- Describe the following issues with the markup: {issues} and indicate how to fix them./n/n
146
-
147
- Third text block of the report./n
148
- The website's homepage also references the following entities: {topics} that could be used to improve the SEO of the website further./n/n
149
-
150
- Fourth text block of the report./n
151
- Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do./n/n
152
-
153
- THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:/n
154
- "first": "First text block with schema classes in <i>italic</i>",
155
- "second": "Second text block with issues in <u>underline</u>",
156
- "third": "Third text block with entities in <b>bold</b>"
157
- "fourth": "Fourth text block with technologies in <i>italic</i>"
158
- """
159
- prompt = PromptTemplate(template=template, input_variables=[
160
- "advice", "items", "topics", "issues", "technologies"])
161
- run_statement = {"advice": _advice, "items": _items,
162
- "topics": _topics, "issues": _issues, "technologies": _technologies}
163
 
164
- # Format the prompt
165
- user_message = prompt.format(**run_statement)
166
- messages.append({"role": "user", "content": user_message})
167
-
168
- # Make the API call
169
  try:
170
- response = client.chat.completions.create(
171
- model="gpt-4o",
172
- messages=messages
173
- )
174
- out = response.choices[0].message.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  except Exception as e:
176
- out = f"Sorry, there was an error with the OpenAI API: {e}"
177
-
178
-
179
-
180
- return out
181
-
182
  # Call WooRank API to get the data (cached)
183
  @st.cache_data
184
  def get_woorank_data(url):
 
71
  st.error("The OpenAI API key is not properly configured. Check your environment variables!")
72
  else:
73
  # Generate the report by calling the ChatGPT Turbo API and the WooRank API
74
+
75
+ # First, let's create a simple PromptTemplate class since it's not imported
76
+ class PromptTemplate:
77
+ def __init__(self, template, input_variables):
78
+ self.template = template
79
+ self.input_variables = input_variables
80
+
81
+ def format(self, **kwargs):
82
+ return self.template.format(**kwargs)
83
+
84
  def analyze_data(_advice, _items, _topics, _issues, _technologies, openai_api_key):
85
  """
86
+ Analyzes website data and generates a structured report using OpenAI's GPT model.
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
+ Args:
89
+ _advice (list): A list of strings, each string is a piece of advice
90
+ _items (list): A list of items that are being analyzed
91
+ _topics (list): A list of topics that the user is interested in
92
+ _issues (list): A list of issues that the user has selected
93
+ _technologies (list): A list of technologies that the user has selected
94
+ openai_api_key (str): The OpenAI API key
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ Returns:
97
+ str: A JSON-formatted string containing the analysis report
98
+ """
 
 
99
  try:
100
+ # Create the system message for ChatGPT
101
+ prefix_messages = [{
102
+ "role": "system",
103
+ "content": '''You are a helpful and truthful SEO that is very good at analyzing websites with a specific focus on structured data.
104
+ You are able to provide a detailed report on the website's structured data and how to improve it.
105
+ ADD AS LEARN MORE LINKS FOR THE FIRST TEXT BLOCK LINKS TO structured data https://wordlift.io/blog/en/entity/structured-data/ and schema.org https://wordlift.io/blog/en/entity/schema-org/ TO PROVIDE ADDITIONAL HELP.
106
+ YOU ARE WRITING THE REPORT IN HTML USING A TEMPLATE.'''
107
+ }]
108
+
109
+ # Initialize OpenAI client
110
+ client = OpenAI(api_key=openai_api_key)
111
+
112
+ # Construct messages for the chat API
113
+ messages = []
114
+ messages.extend(prefix_messages)
115
+
116
+ # Create the prompt template and run statement based on conditions
117
+ if not _issues and len(_items) > 0:
118
+ # Case 1: When there are NO issues but there ARE items
119
+ template = """
120
+ First text block of the report.
121
+ Analyze the: {advice}, consider that the site features the following schema classes: {items}.
122
+
123
+ Second text block of the report.
124
+ The website's homepage also references the following entities: {topics} that could be used to improve the SEO of the website further.
125
+
126
+ Third text block of the report.
127
+ Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do.
128
+
129
+ THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:
130
+ "first": "First text block with schema classes in <i>italic</i>",
131
+ "second": "Second text block with entities in <b>bold</b>",
132
+ "third": "Third text block with technologies in <i>italic</i>"
133
+ """
134
+ prompt = PromptTemplate(
135
+ template=template,
136
+ input_variables=["advice", "items", "topics", "technologies"]
137
+ )
138
+ run_statement = {
139
+ "advice": _advice,
140
+ "items": _items,
141
+ "topics": _topics,
142
+ "technologies": _technologies
143
+ }
144
+
145
+ elif not _items:
146
+ # Case 2: When there are NO schema classes
147
+ template = """
148
+ First text block of the report.
149
+ The website homepage doesn't seem to feature any schema class.
150
+
151
+ Second text block of the report.
152
+ The website's homepage also references the following entities: {topics} that can be used to improve the SEO of the website.
153
+
154
+ Third text block of the report.
155
+ Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do.
156
+
157
+ THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:
158
+ "first": "First text block",
159
+ "second": "Second text block with entities in <b>bold</b>",
160
+ "third": "Third text block with technologies in <i>italic</i>"
161
+ """
162
+ prompt = PromptTemplate(
163
+ template=template,
164
+ input_variables=["topics", "technologies"]
165
+ )
166
+ run_statement = {
167
+ "topics": _topics,
168
+ "technologies": _technologies
169
+ }
170
+
171
+ else:
172
+ # Case 3: When there ARE issues
173
+ template = """
174
+ First text block of the report.
175
+ Analyze the: {advice}, consider that the site features the following schema classes: {items}.
176
+
177
+ Second text block of the report.
178
+ Describe the following issues with the markup: {issues} and indicate how to fix them.
179
+
180
+ Third text block of the report.
181
+ The website's homepage also references the following entities: {topics} that could be used to improve the SEO of the website further.
182
+
183
+ Fourth text block of the report.
184
+ Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do.
185
+
186
+ THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:
187
+ "first": "First text block with schema classes in <i>italic</i>",
188
+ "second": "Second text block with issues in <u>underline</u>",
189
+ "third": "Third text block with entities in <b>bold</b>",
190
+ "fourth": "Fourth text block with technologies in <i>italic</i>"
191
+ """
192
+ prompt = PromptTemplate(
193
+ template=template,
194
+ input_variables=["advice", "items", "topics", "issues", "technologies"]
195
+ )
196
+ run_statement = {
197
+ "advice": _advice,
198
+ "items": _items,
199
+ "topics": _topics,
200
+ "issues": _issues,
201
+ "technologies": _technologies
202
+ }
203
+
204
+ # Format the prompt and add it to messages
205
+ user_message = prompt.format(**run_statement)
206
+ messages.append({"role": "user", "content": user_message})
207
+
208
+ # Make the API call
209
+ try:
210
+ response = client.chat.completions.create(
211
+ model="gpt-4", # Fixed model name from "gpt-4o"
212
+ messages=messages
213
+ )
214
+ out = response.choices[0].message.content
215
+ except Exception as e:
216
+ out = f"Sorry, there was an error with the OpenAI API: {e}"
217
+ print(f"OpenAI API Error: {str(e)}") # Log the error for debugging
218
+
219
+ return out
220
+
221
  except Exception as e:
222
+ error_message = f"An unexpected error occurred: {str(e)}"
223
+ print(error_message) # Log the error for debugging
224
+ return error_message
225
+
 
 
226
  # Call WooRank API to get the data (cached)
227
  @st.cache_data
228
  def get_woorank_data(url):