|
|
|
import streamlit as st |
|
from PIL import Image |
|
import base64 |
|
import os |
|
from groq import Groq |
|
import io |
|
|
|
|
|
st.set_page_config( |
|
page_title="AI Trade Predictor", |
|
layout="wide", |
|
initial_sidebar_state="expanded" |
|
) |
|
|
|
st.title("π AI Trade Predictor") |
|
st.markdown("Upload a candlestick chart and let AI analyze trade signals with risk and timeframe guidance.") |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload Candlestick Chart (PNG or JPG)", type=["png", "jpg", "jpeg"]) |
|
|
|
|
|
groq_api_key = st.secrets["GROQ_API_KEY"] if "GROQ_API_KEY" in st.secrets else os.environ.get("GROQ_API_KEY") |
|
client = Groq(api_key=groq_api_key) |
|
|
|
|
|
def image_to_base64_str(image_file): |
|
img_bytes = image_file.read() |
|
base64_str = base64.b64encode(img_bytes).decode("utf-8") |
|
return base64_str |
|
|
|
|
|
prompt_template = """ |
|
You're an AI financial assistant. Analyze the candlestick chart image and give: |
|
1. Signal (Buy/Sell/Hold) |
|
2. Confidence percentage |
|
3. Reasoning (simple language) |
|
4. Suggest best timeframes like 30min, 1hr, 4hr |
|
5. Explain key risks in a beginner-friendly way |
|
|
|
Only reply in concise format. |
|
""" |
|
|
|
|
|
if uploaded_file is not None: |
|
st.image(uploaded_file, caption="Uploaded Chart", use_column_width=True) |
|
with st.spinner("Analyzing chart and generating prediction..."): |
|
|
|
image_base64 = image_to_base64_str(uploaded_file) |
|
|
|
|
|
full_prompt = prompt_template + "\n[This is a simulated chart image base64: {}]".format(image_base64[:100]) |
|
|
|
try: |
|
chat_completion = client.chat.completions.create( |
|
messages=[ |
|
{"role": "user", "content": full_prompt} |
|
], |
|
model="llama-3.3-70b-versatile" |
|
) |
|
result = chat_completion.choices[0].message.content |
|
st.success("Prediction Ready") |
|
st.markdown(result) |
|
|
|
except Exception as e: |
|
st.error(f"Something went wrong: {e}") |
|
|
|
|
|
st.markdown("---") |
|
st.markdown("Made β€οΈ by Abdullah's AI Labs") |
|
|