File size: 2,141 Bytes
bef155a
3dc74fc
bef155a
 
ad4d684
bef155a
 
 
ec74f6d
672fe26
 
bef155a
672fe26
 
 
 
 
 
 
 
 
 
 
ec74f6d
672fe26
 
34fbb5a
0dd02ff
6b13e4e
 
 
 
ad4d684
8426ee5
672fe26
 
 
ad4d684
ec74f6d
 
 
 
 
 
 
 
 
 
 
bef155a
ad4d684
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import google.generativeai as genai
import gradio as gr
import numpy as np
import PIL.Image
import re

genai.configure(api_key="AIzaSyAj-b3sO_wUguMdpXWScxKzMHxb8C5cels")

def ImageChat(image, prompt):
    # Check image file and convert to a PIL Image object
    if isinstance(image, np.ndarray):
        img = PIL.Image.fromarray(image)
    else:
        try:
            img = PIL.Image.open(image)
        except (AttributeError, IOError) as e:
            return f"Invalid image provided. Please provide a valid image file. Error: {e}"

    # Load model
    model = genai.GenerativeModel("gemini-pro-vision")

    # Generate response
    try:
        response = model.generate_content([prompt, img])
        if not response or not response.text:
            return "No valid response received. The response might have been blocked."

        # Apply rich formatting to the response
        formatted_response = response.text
        for title in ["Trend Analysis", "Price Action", "Entry Point", "Exit Point", "Hold Conditions", "Risk Management", "Timeframe", "Profit Potential"]:
            formatted_response = formatted_response.replace(title, f"**{title.upper()}**")
        formatted_response = re.sub(r"(\d+\.?\d*)", r"**\1**", formatted_response)
        formatted_response = formatted_response.replace('\n', '\n\n')
        return formatted_response
    except ValueError as e:
        return f"Error in generating response: {e}"

# Define the Gradio interface
with gr.Blocks() as app:
    gr.Markdown("# Image Chat")
    image_input = gr.Image(label="Image")
    prompt_input = gr.Textbox(label="Prompt", value="Analyze the attached stock chart image as a technical quant analyst...")
    analyze_button = gr.Button("Analyze", elem_id="analyze_button")
    response_output = gr.Textbox(label="Response")
    
    def analyze_image(image, prompt):
        return ImageChat(image, prompt)
    
    analyze_button.click(fn=analyze_image, inputs=[image_input, prompt_input], outputs=response_output)

# Style the analyze button
app.css = """
#analyze_button {
    background-color: #26de81;
    color: #ffffff;
}
"""

app.launch()