Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,101 @@
|
|
1 |
-
import
|
2 |
from groq import Groq
|
3 |
import base64
|
4 |
from PIL import Image
|
5 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Encode image to base64
|
8 |
def encode_image_to_base64(image):
|
@@ -10,8 +103,8 @@ def encode_image_to_base64(image):
|
|
10 |
image.save(buffered, format="JPEG")
|
11 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
12 |
|
13 |
-
# Prediction function
|
14 |
-
def
|
15 |
if not api_key:
|
16 |
return "โ Please provide your Groq API key."
|
17 |
|
@@ -19,35 +112,99 @@ def predict(api_key, image, user_prompt):
|
|
19 |
client = Groq(api_key=api_key)
|
20 |
base64_image = encode_image_to_base64(image)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
|
37 |
except Exception as e:
|
38 |
return f"โ ๏ธ Error: {str(e)}"
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
-
|
|
|
|
1 |
+
import streamlit as st
|
2 |
from groq import Groq
|
3 |
import base64
|
4 |
from PIL import Image
|
5 |
import io
|
6 |
+
import time
|
7 |
+
|
8 |
+
# Set page config for futuristic theme
|
9 |
+
st.set_page_config(
|
10 |
+
page_title="NeuraVision AI",
|
11 |
+
page_icon="๐ฎ",
|
12 |
+
layout="wide",
|
13 |
+
initial_sidebar_state="expanded"
|
14 |
+
)
|
15 |
+
|
16 |
+
# Custom CSS for futuristic aesthetic
|
17 |
+
def set_custom_css():
|
18 |
+
st.markdown("""
|
19 |
+
<style>
|
20 |
+
:root {
|
21 |
+
--primary-color: #00f2ff;
|
22 |
+
--secondary-color: #ff00e6;
|
23 |
+
--dark-bg: #0a0a1a;
|
24 |
+
--darker-bg: #050510;
|
25 |
+
--card-bg: rgba(15, 15, 35, 0.7);
|
26 |
+
}
|
27 |
+
|
28 |
+
body {
|
29 |
+
background-color: var(--dark-bg);
|
30 |
+
color: white;
|
31 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
32 |
+
}
|
33 |
+
|
34 |
+
.stApp {
|
35 |
+
background: linear-gradient(135deg, var(--darker-bg) 0%, var(--dark-bg) 100%);
|
36 |
+
}
|
37 |
+
|
38 |
+
.stTextInput>div>div>input, .stTextArea>div>div>textarea {
|
39 |
+
background-color: rgba(20, 20, 40, 0.8) !important;
|
40 |
+
color: white !important;
|
41 |
+
border: 1px solid var(--primary-color) !important;
|
42 |
+
}
|
43 |
+
|
44 |
+
.stButton>button {
|
45 |
+
background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
|
46 |
+
color: black !important;
|
47 |
+
font-weight: bold;
|
48 |
+
border: none;
|
49 |
+
border-radius: 5px;
|
50 |
+
padding: 0.5rem 1rem;
|
51 |
+
transition: all 0.3s ease;
|
52 |
+
}
|
53 |
+
|
54 |
+
.stButton>button:hover {
|
55 |
+
transform: scale(1.05);
|
56 |
+
box-shadow: 0 0 15px var(--primary-color);
|
57 |
+
}
|
58 |
+
|
59 |
+
.stMarkdown {
|
60 |
+
color: white;
|
61 |
+
}
|
62 |
+
|
63 |
+
.sidebar .sidebar-content {
|
64 |
+
background-color: var(--darker-bg);
|
65 |
+
border-right: 1px solid rgba(0, 242, 255, 0.2);
|
66 |
+
}
|
67 |
+
|
68 |
+
h1, h2, h3 {
|
69 |
+
background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
|
70 |
+
-webkit-background-clip: text;
|
71 |
+
-webkit-text-fill-color: transparent;
|
72 |
+
}
|
73 |
+
|
74 |
+
.card {
|
75 |
+
background-color: var(--card-bg);
|
76 |
+
border-radius: 10px;
|
77 |
+
padding: 1.5rem;
|
78 |
+
margin-bottom: 1rem;
|
79 |
+
border-left: 3px solid var(--primary-color);
|
80 |
+
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
|
81 |
+
}
|
82 |
+
|
83 |
+
.glow {
|
84 |
+
animation: glow 2s infinite alternate;
|
85 |
+
}
|
86 |
+
|
87 |
+
@keyframes glow {
|
88 |
+
from {
|
89 |
+
box-shadow: 0 0 5px var(--primary-color);
|
90 |
+
}
|
91 |
+
to {
|
92 |
+
box-shadow: 0 0 20px var(--primary-color);
|
93 |
+
}
|
94 |
+
}
|
95 |
+
</style>
|
96 |
+
""", unsafe_allow_html=True)
|
97 |
+
|
98 |
+
set_custom_css()
|
99 |
|
100 |
# Encode image to base64
|
101 |
def encode_image_to_base64(image):
|
|
|
103 |
image.save(buffered, format="JPEG")
|
104 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
105 |
|
106 |
+
# Prediction function
|
107 |
+
def analyze_image(api_key, image, prompt):
|
108 |
if not api_key:
|
109 |
return "โ Please provide your Groq API key."
|
110 |
|
|
|
112 |
client = Groq(api_key=api_key)
|
113 |
base64_image = encode_image_to_base64(image)
|
114 |
|
115 |
+
with st.spinner('๐ Processing image with quantum neural networks...'):
|
116 |
+
time.sleep(1) # For dramatic effect
|
117 |
+
response = client.chat.completions.create(
|
118 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
119 |
+
messages=[
|
120 |
+
{
|
121 |
+
"role": "user",
|
122 |
+
"content": [
|
123 |
+
{"type": "text", "text": prompt},
|
124 |
+
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}
|
125 |
+
]
|
126 |
+
}
|
127 |
+
],
|
128 |
+
max_tokens=1024,
|
129 |
+
)
|
130 |
+
return response.choices[0].message.content
|
131 |
|
132 |
except Exception as e:
|
133 |
return f"โ ๏ธ Error: {str(e)}"
|
134 |
|
135 |
+
# App layout
|
136 |
+
def main():
|
137 |
+
st.title("๐ฎ NeuraVision AI")
|
138 |
+
st.markdown("### Quantum-Powered Image Analysis with Groq & LLaMA 4 Scout")
|
139 |
+
st.markdown("---")
|
140 |
+
|
141 |
+
col1, col2 = st.columns([1, 2])
|
142 |
+
|
143 |
+
with col1:
|
144 |
+
with st.container():
|
145 |
+
st.markdown("### โ๏ธ Configuration Panel")
|
146 |
+
api_key = st.text_input("๐ Groq API Key", type="password", help="Enter your Groq API key")
|
147 |
+
|
148 |
+
uploaded_file = st.file_uploader("๐ธ Upload Image", type=["png", "jpg", "jpeg"],
|
149 |
+
help="Upload an image for analysis")
|
150 |
+
|
151 |
+
prompt = st.text_area("๐ฌ Analysis Prompt",
|
152 |
+
value="Describe this image in detail, including all important elements and their relationships.",
|
153 |
+
height=150)
|
154 |
+
|
155 |
+
if st.button("๐ Analyze Image", use_container_width=True):
|
156 |
+
if uploaded_file is not None:
|
157 |
+
image = Image.open(uploaded_file)
|
158 |
+
with col2:
|
159 |
+
with st.container():
|
160 |
+
st.markdown("### ๐ Analysis Results")
|
161 |
+
st.image(image, caption="Uploaded Image", width=300)
|
162 |
+
|
163 |
+
result = analyze_image(api_key, image, prompt)
|
164 |
+
st.markdown("### ๐ Insights")
|
165 |
+
st.markdown(f"<div class='card glow'>{result}</div>", unsafe_allow_html=True)
|
166 |
+
else:
|
167 |
+
st.warning("Please upload an image first.")
|
168 |
+
|
169 |
+
with col2:
|
170 |
+
if 'result' not in st.session_state:
|
171 |
+
st.markdown("### ๐ Analysis Results")
|
172 |
+
st.markdown("""
|
173 |
+
<div class='card'>
|
174 |
+
<h4>Welcome to NeuraVision AI</h4>
|
175 |
+
<p>Upload an image and provide a prompt to get started with quantum-powered analysis.</p>
|
176 |
+
<p>Try prompts like:</p>
|
177 |
+
<ul>
|
178 |
+
<li>"Describe this image in detail"</li>
|
179 |
+
<li>"What emotions does this image convey?"</li>
|
180 |
+
<li>"Analyze the composition and artistic elements"</li>
|
181 |
+
</ul>
|
182 |
+
</div>
|
183 |
+
""", unsafe_allow_html=True)
|
184 |
+
|
185 |
+
st.image("https://via.placeholder.com/600x400/0a0a1a/00f2ff?text=Upload+an+Image",
|
186 |
+
caption="Your analysis will appear here", width=400)
|
187 |
+
|
188 |
+
# Sidebar
|
189 |
+
with st.sidebar:
|
190 |
+
st.markdown("## ๐ Quantum Console")
|
191 |
+
st.markdown("""
|
192 |
+
<div class='card'>
|
193 |
+
<p>System Status: <span style='color: var(--primary-color)'>Online</span></p>
|
194 |
+
<p>Model: LLaMA 4 Scout 17B</p>
|
195 |
+
<p>Processor: Quantum Core</p>
|
196 |
+
</div>
|
197 |
+
""", unsafe_allow_html=True)
|
198 |
+
|
199 |
+
st.markdown("### โก Quick Prompts")
|
200 |
+
if st.button("Describe technically"):
|
201 |
+
st.session_state.prompt = "Provide a detailed technical description of this image, including objects, colors, composition, and any notable features."
|
202 |
+
|
203 |
+
if st.button("Analyze emotions"):
|
204 |
+
st.session_state.prompt = "What emotions does this image convey? Describe the mood, atmosphere, and any emotional elements present."
|
205 |
+
|
206 |
+
if st.button("Artistic critique"):
|
207 |
+
st.session_state.prompt = "Provide an artistic critique of this image, discussing composition, color theory, lighting, and artistic merit."
|
208 |
|
209 |
+
if __name__ == "__main__":
|
210 |
+
main()
|