visionB / app.py
Simba
WIP
a7a04f0
raw
history blame
1.74 kB
import os
import cv2
import uuid
import gradio as gr
import numpy as np
import neovision
MARKDOWN = """
# neovision 💬 + 📸
This is a demo of neovision, a tool that allows you to chat with your webcamusinf GTP Vision.
"""
connector = neovision.OpanAIConnector()
def save_image_to_drive(image: np.ndarray) -> str:
image_filename = f"{uuid.uuid4()}.jpeg"
image_directory = "data"
os.makedirs(image_directory, exist_ok=True)
image_path = os.path.join(image_directory, image_filename)
cv2.imwrite(image_path, image)
return image_path
def respond(image: np.ndarray, prompt: str, chat_history):
image = np.fliplr(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image_path = save_image_to_drive(image)
response = connector.simple_prompt(image=image, prompt=prompt)
chat_history.append(((image_path,), None))
chat_history.append((prompt, response))
return "", chat_history
# Define the function that will process the inputs
def process(inputs):
image, prompt, chat_history = inputs
return respond(image, prompt, chat_history)
with gr.Blocks() as demo:
gr.Markdown(MARKDOWN)
with gr.Row():
webcam = gr.Image(source="webcam", tool="select", type="numpy", streaming=True) # Correct component for webcam
message = gr.Textbox()
chatbot = gr.Chatbot()
clear_button = gr.Button("Clear")
submit_button = gr.Button("Submit")
submit_button.click(
fn=process,
inputs=[webcam, message, chatbot.chat_history],
outputs=[message, chatbot]
)
clear_button.click(
fn=lambda: ("", []),
inputs=[],
outputs=[message, chatbot]
)
demo.launch(debug=False, show_error=True)