sohoso commited on
Commit
8426ee5
·
verified ·
1 Parent(s): 7b7f570

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -35
app.py CHANGED
@@ -4,11 +4,32 @@ import numpy as np
4
  import PIL.Image
5
  import io
6
 
7
- # Configure API key for Google Generative AI
8
  genai.configure(api_key="AIzaSyAj-b3sO_wUguMdpXWScxKzMHxb8C5cels")
9
 
10
- # Function to handle image input and generate analysis
11
- def ImageChat(image, user_prompt):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  # Check image file and convert to a PIL Image object
13
  if isinstance(image, np.ndarray):
14
  img = PIL.Image.fromarray(image)
@@ -21,50 +42,23 @@ def ImageChat(image, user_prompt):
21
  # Load model
22
  model = genai.GenerativeModel("gemini-pro-vision")
23
 
24
- # Define custom prompt for stock chart analysis
25
- custom_prompt = f"""
26
- Analyze the attached stock chart image as a technical quant analyst. Based on the trend, movement, and price action visible in the chart, make intelligent and substantial trading suggestions. Provide clear entry, exit, and hold conditions, along with detailed risk management strategies. Discuss the expected timeframe for the trade, specifying the duration in terms of the timesteps shown in the chart. Aim for a good profit-to-time ratio, favoring high profits within shorter timeframes unless longer timeframes yield significantly higher profits. Reference absolute values from the chart rather than relying on general strategies. Avoid basic explanations of indicators, and focus on providing in-depth analysis.
27
-
28
- Key Elements to Include:
29
-
30
- **Trend Analysis:** Identify the current trend and its strength (e.g., bullish, bearish, consolidation).
31
- **Price Action:** Describe recent price movements and patterns (e.g., support and resistance levels, candlestick patterns).
32
- **Entry and Exit Points:** Specify precise entry and exit points based on the chart’s data.
33
- **Hold Conditions:** Determine the conditions under which holding the position is advisable.
34
- **Risk Management:** Outline stop-loss levels, position sizing, and other risk management techniques.
35
- **Timeframe:** Indicate the expected duration of the trade in terms of the chart’s timesteps, balancing profit potential with trade duration.
36
- **Profit Potential:** Estimate the potential profit in **absolute values** as shown in the chart.
37
-
38
- Example Analysis Structure:
39
-
40
- **Trend Analysis:** The stock is currently in a strong bullish trend, evidenced by higher highs and higher lows over the past **X** timesteps.
41
- **Price Action:** Recent price action shows a breakout from a key resistance level at **$Y**, indicating strong upward momentum.
42
- **Entry Point:** Enter the trade at **$Z**, where the breakout is confirmed.
43
- **Exit Point:** Exit the trade at **$A**, near the next major resistance level, for a potential profit of **$B** per share.
44
- **Hold Conditions:** Hold the position as long as the price remains above the support level at **$C**.
45
- **Risk Management:** Set a stop-loss at **$D** to limit potential losses to **$E** per share, and use position sizing to ensure the total risk is within acceptable limits.
46
- **Timeframe:** The expected duration of the trade is **F** timesteps, aiming for a profit of **$G** in this period.
47
-
48
- {user_prompt}
49
- """
50
-
51
  # Generate response
52
  try:
53
  response = model.generate_content([custom_prompt, img])
54
  if not response or not response.text:
55
  return "No valid response received. The response might have been blocked."
56
- return response.text
 
 
57
  except ValueError as e:
58
  return f"Error in generating response: {e}"
59
 
60
- # Create the Gradio interface
61
  app = gr.Interface(
62
  fn=ImageChat,
63
- inputs=[gr.Image(label="Stock Chart Image", type="pil"), gr.Textbox(label="Continue Chat")],
64
- outputs=gr.Markdown(label="Analysis"),
65
  title="Stock Chart Analysis",
66
  theme=gr.themes.Soft()
67
  )
68
 
69
- # Launch the app
70
  app.launch()
 
4
  import PIL.Image
5
  import io
6
 
 
7
  genai.configure(api_key="AIzaSyAj-b3sO_wUguMdpXWScxKzMHxb8C5cels")
8
 
9
+ # Custom prompt
10
+ custom_prompt = """
11
+ Analyze the attached stock chart image as a technical quant analyst. Based on the trend, movement, and price action visible in the chart, make intelligent and substantial trading suggestions. Provide clear entry, exit, and hold conditions, along with detailed risk management strategies. Discuss the expected timeframe for the trade, specifying the duration in terms of the timesteps shown in the chart. Aim for a good profit-to-time ratio, favoring high profits within shorter timeframes unless longer timeframes yield significantly higher profits. Reference absolute values from the chart rather than relying on general strategies. Avoid basic explanations of indicators, and focus on providing in-depth analysis.
12
+
13
+ Key Elements to Include:
14
+ - **Trend Analysis**: Identify the current trend and its strength (e.g., bullish, bearish, consolidation).
15
+ - **Price Action**: Describe recent price movements and patterns (e.g., support and resistance levels, candlestick patterns).
16
+ - **Entry and Exit Points**: Specify precise entry and exit points based on the chart’s data.
17
+ - **Hold Conditions**: Determine the conditions under which holding the position is advisable.
18
+ - **Risk Management**: Outline stop-loss levels, position sizing, and other risk management techniques.
19
+ - **Timeframe**: Indicate the expected duration of the trade in terms of the chart’s timesteps, balancing profit potential with trade duration.
20
+ - **Profit Potential**: Estimate the potential profit in absolute values as shown in the chart.
21
+
22
+ Example Analysis Structure:
23
+ - **Trend Analysis**: The stock is currently in a strong bullish trend, evidenced by higher highs and higher lows over the past **X** timesteps.
24
+ - **Price Action**: Recent price action shows a breakout from a key resistance level at **$Y**, indicating strong upward momentum.
25
+ - **Entry Point**: Enter the trade at **$Z**, where the breakout is confirmed.
26
+ - **Exit Point**: Exit the trade at **$A**, near the next major resistance level, for a potential profit of **$B** per share.
27
+ - **Hold Conditions**: Hold the position as long as the price remains above the support level at **$C**.
28
+ - **Risk Management**: Set a stop-loss at **$D** to limit potential losses to **$E** per share, and use position sizing to ensure the total risk is within acceptable limits.
29
+ - **Timeframe**: The expected duration of the trade is **F** timesteps, aiming for a profit of **$G** in this period.
30
+ """
31
+
32
+ def ImageChat(image):
33
  # Check image file and convert to a PIL Image object
34
  if isinstance(image, np.ndarray):
35
  img = PIL.Image.fromarray(image)
 
42
  # Load model
43
  model = genai.GenerativeModel("gemini-pro-vision")
44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  # Generate response
46
  try:
47
  response = model.generate_content([custom_prompt, img])
48
  if not response or not response.text:
49
  return "No valid response received. The response might have been blocked."
50
+ # Add rich formatting to the output
51
+ formatted_response = response.text.replace("\n", "<br>").replace("$", "<b>$").replace("**", "</b>")
52
+ return formatted_response
53
  except ValueError as e:
54
  return f"Error in generating response: {e}"
55
 
 
56
  app = gr.Interface(
57
  fn=ImageChat,
58
+ inputs=gr.Image(label="Upload Stock Chart Image"),
59
+ outputs=gr.HTML(label="Analysis Response"),
60
  title="Stock Chart Analysis",
61
  theme=gr.themes.Soft()
62
  )
63
 
 
64
  app.launch()