test / app.py
alikayh's picture
Update app.py
f87bdf0 verified
import streamlit as st
import requests
import pandas as pd
import subprocess
subprocess.run("pip install plotly")
import plotly.graph_objects as go
import io
subprocess.run("pip install -q -U google-generativeai", shell=True)
import google.generativeai as genai
from PIL import Image
# Configure Gemini AI
API_KEY = "AIzaSyBOOuFWwlywK77pLGg82li1m1mkNgeyQz4"
genai.configure(api_key=API_KEY)
model = genai.GenerativeModel("gemini-2.0-flash-exp")
# Function to fetch data from Gemini API
def get_gemini_data(symbol, timeframe):
# Mapping timeframe to Gemini API argument
timeframe_mapping = {
'1m': '1m',
'5m': '5m',
'15m': '15m',
'30m': '30m',
'1h': '1h',
'6h': '6h',
'1d': '1d'
}
url = f"https://api.gemini.com/v2/candles/{symbol}/{timeframe_mapping[timeframe]}"
response = requests.get(url)
data = response.json()
df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
return df
# Function to plot candlestick chart
def plot_candlestick(data):
fig = go.Figure(data=[
go.Candlestick(
x=data['timestamp'],
open=data['open'],
high=data['high'],
low=data['low'],
close=data['close']
)
])
fig.update_xaxes(title_text='Time')
fig.update_yaxes(title_text='Price')
fig.update_layout(title_text='Candlestick Chart')
return fig
# Function to analyze candlestick image with Gemini AI
def analyze_candlestick(model, chart_image):
image_bytes = io.BytesIO()
chart_image.save(image_bytes, format='PNG')
image_bytes = image_bytes.getvalue()
content = f"خودت رو بعنوان یک متخصص کارگزاری تصمیم بگیر و طبق نمودار شمعی که بهت داده شده، یک توصیه معاملاتی برای خرید یا فروش ارائه کن و توضیحات دقیق ارائه ده: {image_bytes}"
response = model.generate_content([content, "خودت رو بعنوان یک متخصص کارگزاری تصمور کن و طبق این نمودار شمعی که بهت داده شده یک توصیه معاملاتی برای خرید یا فروش ارائه کن و توضیحات دقیق ارائه ده"])
return response.text
# Main function
def main():
st.title("Gemini Trading Analysis with AI")
# User input for symbol and timeframe
symbol = st.text_input("Enter trading symbol (e.g., BTCUSD):", value="BTCUSD")
timeframe = st.selectbox("Select timeframe:", ['1m', '5m', '15m', '30m', '1h', '6h', '1d'])
# Fetch and display candlestick chart
if st.button("Analyze"):
df = get_gemini_data(symbol, timeframe)
fig = plot_candlestick(df)
st.plotly_chart(fig)
# Save the plot as an image
img_bytes = io.BytesIO()
fig.write_image(img_bytes, format='png')
img = Image.open(img_bytes)
# Run AI analysis
suggestion = analyze_candlestick(model, img)
st.markdown(suggestion)
if __name__ == "__main__":
main()