import streamlit as st
from io import StringIO
from PIL import Image
import numpy as np


from transformers import pipeline

from helper.image_helper import to_base64




pipe = pipeline("image-to-text",
                model="Salesforce/blip-image-captioning-base")

img= None

def process_file():
    stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
    txt = launch(stringio) 
    st.write(txt)

def launch(input):
    out = pipe(input)
    return out[0]['generated_text']


# uploaded_file = st.file_uploader("Choose a file", on_change=process_file)

uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:
  
    img = Image.open(uploaded_file) 
    st.image(img) 

    # bytes_data = uploaded_file.getvalue()
    # base64 = to_base64(uploaded_file)
    # st.image(base64)

    txt = launch(img) 
    st.write(txt)

# iface = gr.Interface(launch,
#                      inputs=gr.Image(type='pil'),
#                      outputs="text")

# iface.launch()