Narayana02 commited on
Commit
8f51cb5
·
verified ·
1 Parent(s): 0654b11

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+ 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)
50
+
51
+ # Define messages for the model
52
+ messages = [
53
+ {
54
+ "role": "user",
55
+ "content": [
56
+ {"type": "text", "text": user_prompt},
57
+ {"type": "image_url", "image_url": {"url": image_url}}
58
+ ]
59
+ }
60
+ ]
61
+
62
+ # Call the model
63
+ completion = client.chat.completions.create(
64
+ model="meta-llama/Llama-3.2-11B-Vision-Instruct",
65
+ messages=messages,
66
+ max_tokens=500
67
+ )
68
+
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("""
85
+ <style>
86
+ .stButton>button {
87
+ background-color: #0072BB;
88
+ color: white;
89
+ font-size: 16px;
90
+ border-radius: 10px;
91
+ padding: 10px 20px;
92
+ font-weight: bold;
93
+ transition: background-color 0.3s;
94
+ }
95
+ .stButton>button:hover {
96
+ background-color: #005f8a;
97
+ }
98
+
99
+ .stTextInput>div>div>input {
100
+ padding: 10px;
101
+ font-size: 16px;
102
+ border-radius: 10px;
103
+ }
104
+
105
+ /* Center the image */
106
+ .stImage {
107
+ display: block;
108
+ margin-left: auto;
109
+ margin-right: auto;
110
+ }
111
+ </style>
112
+ """, unsafe_allow_html=True)