MCPcopy Create free account
hub / github.com/codingis4noobs2/QuickDigest / transform_products

Function transform_products

app.py:448–515  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

446
447# Function to display transform products page
448def transform_products():
449 import streamlit as st
450 import requests
451 import os
452 import replicate
453 import io
454 from PIL import Image
455
456
457 st.session_state['replicate_api_token'] = st.sidebar.text_input("Replicate API Token", type='password')
458 os.environ['REPLICATE_API_TOKEN'] = st.session_state['replicate_api_token']
459
460 if not st.session_state['replicate_api_token']:
461 st.sidebar.warning('Please enter your Replicate API Token to continue!!')
462 st.sidebar.info("You can get your Replicate API Token form here: [Replicate](https://replicate.com/account/api-tokens)")
463 st.stop()
464
465 if st.session_state['replicate_api_token']:
466 st.info("This model works best with product images having transparent or plain backgrounds")
467 # Prompt user to upload an image file
468 img = st.file_uploader("Upload your product image", type=['png', 'jpg', 'jpeg'])
469
470 if img is not None:
471 has_plain_background = st.toggle("Does your product image have a plain or transparent background? If not, let us do the hard work for you!")
472 prompt = st.text_input("Enter Prompt", help="Enter something you imagine...")
473 negative_prompt = st.text_input("Enter Negative Prompt", help="Write what you don't want in the generated images")
474 submit = st.button("Submit")
475
476 if submit:
477 if has_plain_background:
478 # If image already has a plain background, prepare it for Replicate
479 image = Image.open(img)
480 bytes_obj = io.BytesIO()
481 image.save(bytes_obj, format='PNG')
482 bytes_obj.seek(0)
483 else:
484 # If image does not have a plain background, send it to ClipDrop to remove background
485 image_file_object = img.read()
486 r = requests.post('https://clipdrop-api.co/remove-background/v1',
487 files={
488 'image_file': ('uploaded_image.jpg', image_file_object, 'image/jpeg')
489 },
490 headers={'x-api-key': st.secrets['clipdrop_api_key']}
491 )
492
493 if r.ok:
494 # If background removal is successful, prepare image for Replicate
495 image = Image.open(io.BytesIO(r.content))
496 bytes_obj = io.BytesIO()
497 image.save(bytes_obj, format='PNG')
498 bytes_obj.seek(0)
499 else:
500 r.raise_for_status()
501 st.error('Failed to remove background. Try again.')
502 st.stop()
503
504 # Send image to Replicate for transformation
505 output = replicate.run(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected