abdullahrehan's picture
Update app.py
3ac8611 verified
raw
history blame
2.29 kB
# app.py
import streamlit as st
from PIL import Image
import base64
import os
from groq import Groq
import io
# --- PAGE CONFIG ---
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.")
# --- UPLOAD SECTION ---
uploaded_file = st.file_uploader("Upload Candlestick Chart (PNG or JPG)", type=["png", "jpg", "jpeg"])
# --- GROQ SETUP ---
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)
# --- FUNCTION: Convert image to base64 string ---
def image_to_base64_str(image_file):
img_bytes = image_file.read()
base64_str = base64.b64encode(img_bytes).decode("utf-8")
return base64_str
# --- SHORTER PROMPT TEMPLATE ---
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.
"""
# --- MAIN PREDICTION LOGIC ---
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)
# Compose final prompt with image reference (simulated for now)
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" # Smaller model to reduce token use
)
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}")
# --- FOOTER ---
st.markdown("---")
st.markdown("Made ❀️ by Abdullah's AI Labs")