Update app.py
Browse files
app.py
CHANGED
@@ -1,69 +1,75 @@
|
|
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
|
8 |
|
9 |
-
|
10 |
-
st.set_page_config(
|
11 |
-
page_title="AI Trade Predictor",
|
12 |
-
layout="wide",
|
13 |
-
initial_sidebar_state="expanded"
|
14 |
-
)
|
15 |
|
16 |
-
st.
|
17 |
-
st.markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
uploaded_file = st.file_uploader("Upload Candlestick Chart (PNG or JPG)", type=["png", "jpg", "jpeg"])
|
21 |
|
22 |
-
|
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 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
return base64_str
|
31 |
|
32 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
4. Suggest best timeframes like 30min, 1hr, 4hr
|
39 |
-
5. Explain key risks in a beginner-friendly way
|
40 |
|
41 |
-
|
42 |
-
""
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
|
|
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
55 |
-
|
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("---")
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from PIL import Image
|
|
|
3 |
import os
|
4 |
from groq import Groq
|
5 |
+
from dotenv import load_dotenv
|
6 |
|
7 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
st.set_page_config(page_title="AI Trade Predictor", layout="wide")
|
10 |
+
st.markdown("""
|
11 |
+
<style>
|
12 |
+
.main {
|
13 |
+
background-color: #f5f7fa;
|
14 |
+
}
|
15 |
+
h1 {
|
16 |
+
color: #3b3b3b;
|
17 |
+
}
|
18 |
+
.stButton > button {
|
19 |
+
color: white;
|
20 |
+
background-color: #4CAF50;
|
21 |
+
font-size: 16px;
|
22 |
+
border-radius: 10px;
|
23 |
+
padding: 10px 24px;
|
24 |
+
}
|
25 |
+
</style>
|
26 |
+
""", unsafe_allow_html=True)
|
27 |
|
28 |
+
st.title("π AI Trade Predictor")
|
|
|
29 |
|
30 |
+
uploaded_file = st.file_uploader("Upload a candlestick chart image", type=["png", "jpg", "jpeg"])
|
|
|
|
|
31 |
|
32 |
+
if uploaded_file:
|
33 |
+
image = Image.open(uploaded_file)
|
34 |
+
st.image(image, caption='Uploaded Chart', use_column_width=True)
|
35 |
+
st.write("Analyzing chart using AI model...")
|
|
|
36 |
|
37 |
+
# Convert image to base64 string
|
38 |
+
import base64
|
39 |
+
import io
|
40 |
+
buffered = io.BytesIO()
|
41 |
+
image.save(buffered, format="PNG")
|
42 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
|
|
|
|
43 |
|
44 |
+
# Connect to Groq and send the image analysis prompt
|
45 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
46 |
|
47 |
+
user_prompt = f"""
|
48 |
+
Analyze the following candlestick chart image (base64-encoded PNG) and provide a trading decision:
|
49 |
+
- Tell whether the action should be BUY, SELL, or HOLD.
|
50 |
+
- Give confidence level in percentage.
|
51 |
+
- Suggest timeframes (e.g., 30 min, 1 hour, 4 hour, 1 day) and what signal applies to each.
|
52 |
+
- List any risks or reasons the prediction may fail.
|
53 |
+
- Use clear language that a beginner can understand.
|
54 |
+
- Give a short summary at the end.
|
55 |
|
56 |
+
Image (base64 PNG): {img_str}
|
57 |
+
"""
|
58 |
|
59 |
+
try:
|
60 |
+
chat_completion = client.chat.completions.create(
|
61 |
+
messages=[
|
62 |
+
{"role": "user", "content": user_prompt}
|
63 |
+
],
|
64 |
+
model="meta-llama/llama-guard-4-12b"
|
65 |
+
)
|
66 |
+
response = chat_completion.choices[0].message.content
|
67 |
+
st.success("Prediction Complete")
|
68 |
+
st.markdown(response)
|
69 |
|
70 |
+
except Exception as e:
|
71 |
+
st.error(f"Something went wrong: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
|
|
|
|
73 |
|
74 |
# --- FOOTER ---
|
75 |
st.markdown("---")
|