Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,55 +1,54 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
from PIL import Image
|
4 |
import torch
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
def initialize_pipeline():
|
15 |
-
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
16 |
-
model_pipeline = pipeline(
|
17 |
-
"image-to-text",
|
18 |
-
model=model_id,
|
19 |
-
feature_extractor=feature_extractor,
|
20 |
-
model_kwargs={"torch_dtype": torch.float32, "use_auth_token": HF_TOKEN},
|
21 |
)
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
def
|
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 |
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
|
|
3 |
import torch
|
4 |
+
from PIL import Image
|
5 |
+
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
6 |
+
|
7 |
+
def load_model_and_processor(model_id):
|
8 |
+
"""Load the model and processor."""
|
9 |
+
model = MllamaForConditionalGeneration.from_pretrained(
|
10 |
+
model_id,
|
11 |
+
torch_dtype=torch.bfloat16,
|
12 |
+
device_map="auto",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
)
|
14 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
15 |
+
return model, processor
|
16 |
+
|
17 |
+
def generate_text(model, processor, image_url, prompt):
|
18 |
+
"""Generate text using the model and processor."""
|
19 |
+
try:
|
20 |
+
image = Image.open(requests.get(image_url, stream=True).raw)
|
21 |
+
inputs = processor(image, prompt, return_tensors="pt").to(model.device)
|
22 |
+
output = model.generate(**inputs, max_new_tokens=30)
|
23 |
+
return processor.decode(output[0])
|
24 |
+
except Exception as e:
|
25 |
+
return f"Error: {e}"
|
26 |
+
|
27 |
+
# Streamlit App
|
28 |
+
st.title("LLaMA 3 Vision Haiku Generator")
|
29 |
+
|
30 |
+
# Model ID and loading
|
31 |
+
MODEL_ID = "meta-llama/Llama-3.2-11B-Vision"
|
32 |
+
model, processor = load_model_and_processor(MODEL_ID)
|
33 |
+
|
34 |
+
# User input for image URL and prompt
|
35 |
+
image_url = st.text_input("Enter the Image URL:", "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg")
|
36 |
+
|
37 |
+
prompt = st.text_area("Enter your prompt:", "<|image|><|begin_of_text|>If I had to write a haiku for this one")
|
38 |
+
|
39 |
+
if st.button("Generate Haiku"):
|
40 |
+
with st.spinner("Generating haiku..."):
|
41 |
+
result = generate_text(model, processor, image_url, prompt)
|
42 |
+
|
43 |
+
st.subheader("Generated Text")
|
44 |
+
st.write(result)
|
45 |
+
|
46 |
+
try:
|
47 |
+
st.image(image_url, caption="Input Image")
|
48 |
+
except Exception:
|
49 |
+
st.error("Failed to load image. Please check the URL.")
|
50 |
+
|
51 |
+
|
52 |
|
53 |
|
54 |
|