cyberandy commited on
Commit
73367c1
·
verified ·
1 Parent(s): 96a1157

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -47
app.py CHANGED
@@ -102,12 +102,19 @@ else:
102
  """
103
  try:
104
  # Create the system message for ChatGPT
105
- prefix_messages = [{
106
  "role": "system",
107
- "content": '''You are a helpful and truthful SEO that is very good at analyzing websites with a specific focus on structured data.
108
- You are able to provide a detailed report on the website's structured data and how to improve it.
109
- 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.
110
- YOU ARE WRITING THE REPORT IN HTML USING A TEMPLATE.'''
 
 
 
 
 
 
 
111
  }]
112
 
113
  # Initialize OpenAI client with basic configuration
@@ -125,19 +132,17 @@ else:
125
  if not _issues and len(_items) > 0:
126
  # Case 1: When there are NO issues but there ARE items
127
  template = """
128
- First text block of the report.
129
- Analyze the: {advice}, consider that the site features the following schema classes: {items}.
130
-
131
- Second text block of the report.
132
- The website's homepage also references the following entities: {topics} that could be used to improve the SEO of the website further.
133
-
134
- Third text block of the report.
135
- Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do.
136
 
137
- THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:
138
- "first": "First text block with schema classes in <i>italic</i>",
139
- "second": "Second text block with entities in <b>bold</b>",
140
- "third": "Third text block with technologies in <i>italic</i>"
 
 
141
  """
142
  prompt = PromptTemplate(
143
  template=template,
@@ -153,19 +158,16 @@ else:
153
  elif not _items:
154
  # Case 2: When there are NO schema classes
155
  template = """
156
- First text block of the report.
157
- The website homepage doesn't seem to feature any schema class.
 
158
 
159
- Second text block of the report.
160
- The website's homepage also references the following entities: {topics} that can be used to improve the SEO of the website.
161
-
162
- Third text block of the report.
163
- Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do.
164
-
165
- THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:
166
- "first": "First text block",
167
- "second": "Second text block with entities in <b>bold</b>",
168
- "third": "Third text block with technologies in <i>italic</i>"
169
  """
170
  prompt = PromptTemplate(
171
  template=template,
@@ -179,23 +181,19 @@ else:
179
  else:
180
  # Case 3: When there ARE issues
181
  template = """
182
- First text block of the report.
183
- Analyze the: {advice}, consider that the site features the following schema classes: {items}.
184
-
185
- Second text block of the report.
186
- Describe the following issues with the markup: {issues} and indicate how to fix them.
187
 
188
- Third text block of the report.
189
- The website's homepage also references the following entities: {topics} that could be used to improve the SEO of the website further.
190
-
191
- Fourth text block of the report.
192
- Describe, if available, IN A SINGLE SENTENCE the {technologies} that the site appears to be using and what they do.
193
-
194
- THE OUTPUT MUST USE THE FOLLOWING TEMPLATE:
195
- "first": "First text block with schema classes in <i>italic</i>",
196
- "second": "Second text block with issues in <u>underline</u>",
197
- "third": "Third text block with entities in <b>bold</b>",
198
- "fourth": "Fourth text block with technologies in <i>italic</i>"
199
  """
200
  prompt = PromptTemplate(
201
  template=template,
@@ -218,8 +216,7 @@ else:
218
  response = client.chat.completions.create(
219
  model="gpt-4o",
220
  messages=messages,
221
- temperature=0.7,
222
- max_tokens=1500
223
  )
224
  if hasattr(response.choices[0].message, 'content'):
225
  out = response.choices[0].message.content
@@ -240,6 +237,11 @@ else:
240
  error_message = f"An unexpected error occurred: {str(e)}"
241
  print(error_message) # Log the error for debugging
242
  return error_message
 
 
 
 
 
243
 
244
  # Call WooRank API to get the data (cached)
245
  @st.cache_data
 
102
  """
103
  try:
104
  # Create the system message for ChatGPT
105
+ prefix_messages = [{
106
  "role": "system",
107
+ "content": '''You are an SEO expert specializing in structured data analysis. Your task is to create JSON-formatted reports about websites' structured data.
108
+
109
+ Key requirements:
110
+ 1. Always format output as a valid JSON object
111
+ 2. Use the exact structure provided in the template
112
+ 3. Include HTML formatting (<i>, <b>, <u>) as specified
113
+ 4. Add relevant links to structured data (https://wordlift.io/blog/en/entity/structured-data/) and schema.org (https://wordlift.io/blog/en/entity/schema-org/) in the first section
114
+ 5. Keep responses concise but informative
115
+ 6. Ensure proper JSON escaping for quotes and special characters
116
+
117
+ Remember: The output must be a single, valid JSON object that can be parsed without additional processing.'''
118
  }]
119
 
120
  # Initialize OpenAI client with basic configuration
 
132
  if not _issues and len(_items) > 0:
133
  # Case 1: When there are NO issues but there ARE items
134
  template = """
135
+ Based on the following data, create a JSON object with three parts:
136
+ 1. {advice} and schema classes: {items}
137
+ 2. Entities found: {topics}
138
+ 3. Technologies: {technologies}
 
 
 
 
139
 
140
+ Structure your response as a valid JSON object with this exact format:
141
+ {{
142
+ "first": "Analysis of schema classes with classes marked in <i>italic</i>",
143
+ "second": "Description of entities marked in <b>bold</b>",
144
+ "third": "Description of technologies in <i>italic</i>"
145
+ }}
146
  """
147
  prompt = PromptTemplate(
148
  template=template,
 
158
  elif not _items:
159
  # Case 2: When there are NO schema classes
160
  template = """
161
+ Create a JSON object for a website with no schema classes, based on:
162
+ 1. Entities found: {topics}
163
+ 2. Technologies: {technologies}
164
 
165
+ Structure your response as a valid JSON object with this exact format:
166
+ {{
167
+ "first": "Notice about missing schema classes",
168
+ "second": "Description of entities marked in <b>bold</b>",
169
+ "third": "Description of technologies in <i>italic</i>"
170
+ }}
 
 
 
 
171
  """
172
  prompt = PromptTemplate(
173
  template=template,
 
181
  else:
182
  # Case 3: When there ARE issues
183
  template = """
184
+ Create a JSON object based on the following data:
185
+ 1. {advice} and schema classes: {items}
186
+ 2. Markup issues: {issues}
187
+ 3. Entities found: {topics}
188
+ 4. Technologies: {technologies}
189
 
190
+ Structure your response as a valid JSON object with this exact format:
191
+ {{
192
+ "first": "Analysis of schema classes with classes marked in <i>italic</i>",
193
+ "second": "Description of issues marked in <u>underline</u>",
194
+ "third": "Description of entities marked in <b>bold</b>",
195
+ "fourth": "Description of technologies in <i>italic</i>"
196
+ }}
 
 
 
 
197
  """
198
  prompt = PromptTemplate(
199
  template=template,
 
216
  response = client.chat.completions.create(
217
  model="gpt-4o",
218
  messages=messages,
219
+ temperature=0.2,
 
220
  )
221
  if hasattr(response.choices[0].message, 'content'):
222
  out = response.choices[0].message.content
 
237
  error_message = f"An unexpected error occurred: {str(e)}"
238
  print(error_message) # Log the error for debugging
239
  return error_message
240
+
241
+ except Exception as e:
242
+ error_message = f"An unexpected error occurred: {str(e)}"
243
+ print(error_message) # Log the error for debugging
244
+ return error_message
245
 
246
  # Call WooRank API to get the data (cached)
247
  @st.cache_data