abdullahrehan commited on
Commit
0000616
Β·
verified Β·
1 Parent(s): 9ecdf3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -50
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 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("---")
 
 
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("---")