|
import streamlit as st |
|
from huggingface_hub import InferenceClient |
|
from config import HUGGINGFACE_API_KEY |
|
from PIL import Image |
|
import requests |
|
from io import BytesIO |
|
|
|
|
|
st.set_page_config(page_title="Llama-3.2 Demo App", page_icon="π€", layout="wide") |
|
st.title("πΌοΈ Llama-3.2-90B-Vision-Instruct Demo App") |
|
st.markdown("<p style='text-align: center; font-size: 18px; color: #555;'>Enter an image URL and get a description</p>", unsafe_allow_html=True) |
|
|
|
|
|
image_url = st.text_input("Enter Image URL", value="", placeholder="Paste image URL here...", max_chars=400) |
|
user_prompt = st.text_input("Enter your prompt", value="Describe this image in a paragraph", placeholder="e.g., What is shown in the image?") |
|
|
|
|
|
def show_image_from_url(image_url, max_height=200): |
|
try: |
|
response = requests.get(image_url) |
|
img = Image.open(BytesIO(response.content)) |
|
|
|
|
|
img_width, img_height = img.size |
|
|
|
|
|
if img_height > max_height: |
|
aspect_ratio = img_width / img_height |
|
new_height = max_height |
|
new_width = int(new_height * aspect_ratio) |
|
img_resized = img.resize((new_width, new_height)) |
|
else: |
|
img_resized = img |
|
|
|
|
|
st.image(img_resized, caption=f"Source: {image_url}", use_container_width=True) |
|
|
|
except Exception as e: |
|
st.error(f"β Unable to load image. Error: {e}") |
|
|
|
|
|
if st.button("Get Description", key="get_description"): |
|
if image_url and user_prompt: |
|
try: |
|
|
|
show_image_from_url(image_url, max_height=600) |
|
|
|
|
|
client = InferenceClient(api_key=HUGGINGFACE_API_KEY) |
|
|
|
|
|
messages = [ |
|
{ |
|
"role": "user", |
|
"content": [ |
|
{"type": "text", "text": user_prompt}, |
|
{"type": "image_url", "image_url": {"url": image_url}} |
|
] |
|
} |
|
] |
|
|
|
|
|
completion = client.chat.completions.create( |
|
model="meta-llama/Llama-3.2-11B-Vision-Instruct", |
|
messages=messages, |
|
max_tokens=500 |
|
) |
|
|
|
|
|
model_response = completion.choices[0].message |
|
|
|
|
|
st.subheader("π Model Response") |
|
|
|
|
|
st.markdown(f"**Description**: {model_response.get('content', 'No description available')}") |
|
|
|
except Exception as e: |
|
st.error(f"β An error occurred: {e}") |
|
else: |
|
st.warning("β οΈ Please enter an image URL and a prompt.") |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.stButton>button { |
|
background-color: #0072BB; |
|
color: white; |
|
font-size: 16px; |
|
border-radius: 10px; |
|
padding: 10px 20px; |
|
font-weight: bold; |
|
transition: background-color 0.3s; |
|
} |
|
.stButton>button:hover { |
|
background-color: #005f8a; |
|
} |
|
|
|
.stTextInput>div>div>input { |
|
padding: 10px; |
|
font-size: 16px; |
|
border-radius: 10px; |
|
} |
|
|
|
/* Center the image */ |
|
.stImage { |
|
display: block; |
|
margin-left: auto; |
|
margin-right: auto; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |