[Python] How do I load huggingface model locally to use with langchain and host and streamlit?

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to make an AI app with langchain and Huggingface. I got the following error:


    { "error": "Could not load model paragon-AI/blip2-image-to-text with any of the following classes: (<class 'transformers.models.blip_2.modeling_blip_2.Blip2ForConditionalGeneration'>,). See the original errors:\n\nwhile loading with Blip2ForConditionalGeneration, an error is thrown:\nTraceback (most recent call last):\n File "/src/transformers/src/transformers/pipelines/base.py", line 269, in infer_framework_load_model\n model = model_class.from_pretrained(model, **kwargs)\n File "/src/transformers/src/transformers/modeling_utils.py", line 3138, in from_pretrained\n raise EnvironmentError(\nOSError: paragon-AI/blip2-image-to-text does not appear to have a file named pytorch_model.bin, tf_model.h5, model.ckpt or flax_model.msgpack.\n\n\n" }
    ​

    import streamlit as st
    import requests
    from PIL import Image
    from io import BytesIO
    from transformers import AutoModelForCausalLM

    def main():
    st.title("Image to Text and Audio Converter")

    # Upload image file
    uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

    if uploaded_file is not None:
    # Display uploaded image
    st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)

    # Convert image to text
    st.subheader("Image to Text")
    text_output = convert_image_to_text(uploaded_file)
    st.write(text_output)

    # Convert image to audio
    st.subheader("Image to Audio")
    audio_output = convert_image_to_audio(uploaded_file)
    st.audio(audio_output)

    def convert_image_to_text(image_file):
    IMAGE_TO_TEXT_API_URL = "https://api-inference.huggingface.co/models/paragon-AI/blip2-image-to-text"
    IMAGE_TO_TEXT_HEADERS = {"Authorization": "Bearer apikey"}

    response = requests.post(IMAGE_TO_TEXT_API_URL, headers=IMAGE_TO_TEXT_HEADERS, files={"file": image_file})
    return response.json()

    def convert_image_to_audio(image_file):
    IMAGE_TO_AUDIO_API_URL = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
    IMAGE_TO_AUDIO_HEADERS = {"Authorization": "Bearer apikey"}

    response = requests.post(IMAGE_TO_AUDIO_API_URL, headers=IMAGE_TO_AUDIO_HEADERS, files={"file": image_file})
    return response.content

    if __name__ == "__main__":
    main()

    Continue reading...

Compartilhe esta Página