Dede16 commited on
Commit
dc6a465
·
1 Parent(s): 263b0ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -9
app.py CHANGED
@@ -431,9 +431,6 @@ def artikel_processing(link_scrap,backlink,keyword,openai_key,replicate_key):
431
  result = pd.DataFrame(data)
432
  return result,image
433
 
434
- image = []
435
- title = []
436
- article = []
437
  def scrap(link_scrap,backlink,keyword,openai_key,replicate_key):
438
  # try:
439
  data,gambar= artikel_processing(link_scrap,backlink,keyword,openai_key,replicate_key)
@@ -470,16 +467,26 @@ def scrap(link_scrap,backlink,keyword,openai_key,replicate_key):
470
  # kontent = 'Gagal Generate Artikel:Request Time Out'
471
  # return gambar,judul,kontent
472
 
473
- def post(endpoint,endpoint_media,username,password,tags,categories,metode):
474
  credentials = base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("utf-8")
475
  headers = {"Authorization": f"Basic {credentials}"}
476
 
 
 
 
 
 
 
 
 
 
 
477
  data = {
478
- "alt_text": title[0],
479
  "media_type": "image",
480
  "mime_type": "png"
481
  }
482
- files = {"file": ("image.jpg", image[0])}
483
  try :
484
  response_media= requests.post(endpoint_media, headers=headers, data=data, files=files) # Send
485
 
@@ -490,8 +497,8 @@ def post(endpoint,endpoint_media,username,password,tags,categories,metode):
490
  media = 0
491
 
492
  data = {
493
- "title": title,
494
- "content":article,
495
  "featured_media" : media,
496
  "status": metode,
497
  "categories": categories,
@@ -529,6 +536,6 @@ with gr.Blocks(theme = "soft", title="Wordpress Article Generator") as article_g
529
  metode= gr.Radio(["publish", "draft"], label="Status Post", info="Pilih Metode Publish atau Draft Untuk Memposting")
530
  button_post = gr.Button("Post Article")
531
  status = gr.Textbox("", label="Response")
532
- button_post.click(fn=post, inputs=[endpoint,endpoint_media,username,password,tags,categories,metode], outputs=status)
533
  if __name__ == "__main__":
534
  article_generator.launch(share=False)
 
431
  result = pd.DataFrame(data)
432
  return result,image
433
 
 
 
 
434
  def scrap(link_scrap,backlink,keyword,openai_key,replicate_key):
435
  # try:
436
  data,gambar= artikel_processing(link_scrap,backlink,keyword,openai_key,replicate_key)
 
467
  # kontent = 'Gagal Generate Artikel:Request Time Out'
468
  # return gambar,judul,kontent
469
 
470
+ def post(judul, kontent, gambar, endpoint,endpoint_media,username,password,tags,categories,metode):
471
  credentials = base64.b64encode(f"{username}:{password}".encode("utf-8")).decode("utf-8")
472
  headers = {"Authorization": f"Basic {credentials}"}
473
 
474
+ tmp_path = "image.png"
475
+ gambar.save(tmp_path)
476
+ with open(tmp_path, 'rb') as open_file:
477
+ byte_img = open_file.read()
478
+ base64_bytes = base64.b64encode(byte_img)
479
+ base64_string = base64_bytes.decode('utf-8')
480
+ base64_string = base64.b64decode(base64_string)
481
+ image_data= base64_string
482
+ os.remove(tmp_path)
483
+
484
  data = {
485
+ "alt_text": judul,
486
  "media_type": "image",
487
  "mime_type": "png"
488
  }
489
+ files = {"file": ("image.jpg", image_data)}
490
  try :
491
  response_media= requests.post(endpoint_media, headers=headers, data=data, files=files) # Send
492
 
 
497
  media = 0
498
 
499
  data = {
500
+ "title": judul,
501
+ "content":kontent,
502
  "featured_media" : media,
503
  "status": metode,
504
  "categories": categories,
 
536
  metode= gr.Radio(["publish", "draft"], label="Status Post", info="Pilih Metode Publish atau Draft Untuk Memposting")
537
  button_post = gr.Button("Post Article")
538
  status = gr.Textbox("", label="Response")
539
+ button_post.click(fn=post, inputs=[outputs, content, img, endpoint,endpoint_media,username,password,tags,categories,metode], outputs=status)
540
  if __name__ == "__main__":
541
  article_generator.launch(share=False)