abdullahrehan commited on
Commit
0f4aad7
Β·
verified Β·
1 Parent(s): a8b0bcf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -41
app.py CHANGED
@@ -1,69 +1,69 @@
1
  # app.py
 
2
  import os
3
  import streamlit as st
4
  from PIL import Image
5
  from groq import Groq
6
- import io
7
-
8
- # Set your GROQ API key here or export as environment variable before running
9
- os.environ["GROQ_API_KEY"] = "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"
10
 
11
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
 
12
 
13
- st.set_page_config(page_title="AI Trade Predictor", layout="wide")
 
 
 
14
 
 
 
15
  st.markdown("""
16
  <style>
17
  .main {
18
- background-color: #0d1117;
19
- color: white;
20
  }
21
  .stButton>button {
22
- background-color: #1f6feb;
23
  color: white;
24
- font-weight: bold;
25
- }
26
- .stFileUploader label {
27
- color: #58a6ff;
28
  }
29
  </style>
30
  """, unsafe_allow_html=True)
31
 
32
- st.title("πŸ’° AI Trade Predictor")
33
- st.markdown("Upload a candlestick chart image, and the AI will analyze it using common trading strategies.")
34
 
35
- uploaded_file = st.file_uploader("Upload Candlestick Chart Image", type=["jpg", "png", "jpeg"])
36
 
37
- if uploaded_file is not None:
38
  image = Image.open(uploaded_file)
39
  st.image(image, caption="Uploaded Chart", use_column_width=True)
40
 
41
- if st.button("Analyze Chart πŸ”"):
42
- with st.spinner("Analyzing chart and generating predictions..."):
43
- # Instead of sending the full image, just notify AI that user uploaded a chart image
44
- prompt = (
45
- "You are a trading expert AI. "
46
- "A user uploaded a candlestick chart image (image content not available). "
47
- "Based on your knowledge of standard technical strategies (RSI, MACD, moving averages, "
48
- "candlestick patterns, support/resistance), analyze this chart and provide:\n"
49
- "1. Should the user BUY or SELL?\n"
50
- "2. Confidence level in percentage.\n"
51
- "3. Best timeframe for this trade.\n"
52
- "4. Risks involved and how this prediction might go wrong.\n"
53
- "5. Explanation of your reasoning.\n"
54
- "Answer concisely and clearly."
55
- )
 
56
 
57
  try:
58
- chat_completion = client.chat.completions.create(
59
  messages=[{"role": "user", "content": prompt}],
60
- model="llama-3.3-70b-versatile",
61
  )
62
- result = chat_completion.choices[0].message.content
63
- st.markdown("### πŸ“ˆ Prediction Result")
64
- st.markdown(result)
65
- except Exception as e:
66
- st.error(f"Error communicating with GROQ API: {e}")
67
- else:
68
- st.info("Please upload a candlestick chart image to begin analysis.")
69
 
 
 
 
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}")