Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,48 @@ import streamlit as st
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from config import HUGGINGFACE_API_KEY # Import your API key from a separate config file
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
|
| 7 |
# Streamlit App Configuration
|
| 8 |
st.set_page_config(page_title="Llama-3.2 Demo App", page_icon="π€", layout="wide")
|
| 9 |
st.title("πΌοΈ Llama-3.2-90B-Vision-Instruct Demo App")
|
| 10 |
-
st.markdown("<p style='text-align: center; font-size: 18px; color: #555;'>
|
| 11 |
|
| 12 |
-
# User Inputs
|
| 13 |
-
|
| 14 |
user_prompt = st.text_input("Enter your prompt", value="Describe this image in a paragraph", placeholder="e.g., What is shown in the image?")
|
| 15 |
|
| 16 |
-
# Function to display the
|
| 17 |
-
def
|
| 18 |
try:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
except Exception as e:
|
| 23 |
-
st.error(f"β Unable to
|
| 24 |
-
return None
|
| 25 |
|
| 26 |
# Process user input
|
| 27 |
if st.button("Get Description", key="get_description"):
|
| 28 |
-
if
|
| 29 |
try:
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
if img is None:
|
| 33 |
-
st.error("β Image processing failed.")
|
| 34 |
-
st.stop()
|
| 35 |
-
|
| 36 |
-
# Convert the image to bytes for model input
|
| 37 |
-
img_buffer = BytesIO()
|
| 38 |
-
img.save(img_buffer, format="PNG")
|
| 39 |
-
img_bytes = img_buffer.getvalue()
|
| 40 |
|
| 41 |
# Initialize the InferenceClient
|
| 42 |
client = InferenceClient(api_key=HUGGINGFACE_API_KEY)
|
|
@@ -47,7 +54,7 @@ if st.button("Get Description", key="get_description"):
|
|
| 47 |
"role": "user",
|
| 48 |
"content": [
|
| 49 |
{"type": "text", "text": user_prompt},
|
| 50 |
-
{"type": "
|
| 51 |
]
|
| 52 |
}
|
| 53 |
]
|
|
@@ -62,14 +69,16 @@ if st.button("Get Description", key="get_description"):
|
|
| 62 |
# Extract JSON response
|
| 63 |
model_response = completion.choices[0].message
|
| 64 |
|
| 65 |
-
# Display the result
|
| 66 |
st.subheader("π Model Response")
|
|
|
|
|
|
|
| 67 |
st.markdown(f"**Description**: {model_response.get('content', 'No description available')}")
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
st.error(f"β An error occurred: {e}")
|
| 71 |
else:
|
| 72 |
-
st.warning("β οΈ Please
|
| 73 |
|
| 74 |
# Clean UI Enhancements
|
| 75 |
st.markdown("""
|
|
@@ -93,10 +102,6 @@ st.markdown("""
|
|
| 93 |
border-radius: 10px;
|
| 94 |
}
|
| 95 |
|
| 96 |
-
.stFileUploader>div>div {
|
| 97 |
-
border-radius: 10px;
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
/* Center the image */
|
| 101 |
.stImage {
|
| 102 |
display: block;
|
|
@@ -104,4 +109,4 @@ st.markdown("""
|
|
| 104 |
margin-right: auto;
|
| 105 |
}
|
| 106 |
</style>
|
| 107 |
-
""", unsafe_allow_html=True)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from config import HUGGINGFACE_API_KEY # Import your API key from a separate config file
|
| 4 |
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
# Streamlit App Configuration
|
| 9 |
st.set_page_config(page_title="Llama-3.2 Demo App", page_icon="π€", layout="wide")
|
| 10 |
st.title("πΌοΈ Llama-3.2-90B-Vision-Instruct Demo App")
|
| 11 |
+
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)
|
| 12 |
|
| 13 |
+
# User Inputs with placeholder
|
| 14 |
+
image_url = st.text_input("Enter Image URL", value="", placeholder="Paste image URL here...", max_chars=400)
|
| 15 |
user_prompt = st.text_input("Enter your prompt", value="Describe this image in a paragraph", placeholder="e.g., What is shown in the image?")
|
| 16 |
|
| 17 |
+
# Function to display the image from URL with height limit based on its actual size
|
| 18 |
+
def show_image_from_url(image_url, max_height=200):
|
| 19 |
try:
|
| 20 |
+
response = requests.get(image_url)
|
| 21 |
+
img = Image.open(BytesIO(response.content))
|
| 22 |
+
|
| 23 |
+
# Get the original image size
|
| 24 |
+
img_width, img_height = img.size
|
| 25 |
+
|
| 26 |
+
# Calculate the new height and width based on the max height while maintaining the aspect ratio
|
| 27 |
+
if img_height > max_height:
|
| 28 |
+
aspect_ratio = img_width / img_height
|
| 29 |
+
new_height = max_height
|
| 30 |
+
new_width = int(new_height * aspect_ratio)
|
| 31 |
+
img_resized = img.resize((new_width, new_height))
|
| 32 |
+
else:
|
| 33 |
+
img_resized = img # No resizing needed if the image is smaller than the max height
|
| 34 |
+
|
| 35 |
+
# Center the image and display it
|
| 36 |
+
st.image(img_resized, caption=f"Source: {image_url}", use_container_width=True)
|
| 37 |
+
|
| 38 |
except Exception as e:
|
| 39 |
+
st.error(f"β Unable to load image. Error: {e}")
|
|
|
|
| 40 |
|
| 41 |
# Process user input
|
| 42 |
if st.button("Get Description", key="get_description"):
|
| 43 |
+
if image_url and user_prompt:
|
| 44 |
try:
|
| 45 |
+
# Show the image with dynamic resizing based on the image size
|
| 46 |
+
show_image_from_url(image_url, max_height=600)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Initialize the InferenceClient
|
| 49 |
client = InferenceClient(api_key=HUGGINGFACE_API_KEY)
|
|
|
|
| 54 |
"role": "user",
|
| 55 |
"content": [
|
| 56 |
{"type": "text", "text": user_prompt},
|
| 57 |
+
{"type": "image_url", "image_url": {"url": image_url}}
|
| 58 |
]
|
| 59 |
}
|
| 60 |
]
|
|
|
|
| 69 |
# Extract JSON response
|
| 70 |
model_response = completion.choices[0].message
|
| 71 |
|
| 72 |
+
# Display the result in a clean and simple format
|
| 73 |
st.subheader("π Model Response")
|
| 74 |
+
|
| 75 |
+
# Display Content
|
| 76 |
st.markdown(f"**Description**: {model_response.get('content', 'No description available')}")
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
st.error(f"β An error occurred: {e}")
|
| 80 |
else:
|
| 81 |
+
st.warning("β οΈ Please enter an image URL and a prompt.")
|
| 82 |
|
| 83 |
# Clean UI Enhancements
|
| 84 |
st.markdown("""
|
|
|
|
| 102 |
border-radius: 10px;
|
| 103 |
}
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
/* Center the image */
|
| 106 |
.stImage {
|
| 107 |
display: block;
|
|
|
|
| 109 |
margin-right: auto;
|
| 110 |
}
|
| 111 |
</style>
|
| 112 |
+
""", unsafe_allow_html=True)
|