Spaces:
Runtime error
Runtime error
File size: 1,529 Bytes
bef155a 3dc74fc bef155a 672fe26 bef155a 34fbb5a 672fe26 bef155a 672fe26 34fbb5a 672fe26 34fbb5a 672fe26 34fbb5a 8426ee5 34fbb5a 8426ee5 672fe26 34fbb5a 8426ee5 5f61616 672fe26 bef155a 154c1fd |
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 |
import google.generativeai as genai
import gradio as gr
import numpy as np
import PIL.Image
import io
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")
# Combine the provided prompt with the custom prompt
combined_prompt = f"{prompt}\n\n{custom_prompt}"
# Generate response
try:
response = model.generate_content([combined_prompt, img])
if not response or not response.text:
return "No valid response received. The response might have been blocked."
# Add rich formatting to the output
formatted_response = response.text.replace("\n", "<br>").replace(":", ":</b>").replace("\n", "<br><b>")
formatted_response = f"<b>{formatted_response}"
return formatted_response
except ValueError as e:
return f"Error in generating response: {e}"
app = gr.Interface(
fn=ImageChat,
inputs=[gr.Image(label="Upload Stock Chart Image"), gr.Text(label="Prompt")],
outputs=gr.HTML(label="Analysis Response"),
title="Stock Chart Analysis",
theme=gr.themes.Soft()
)
app.launch()
|