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()