Spaces:
Sleeping
Sleeping
File size: 1,915 Bytes
fa07468 2a17089 d17f3c6 2a17089 ba9839b d061a4c dd88080 2a17089 ba9839b 07391f2 2a17089 fb3dfe7 2a17089 fb3dfe7 2a17089 fb3dfe7 2a17089 fb3dfe7 2a17089 fb3dfe7 2a17089 fb3dfe7 2a17089 fb3dfe7 2a17089 051047a 49786bf 07391f2 038d603 722c6e5 fa07468 cb4d59e fa07468 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import streamlit as st
import requests
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor
from huggingface_hub import login
login()
HF_TOKEN=st.secrets["newfinegrained"]
def load_model_and_processor(model_id):
"""Load the model and processor."""
model = MllamaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto"
)
processor = AutoProcessor.from_pretrained(model_id)
return model, processor
# def generate_text(model, processor, image_url, prompt):
# """Generate text using the model and processor."""
# try:
# image = Image.open(requests.get(image_url, stream=True).raw)
# inputs = processor(image, prompt, return_tensors="pt").to(model.device)
# output = model.generate(**inputs, max_new_tokens=30)
# return processor.decode(output[0])
# except Exception as e:
# return f"Error: {e}"
# Streamlit App
st.title("LLaMA 3 Vision Haiku Generator")
# Model ID and loading
MODEL_ID = "meta-llama/Llama-3.2-11B-Vision"
model, processor = load_model_and_processor(MODEL_ID)
print(model)
# User input for image URL and prompt
# image_url = st.text_input("Enter the Image URL:", "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg")
# prompt = st.text_area("Enter your prompt:", "<|image|><|begin_of_text|>If I had to write a haiku for this one")
# if st.button("Generate Haiku"):
# with st.spinner("Generating haiku..."):
# result = generate_text(model, processor, image_url, prompt)
# st.subheader("Generated Text")
# st.write(result)
# try:
# st.image(image_url, caption="Input Image")
# except Exception:
# st.error("Failed to load image. Please check the URL.")
|