Dede16 commited on
Commit
b0348ae
·
1 Parent(s): 7a40723

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -29
app.py CHANGED
@@ -405,7 +405,6 @@ def get_image(prompt):
405
  "width": 1648,
406
  "height": 1024}
407
  )
408
- prediction
409
  prediction.reload()
410
  prediction.wait()
411
 
@@ -413,21 +412,32 @@ def get_image(prompt):
413
  print(f"Error: {prediction.error}")
414
  print(f"Last Log")
415
  print(prediction.logs)
416
- elif (prediction.status == 'succeeded' or prediction.output is not None):
 
417
  response = requests.get(prediction.output[0])
418
- image_base64 = base64.b64encode(response.content)
419
- image = Image.open(io.BytesIO(base64.b64decode(image_base64)))
420
- image = image.crop((3, 0, 1645, 1024))
421
- w, h = image.size
422
- new_w = int(w / 1.641)
423
- new_h = int(h / 1.641)
424
- image = image.resize((new_w, new_h), Image.ANTIALIAS)
425
- image_data = base64.b64encode(image.tobytes()).decode('utf-8')
426
- return image_data, image
427
  except Exception as e:
428
  print(f"An error occurred: {e}")
429
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
430
 
 
 
431
 
432
  # return tags, content, url #image, image_data
433
 
@@ -437,21 +447,21 @@ def get_image(prompt):
437
  # return judul, content
438
 
439
 
440
- with gr.Blocks(theme = "soft", title="Article Writer") as article_generator:
441
- gr.Markdown(
442
- """
443
- # Article Writer
444
- Buat artikel yang anda inginkan dengan mudah, cukup menuliskan keyword atau topik artikel yang ingin anda buat dan dalam sekejap artikel tersebut menjadi milik anda.
445
- """)
446
- with gr.Row():
447
- inputs = gr.Textbox(placeholder="Link Article", show_label=False)
448
- with gr.Row():
449
- button_scrap = gr.Button("Scrap Article")
450
- with gr.Row():
451
- # title = gr.Textbox("", label="Title")
452
- # content = gr.Textbox("", label="Content")
453
- img_data = gr.Textbox("", label="Content")
454
- img = gr.Image("", label="Image")
455
- button_scrap.click(fn=get_image, inputs=inputs, outputs=[img, img_data])
456
- if __name__ == "__main__":
457
- article_generator.launch(share=False)
 
405
  "width": 1648,
406
  "height": 1024}
407
  )
 
408
  prediction.reload()
409
  prediction.wait()
410
 
 
412
  print(f"Error: {prediction.error}")
413
  print(f"Last Log")
414
  print(prediction.logs)
415
+ return None
416
+ elif (prediction.status == 'succeeded' and prediction.output is not None):
417
  response = requests.get(prediction.output[0])
418
+ image_base64 = base64.b64encode(response.content).decode('utf-8')
419
+ return image_base64
 
 
 
 
 
 
 
420
  except Exception as e:
421
  print(f"An error occurred: {e}")
422
  return None
423
+
424
+ def show_image(image_data, width=800, height=600):
425
+ # Konversi data gambar base64 menjadi gambar PIL
426
+ image_bytes = base64.b64decode(image_data)
427
+ image = Image.open(io.BytesIO(image_bytes))
428
+
429
+ # Resize gambar sesuai dengan lebar dan tinggi yang diinginkan
430
+ image = image.resize((width, height), Image.ANTIALIAS)
431
+
432
+ # Konversi kembali gambar ke format base64
433
+ buffered = io.BytesIO()
434
+ image.save(buffered, format="JPEG")
435
+ image_data_resized = base64.b64encode(buffered.getvalue()).decode('utf-8')
436
+
437
+ return gr.outputs.Image(image_data_resized)
438
 
439
+ iface = gr.Interface(fn=get_image, inputs="text", outputs=show_image)
440
+ iface.launch()
441
 
442
  # return tags, content, url #image, image_data
443
 
 
447
  # return judul, content
448
 
449
 
450
+ # with gr.Blocks(theme = "soft", title="Article Writer") as article_generator:
451
+ # gr.Markdown(
452
+ # """
453
+ # # Article Writer
454
+ # Buat artikel yang anda inginkan dengan mudah, cukup menuliskan keyword atau topik artikel yang ingin anda buat dan dalam sekejap artikel tersebut menjadi milik anda.
455
+ # """)
456
+ # with gr.Row():
457
+ # inputs = gr.Textbox(placeholder="Link Article", show_label=False)
458
+ # with gr.Row():
459
+ # button_scrap = gr.Button("Scrap Article")
460
+ # with gr.Row():
461
+ # # title = gr.Textbox("", label="Title")
462
+ # # content = gr.Textbox("", label="Content")
463
+ # img_data = gr.Textbox("", label="Content")
464
+ # img = gr.Image("", label="Image")
465
+ # button_scrap.click(fn=get_image, inputs=inputs, outputs=[img, img_data])
466
+ # if __name__ == "__main__":
467
+ # article_generator.launch(share=False)