shivace007 commited on
Commit
3011aaf
·
verified ·
1 Parent(s): a2bea46

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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")