Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +90 -38
src/streamlit_app.py
CHANGED
@@ -1,40 +1,92 @@
|
|
1 |
-
import altair as alt
|
2 |
-
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
""
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import openai
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
from groq import Groq
|
6 |
+
import base64
|
7 |
+
from PIL import Image
|
8 |
+
import io
|
9 |
|
10 |
+
# Set page config
|
11 |
+
st.set_page_config(
|
12 |
+
page_title="Image Analysis with AI",
|
13 |
+
page_icon="🖼️",
|
14 |
+
layout="centered"
|
15 |
+
)
|
16 |
+
|
17 |
+
# Add title and description
|
18 |
+
st.title("AI Image Analysis")
|
19 |
+
st.markdown("Upload an image or provide an image URL to get AI-generated commentary.")
|
20 |
+
|
21 |
+
# Load environment variables
|
22 |
+
load_dotenv()
|
23 |
+
|
24 |
+
# Initialize Groq client
|
25 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
26 |
+
|
27 |
+
# Create input field for image URL
|
28 |
+
image_url = st.text_input("Enter Image URL", "https://static.seekingalpha.com/uploads/2016/1/19/saupload_fredgraph.jpg")
|
29 |
+
|
30 |
+
# Add a file uploader
|
31 |
+
uploaded_file = st.file_uploader("Or upload an image", type=["jpg", "jpeg", "png"])
|
32 |
+
|
33 |
+
def get_image_url():
|
34 |
+
if uploaded_file is not None:
|
35 |
+
# Convert uploaded file to base64
|
36 |
+
image = Image.open(uploaded_file)
|
37 |
+
buffered = io.BytesIO()
|
38 |
+
image.save(buffered, format="PNG")
|
39 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
40 |
+
return f"data:image/png;base64,{img_str}"
|
41 |
+
return image_url
|
42 |
+
|
43 |
+
# Add a button to trigger analysis
|
44 |
+
if st.button("Analyze Image"):
|
45 |
+
try:
|
46 |
+
# Show loading spinner
|
47 |
+
with st.spinner("Analyzing image..."):
|
48 |
+
current_image_url = get_image_url()
|
49 |
+
|
50 |
+
# Create the completion request
|
51 |
+
completion = client.chat.completions.create(
|
52 |
+
model="meta-llama/llama-4-scout-17b-16e-instruct",
|
53 |
+
messages=[
|
54 |
+
{
|
55 |
+
"role": "user",
|
56 |
+
"content": [
|
57 |
+
{
|
58 |
+
"type": "text",
|
59 |
+
"text": "Write a detailed commentary on the trend observed in the image?"
|
60 |
+
},
|
61 |
+
{
|
62 |
+
"type": "image_url",
|
63 |
+
"image_url": {
|
64 |
+
"url": current_image_url
|
65 |
+
}
|
66 |
+
}
|
67 |
+
]
|
68 |
+
}
|
69 |
+
],
|
70 |
+
temperature=1,
|
71 |
+
max_completion_tokens=300,
|
72 |
+
top_p=1,
|
73 |
+
stream=False,
|
74 |
+
stop=None,
|
75 |
+
)
|
76 |
+
|
77 |
+
# Display the image
|
78 |
+
if uploaded_file is not None:
|
79 |
+
st.image(uploaded_file, caption="Analyzed Image", use_column_width=True)
|
80 |
+
else:
|
81 |
+
st.image(image_url, caption="Analyzed Image", use_column_width=True)
|
82 |
+
|
83 |
+
# Display the analysis
|
84 |
+
st.subheader("AI Analysis")
|
85 |
+
st.write(completion.choices[0].message.content)
|
86 |
+
|
87 |
+
except Exception as e:
|
88 |
+
st.error(f"An error occurred: {str(e)}")
|
89 |
+
|
90 |
+
# Add footer
|
91 |
+
st.markdown("---")
|
92 |
+
st.markdown("Built with Streamlit and Groq AI")
|