abdullahrehan commited on
Commit
3ac8611
·
verified ·
1 Parent(s): 0f4aad7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -54
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
- # Set up Groq API
10
- GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
11
- client = Groq(api_key=GROQ_API_KEY)
 
 
 
12
 
13
- # Helper function to encode image as base64
14
- def encode_image(image):
15
- buffered = image.getvalue()
16
- return base64.b64encode(buffered).decode()
17
 
18
- # Streamlit UI
19
- st.set_page_config(page_title="AI Trade Predictor", layout="centered")
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
- st.title("🤖 AI Trade Predictor")
35
- st.write("Upload your candlestick chart image and let AI analyze the trading signals for you.")
 
36
 
37
- uploaded_file = st.file_uploader("Upload a candlestick chart image", type=["png", "jpg", "jpeg"])
 
 
 
 
38
 
39
- if uploaded_file:
40
- image = Image.open(uploaded_file)
41
- st.image(image, caption="Uploaded Chart", use_column_width=True)
 
 
 
 
 
42
 
43
- if st.button("Analyze Chart"):
44
- with st.spinner("Analyzing..."):
45
- encoded_image = encode_image(uploaded_file)
46
 
47
- prompt = f"""
48
- You are an AI trading assistant. A user has uploaded a candlestick chart. Based on the chart image, perform the following:
49
- 1. Identify key signals like bullish/bearish patterns, RSI, MACD etc.
50
- 2. Predict BUY/SELL/WAIT decisions for multiple timeframes (e.g., 30m, 1h, 4h, 1d).
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
- Here is the chart (as base64): {encoded_image}
58
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- try:
61
- response = client.chat.completions.create(
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
- except Exception as e:
69
- st.error(f"Something went wrong: {e}")
 
 
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")