Dede16 commited on
Commit
4aa2a66
·
1 Parent(s): b60f12e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -33
app.py CHANGED
@@ -70,7 +70,8 @@ base_replacements = {
70
  'Aplikasi pesan':'Message app'
71
  }
72
 
73
- def get_openai_response(messages):
 
74
  response = openai.ChatCompletion.create(
75
  model="gpt-3.5-turbo",
76
  messages=messages,
@@ -80,7 +81,11 @@ def get_openai_response(messages):
80
  if finish_reason == 'length' or finish_reason == 'stop':
81
  return response['choices'][0]['message']['content']
82
 
83
- def get_azure_response(messages):
 
 
 
 
84
  response = openai.ChatCompletion.create(
85
  engine="gpt-35-turbo",
86
  messages=messages,
@@ -90,7 +95,7 @@ def get_azure_response(messages):
90
  if finish_reason == 'length' or finish_reason == 'stop':
91
  return response['choices'][0]['message']['content']
92
 
93
- def clean_scrap(artikel,link, models):
94
  new_artikel = []
95
  article = []
96
  if len(artikel) > 1:
@@ -100,11 +105,11 @@ def clean_scrap(artikel,link, models):
100
  {"role": "user", "content": "I have a raw article that contains a lot of unnecessary data such as ads, website information, and article publishers, as well as links to other pages, and so on. Please clean up the article I provided so that only the article's content remains. \nThen, you should also summarize the article so that it does not exceed 5000 characters" + art + "\nDo not write any explanation and any pleasantries. Please use the following complete format to display the output: {the cleaned and summarized article's content}"}
101
  ]
102
  if models == 'openai':
103
- result = get_openai_response(messages)
104
  new_artikel.append(result)
105
  time.sleep(2)
106
  else:
107
- result = get_azure_response(messages)
108
  new_artikel.append(result)
109
  time.sleep(2)
110
  else:
@@ -114,11 +119,11 @@ def clean_scrap(artikel,link, models):
114
  {"role": "user", "content": "I have a raw article that contains a lot of unnecessary data such as ads, website information, and article publishers, as well as links to other pages, and so on. Please clean up the article I provided so that only the article's content remains." + art + "\nDo not write any explanation and any pleasantries. Please use the following complete format to display the output: {the cleaned article's content}"}
115
  ]
116
  if models == 'openai':
117
- result = get_openai_response(messages)
118
  new_artikel.append(result)
119
  time.sleep(2)
120
  else:
121
- result = get_azure_response(messages)
122
  new_artikel.append(result)
123
  time.sleep(2)
124
 
@@ -129,11 +134,11 @@ def clean_scrap(artikel,link, models):
129
  {"role": "user", "content": "Paraphrase the above article to make it a well-written and easily understandable piece for humans, following the conventions of renowned articles. \nThen, You Must Generate a title that is appropriate for the article I provided. The title should be professional, similar to typical article titles and sound more natural for a human to read" + art + "\nDo not write any explanation and any pleasantries. Please use the following complete format to display the output: title:{title}, article: {new paraphrased article}"}
130
  ]
131
  if models == 'openai':
132
- result = get_openai_response(messages)
133
  article.append(result)
134
  time.sleep(2)
135
  else:
136
- result = get_azure_response(messages)
137
  article.append(result)
138
  time.sleep(2)
139
 
@@ -145,9 +150,9 @@ def clean_scrap(artikel,link, models):
145
  ]
146
  judul = response['choices'][0]['message']['content']
147
  if models == 'openai':
148
- judul = get_openai_response(messages)
149
  else:
150
- judul = get_azure_response(messages)
151
  judul = judul.replace("Judul:", '').strip()
152
  judul = judul.replace("Title:", '').strip()
153
  try:
@@ -160,7 +165,7 @@ def clean_scrap(artikel,link, models):
160
 
161
  return title, judul, link, contents
162
 
163
- def scrap_artikel(link_scrap, models):
164
  options = webdriver.ChromeOptions()
165
  options.add_argument('--headless')
166
  options.add_argument('--no-sandbox')
@@ -192,11 +197,11 @@ def scrap_artikel(link_scrap, models):
192
  artikels = [part1, part2, part3, part4]
193
  else :
194
  artikels = [paragraf]
195
- title, judul, url, contents = clean_scrap(artikels,link_scrap, models)
196
  return title, judul, url, contents
197
 
198
- def artikel_processing(link_scrap,backlink,keyword,models):
199
- title, judul, url, artikel= scrap_artikel(link_scrap, models)
200
  teks_to_tags = artikel[0][:500]
201
  translated = []
202
  optimized = []
@@ -210,11 +215,11 @@ def artikel_processing(link_scrap,backlink,keyword,models):
210
  {"role": "user", "content": "Translate the following article into Indonesian language. Then, you must resume the article translated. The translated result should be more than 2500 characters and less than 7000 characters.: " + i + "\nDo not write any explanation and any pleasantries. Please use the following complete format to display the output: {Professionally rewritten content}"}
211
  ]
212
  if models == 'openai':
213
- translate = get_openai_response(messages)
214
  translated.append(translate)
215
  time.sleep(2)
216
  else:
217
- translate = get_azure_response(messages)
218
  translated.append(translate)
219
  time.sleep(2)
220
 
@@ -247,11 +252,11 @@ def artikel_processing(link_scrap,backlink,keyword,models):
247
  {"role": "user", "content": "Please ensure the usage of proper and correct Indonesian language. \nDo not write any explanation and any pleasantries. Provide only the rewrited article using this format: {rewrited article}"}
248
  ]
249
  if models == 'openai':
250
- result = get_openai_response(messages)
251
  article.append(result)
252
  time.sleep(2)
253
  else:
254
- result = get_azure_response(messages)
255
  article.append(result)
256
  time.sleep(2)
257
 
@@ -261,11 +266,11 @@ def artikel_processing(link_scrap,backlink,keyword,models):
261
  {"role": "user", "content": "Please rewrite the given article in the style of a professional writer for Forbes or The New York Times with bahasa indonesia as your native language:\n\n" + i + "\nAdd underline tags <u> and bold tags <b> to all foreign terms (non-Indonesian words) you encounter. You only have less than 7 attempts to do this, no more than that in order to keep the article neat and clean. \nThen, You must divide the article into several paragraphs, no less than 3 paragraphs. kamu juga harus membuat subheading menggunakan <h2> pada setiap sub topik pembahasan \n\nPlease ensure the usage of proper and correct Indonesian language. \nDo not write any explanation and any pleasantries. Provide only the reformatted article using this format:<h2>A brief headline of the article content</h2> <p>reformatted article</p>"}
262
  ]
263
  if models == 'openai':
264
- font_formatted = get_openai_response(messages)
265
  edited_format.append(font_formatted)
266
  time.sleep(2)
267
  else:
268
- font_formatted = get_azure_response(messages)
269
  edited_format.append(font_formatted)
270
  time.sleep(2)
271
 
@@ -275,11 +280,11 @@ def artikel_processing(link_scrap,backlink,keyword,models):
275
  {"role": "user", "content": "Please edit the given article:\n" + "\n" + i + f"\nAdd annotations to the words with the keywords {keyword} to format them as links in the HTML structure.the link should be connected to {backlink} \nThe format should be like this: <a title={keyword} href={backlink}>{keyword}</a>. YOU MUST Do this FORMAT ONLY for the first 3 keywords that appear and MUST be on different keywords, IF a keyword appears more than twice then simply ignored it by not adding any links to those keywords. Do not combine two keyword into one or modify any keyword. You only have less than 3 attempts to do this, no more than that in order to keep the article neat and clean. \nExcept for the terms {keyword} you are prohibited from providing backlinks. Additionally, you are not allowed to include backlinks to individuals' names or technology company names such as Google, Microsoft, and others. \nYou only have less than 3 attempts to do this, no more than that in order to keep the article neat and clean.\nPlease ensure the usage of proper and correct Indonesian language. \nDo not write any explanation and any pleasantries."+"Provide only the reformatted article using this format: {new_formatted_article}"}
276
  ]
277
  if models == 'openai':
278
- artikel_post = get_openai_response(messages)
279
  post_article.append(artikel_post )
280
  time.sleep(2)
281
  else:
282
- artikel_post = get_azure_response(messages)
283
  post_article.append(artikel_post )
284
  time.sleep(2)
285
 
@@ -328,9 +333,9 @@ def artikel_processing(link_scrap,backlink,keyword,models):
328
  ]
329
 
330
  if models == 'openai':
331
- image_prompt = get_openai_response(messages)
332
  else:
333
- image_prompt = get_azure_response(messages)
334
 
335
  return image_prompt
336
 
@@ -426,14 +431,7 @@ def artikel_processing(link_scrap,backlink,keyword,models):
426
 
427
  def scrap(link_scrap,backlink,keyword,version,api_key,azure_api_base,replicate_key):
428
  # try:
429
-
430
- replicate_key = replicate_key
431
- openai.api_type = "azure"
432
- openai.api_version = "2023-05-15"
433
- openai.api_base = azure_api_base
434
- openai.api_key = api_key
435
-
436
- judul,kontent,gambar, image_data= artikel_processing(link_scrap,backlink,keyword,version)
437
  desired_timezone = pytz.timezone('Asia/Jakarta')
438
  current_time = datetime.datetime.now(desired_timezone)
439
  Timestamp = current_time.strftime('%Y-%m-%d %H:%M:%S')
 
70
  'Aplikasi pesan':'Message app'
71
  }
72
 
73
+ def get_openai_response(messages, api_key):
74
+ openai.api_key = api_key
75
  response = openai.ChatCompletion.create(
76
  model="gpt-3.5-turbo",
77
  messages=messages,
 
81
  if finish_reason == 'length' or finish_reason == 'stop':
82
  return response['choices'][0]['message']['content']
83
 
84
+ def get_azure_response(messages, api_key, azure_api_base):
85
+ openai.api_type = "azure"
86
+ openai.api_version = "2023-05-15"
87
+ openai.api_base = azure_api_base
88
+ openai.api_key = api_key
89
  response = openai.ChatCompletion.create(
90
  engine="gpt-35-turbo",
91
  messages=messages,
 
95
  if finish_reason == 'length' or finish_reason == 'stop':
96
  return response['choices'][0]['message']['content']
97
 
98
+ def clean_scrap(artikel,link,models,api_key,azure_api_base):
99
  new_artikel = []
100
  article = []
101
  if len(artikel) > 1:
 
105
  {"role": "user", "content": "I have a raw article that contains a lot of unnecessary data such as ads, website information, and article publishers, as well as links to other pages, and so on. Please clean up the article I provided so that only the article's content remains. \nThen, you should also summarize the article so that it does not exceed 5000 characters" + art + "\nDo not write any explanation and any pleasantries. Please use the following complete format to display the output: {the cleaned and summarized article's content}"}
106
  ]
107
  if models == 'openai':
108
+ result = get_openai_response(messages,api_key)
109
  new_artikel.append(result)
110
  time.sleep(2)
111
  else:
112
+ result = get_azure_response(messages,api_key,azure_api_base)
113
  new_artikel.append(result)
114
  time.sleep(2)
115
  else:
 
119
  {"role": "user", "content": "I have a raw article that contains a lot of unnecessary data such as ads, website information, and article publishers, as well as links to other pages, and so on. Please clean up the article I provided so that only the article's content remains." + art + "\nDo not write any explanation and any pleasantries. Please use the following complete format to display the output: {the cleaned article's content}"}
120
  ]
121
  if models == 'openai':
122
+ result = get_openai_response(messages,api_key)
123
  new_artikel.append(result)
124
  time.sleep(2)
125
  else:
126
+ result = get_azure_response(messages,api_key,azure_api_base)
127
  new_artikel.append(result)
128
  time.sleep(2)
129
 
 
134
  {"role": "user", "content": "Paraphrase the above article to make it a well-written and easily understandable piece for humans, following the conventions of renowned articles. \nThen, You Must Generate a title that is appropriate for the article I provided. The title should be professional, similar to typical article titles and sound more natural for a human to read" + art + "\nDo not write any explanation and any pleasantries. Please use the following complete format to display the output: title:{title}, article: {new paraphrased article}"}
135
  ]
136
  if models == 'openai':
137
+ result = get_openai_response(messages,api_key)
138
  article.append(result)
139
  time.sleep(2)
140
  else:
141
+ result = get_azure_response(messages,api_key,azure_api_base)
142
  article.append(result)
143
  time.sleep(2)
144
 
 
150
  ]
151
  judul = response['choices'][0]['message']['content']
152
  if models == 'openai':
153
+ judul = get_openai_response(messages,api_key)
154
  else:
155
+ judul = get_azure_response(messages,api_key,azure_api_base)
156
  judul = judul.replace("Judul:", '').strip()
157
  judul = judul.replace("Title:", '').strip()
158
  try:
 
165
 
166
  return title, judul, link, contents
167
 
168
+ def scrap_artikel(link_scrap,models,api_key,azure_api_base):
169
  options = webdriver.ChromeOptions()
170
  options.add_argument('--headless')
171
  options.add_argument('--no-sandbox')
 
197
  artikels = [part1, part2, part3, part4]
198
  else :
199
  artikels = [paragraf]
200
+ title, judul, url, contents = clean_scrap(artikels,link_scrap,models,api_key,azure_api_base)
201
  return title, judul, url, contents
202
 
203
+ def artikel_processing(link_scrap,backlink,keyword,models,api_key,azure_api_base,replicate_key):
204
+ title, judul, url, artikel= scrap_artikel(link_scrap, models, api_key,azure_api_base)
205
  teks_to_tags = artikel[0][:500]
206
  translated = []
207
  optimized = []
 
215
  {"role": "user", "content": "Translate the following article into Indonesian language. Then, you must resume the article translated. The translated result should be more than 2500 characters and less than 7000 characters.: " + i + "\nDo not write any explanation and any pleasantries. Please use the following complete format to display the output: {Professionally rewritten content}"}
216
  ]
217
  if models == 'openai':
218
+ translate = get_openai_response(messages,api_key)
219
  translated.append(translate)
220
  time.sleep(2)
221
  else:
222
+ translate = get_azure_response(messages,api_key,azure_api_base)
223
  translated.append(translate)
224
  time.sleep(2)
225
 
 
252
  {"role": "user", "content": "Please ensure the usage of proper and correct Indonesian language. \nDo not write any explanation and any pleasantries. Provide only the rewrited article using this format: {rewrited article}"}
253
  ]
254
  if models == 'openai':
255
+ result = get_openai_response(messages,api_key)
256
  article.append(result)
257
  time.sleep(2)
258
  else:
259
+ result = get_azure_response(messages,api_key,azure_api_base)
260
  article.append(result)
261
  time.sleep(2)
262
 
 
266
  {"role": "user", "content": "Please rewrite the given article in the style of a professional writer for Forbes or The New York Times with bahasa indonesia as your native language:\n\n" + i + "\nAdd underline tags <u> and bold tags <b> to all foreign terms (non-Indonesian words) you encounter. You only have less than 7 attempts to do this, no more than that in order to keep the article neat and clean. \nThen, You must divide the article into several paragraphs, no less than 3 paragraphs. kamu juga harus membuat subheading menggunakan <h2> pada setiap sub topik pembahasan \n\nPlease ensure the usage of proper and correct Indonesian language. \nDo not write any explanation and any pleasantries. Provide only the reformatted article using this format:<h2>A brief headline of the article content</h2> <p>reformatted article</p>"}
267
  ]
268
  if models == 'openai':
269
+ font_formatted = get_openai_response(messages,api_key)
270
  edited_format.append(font_formatted)
271
  time.sleep(2)
272
  else:
273
+ font_formatted = get_azure_response(messages,api_key,azure_api_base)
274
  edited_format.append(font_formatted)
275
  time.sleep(2)
276
 
 
280
  {"role": "user", "content": "Please edit the given article:\n" + "\n" + i + f"\nAdd annotations to the words with the keywords {keyword} to format them as links in the HTML structure.the link should be connected to {backlink} \nThe format should be like this: <a title={keyword} href={backlink}>{keyword}</a>. YOU MUST Do this FORMAT ONLY for the first 3 keywords that appear and MUST be on different keywords, IF a keyword appears more than twice then simply ignored it by not adding any links to those keywords. Do not combine two keyword into one or modify any keyword. You only have less than 3 attempts to do this, no more than that in order to keep the article neat and clean. \nExcept for the terms {keyword} you are prohibited from providing backlinks. Additionally, you are not allowed to include backlinks to individuals' names or technology company names such as Google, Microsoft, and others. \nYou only have less than 3 attempts to do this, no more than that in order to keep the article neat and clean.\nPlease ensure the usage of proper and correct Indonesian language. \nDo not write any explanation and any pleasantries."+"Provide only the reformatted article using this format: {new_formatted_article}"}
281
  ]
282
  if models == 'openai':
283
+ artikel_post = get_openai_response(messages,api_key)
284
  post_article.append(artikel_post )
285
  time.sleep(2)
286
  else:
287
+ artikel_post = get_azure_response(messages,api_key,azure_api_base)
288
  post_article.append(artikel_post )
289
  time.sleep(2)
290
 
 
333
  ]
334
 
335
  if models == 'openai':
336
+ image_prompt = get_openai_response(messages,api_key)
337
  else:
338
+ image_prompt = get_azure_response(messages,api_key,azure_api_base)
339
 
340
  return image_prompt
341
 
 
431
 
432
  def scrap(link_scrap,backlink,keyword,version,api_key,azure_api_base,replicate_key):
433
  # try:
434
+ judul,kontent,gambar, image_data= artikel_processing(link_scrap,backlink,keyword,version,api_key,azure_api_base,replicate_key)
 
 
 
 
 
 
 
435
  desired_timezone = pytz.timezone('Asia/Jakarta')
436
  current_time = datetime.datetime.now(desired_timezone)
437
  Timestamp = current_time.strftime('%Y-%m-%d %H:%M:%S')