Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,11 @@ import torch
|
|
4 |
from PIL import Image
|
5 |
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
6 |
from huggingface_hub import login
|
7 |
-
|
|
|
|
|
8 |
login(HF_TOKEN)
|
|
|
9 |
def load_model_and_processor(model_id):
|
10 |
"""Load the model and processor."""
|
11 |
model = MllamaForConditionalGeneration.from_pretrained(
|
@@ -17,7 +20,7 @@ def load_model_and_processor(model_id):
|
|
17 |
return model, processor
|
18 |
|
19 |
def generate_text(model, processor, image_url, prompt):
|
20 |
-
|
21 |
try:
|
22 |
image = Image.open(requests.get(image_url, stream=True).raw)
|
23 |
inputs = processor(image, prompt, return_tensors="pt").to(model.device)
|
@@ -32,12 +35,18 @@ st.title("LLaMA 3 Vision Haiku Generator")
|
|
32 |
# Model ID and loading
|
33 |
MODEL_ID = "meta-llama/Llama-3.2-11B-Vision"
|
34 |
model, processor = load_model_and_processor(MODEL_ID)
|
35 |
-
print(model)
|
36 |
-
# User input for image URL and prompt
|
37 |
-
image_url = st.text_input("Enter the Image URL:", "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg")
|
38 |
-
|
39 |
-
prompt = st.text_area("Enter your prompt:", "<|image|><|begin_of_text|>If I had to write a haiku for this one")
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
if st.button("Generate Haiku"):
|
42 |
with st.spinner("Generating haiku..."):
|
43 |
result = generate_text(model, processor, image_url, prompt)
|
@@ -53,6 +62,8 @@ if st.button("Generate Haiku"):
|
|
53 |
|
54 |
|
55 |
|
|
|
|
|
56 |
|
57 |
|
58 |
|
|
|
4 |
from PIL import Image
|
5 |
from transformers import MllamaForConditionalGeneration, AutoProcessor
|
6 |
from huggingface_hub import login
|
7 |
+
|
8 |
+
# Authenticate with Hugging Face
|
9 |
+
HF_TOKEN = st.secrets["newfinegrained"]
|
10 |
login(HF_TOKEN)
|
11 |
+
|
12 |
def load_model_and_processor(model_id):
|
13 |
"""Load the model and processor."""
|
14 |
model = MllamaForConditionalGeneration.from_pretrained(
|
|
|
20 |
return model, processor
|
21 |
|
22 |
def generate_text(model, processor, image_url, prompt):
|
23 |
+
"""Generate text using the model and processor."""
|
24 |
try:
|
25 |
image = Image.open(requests.get(image_url, stream=True).raw)
|
26 |
inputs = processor(image, prompt, return_tensors="pt").to(model.device)
|
|
|
35 |
# Model ID and loading
|
36 |
MODEL_ID = "meta-llama/Llama-3.2-11B-Vision"
|
37 |
model, processor = load_model_and_processor(MODEL_ID)
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# User input for image URL and prompt
|
40 |
+
image_url = st.text_input(
|
41 |
+
"Enter the Image URL:",
|
42 |
+
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg"
|
43 |
+
)
|
44 |
+
prompt = st.text_area(
|
45 |
+
"Enter your prompt:",
|
46 |
+
"<|image|><|begin_of_text|>If I had to write a haiku for this one"
|
47 |
+
)
|
48 |
+
|
49 |
+
# Button to generate haiku
|
50 |
if st.button("Generate Haiku"):
|
51 |
with st.spinner("Generating haiku..."):
|
52 |
result = generate_text(model, processor, image_url, prompt)
|
|
|
62 |
|
63 |
|
64 |
|
65 |
+
|
66 |
+
|
67 |
|
68 |
|
69 |
|