Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,70 @@
|
|
1 |
# app.py
|
2 |
-
# app.py
|
3 |
-
import os
|
4 |
import streamlit as st
|
5 |
from PIL import Image
|
6 |
-
from groq import Groq
|
7 |
import base64
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
buffered = image.getvalue()
|
16 |
-
return base64.b64encode(buffered).decode()
|
17 |
|
18 |
-
#
|
19 |
-
st.
|
20 |
-
st.markdown("""
|
21 |
-
<style>
|
22 |
-
.main {
|
23 |
-
background-color: #1e1e1e;
|
24 |
-
color: #ffffff;
|
25 |
-
}
|
26 |
-
.stButton>button {
|
27 |
-
background-color: #4CAF50;
|
28 |
-
color: white;
|
29 |
-
font-size: 18px;
|
30 |
-
}
|
31 |
-
</style>
|
32 |
-
""", unsafe_allow_html=True)
|
33 |
|
34 |
-
|
35 |
-
st.
|
|
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
encoded_image = encode_image(uploaded_file)
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
3. Give a confidence percentage for each prediction.
|
52 |
-
4. Explain in simple terms for beginners — avoid complex jargon unless explained.
|
53 |
-
5. List the risks involved with each prediction.
|
54 |
-
6. Provide a summary with what a beginner should do next.
|
55 |
-
7. Format output cleanly with emojis and bullet points.
|
56 |
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
messages=[{"role": "user", "content": prompt}],
|
63 |
-
model="llama-3.3-70b-versatile"
|
64 |
-
)
|
65 |
-
st.success("Analysis Complete!")
|
66 |
-
st.markdown(response.choices[0].message.content)
|
67 |
|
68 |
-
|
69 |
-
|
|
|
|
1 |
# app.py
|
|
|
|
|
2 |
import streamlit as st
|
3 |
from PIL import Image
|
|
|
4 |
import base64
|
5 |
+
import os
|
6 |
+
from groq import Groq
|
7 |
+
import io
|
8 |
|
9 |
+
# --- PAGE CONFIG ---
|
10 |
+
st.set_page_config(
|
11 |
+
page_title="AI Trade Predictor",
|
12 |
+
layout="wide",
|
13 |
+
initial_sidebar_state="expanded"
|
14 |
+
)
|
15 |
|
16 |
+
st.title("📈 AI Trade Predictor")
|
17 |
+
st.markdown("Upload a candlestick chart and let AI analyze trade signals with risk and timeframe guidance.")
|
|
|
|
|
18 |
|
19 |
+
# --- UPLOAD SECTION ---
|
20 |
+
uploaded_file = st.file_uploader("Upload Candlestick Chart (PNG or JPG)", type=["png", "jpg", "jpeg"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
# --- GROQ SETUP ---
|
23 |
+
groq_api_key = st.secrets["GROQ_API_KEY"] if "GROQ_API_KEY" in st.secrets else os.environ.get("GROQ_API_KEY")
|
24 |
+
client = Groq(api_key=groq_api_key)
|
25 |
|
26 |
+
# --- FUNCTION: Convert image to base64 string ---
|
27 |
+
def image_to_base64_str(image_file):
|
28 |
+
img_bytes = image_file.read()
|
29 |
+
base64_str = base64.b64encode(img_bytes).decode("utf-8")
|
30 |
+
return base64_str
|
31 |
|
32 |
+
# --- SHORTER PROMPT TEMPLATE ---
|
33 |
+
prompt_template = """
|
34 |
+
You're an AI financial assistant. Analyze the candlestick chart image and give:
|
35 |
+
1. Signal (Buy/Sell/Hold)
|
36 |
+
2. Confidence percentage
|
37 |
+
3. Reasoning (simple language)
|
38 |
+
4. Suggest best timeframes like 30min, 1hr, 4hr
|
39 |
+
5. Explain key risks in a beginner-friendly way
|
40 |
|
41 |
+
Only reply in concise format.
|
42 |
+
"""
|
|
|
43 |
|
44 |
+
# --- MAIN PREDICTION LOGIC ---
|
45 |
+
if uploaded_file is not None:
|
46 |
+
st.image(uploaded_file, caption="Uploaded Chart", use_column_width=True)
|
47 |
+
with st.spinner("Analyzing chart and generating prediction..."):
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
image_base64 = image_to_base64_str(uploaded_file)
|
50 |
+
|
51 |
+
# Compose final prompt with image reference (simulated for now)
|
52 |
+
full_prompt = prompt_template + "\n[This is a simulated chart image base64: {}]".format(image_base64[:100])
|
53 |
+
|
54 |
+
try:
|
55 |
+
chat_completion = client.chat.completions.create(
|
56 |
+
messages=[
|
57 |
+
{"role": "user", "content": full_prompt}
|
58 |
+
],
|
59 |
+
model="llama-3.3-70b-versatile" # Smaller model to reduce token use
|
60 |
+
)
|
61 |
+
result = chat_completion.choices[0].message.content
|
62 |
+
st.success("Prediction Ready")
|
63 |
+
st.markdown(result)
|
64 |
|
65 |
+
except Exception as e:
|
66 |
+
st.error(f"Something went wrong: {e}")
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
+
# --- FOOTER ---
|
69 |
+
st.markdown("---")
|
70 |
+
st.markdown("Made ❤️ by Abdullah's AI Labs")
|