Simba commited on
Commit ·
a7a04f0
1
Parent(s): 6244fb2
WIP
Browse files- app.py +60 -9
- app_old.py +0 -51
- app_old_old.py +15 -0
app.py
CHANGED
|
@@ -1,15 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import uuid
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
import neovision
|
| 9 |
+
|
| 10 |
+
MARKDOWN = """
|
| 11 |
+
# neovision 💬 + 📸
|
| 12 |
+
|
| 13 |
+
This is a demo of neovision, a tool that allows you to chat with your webcamusinf GTP Vision.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
connector = neovision.OpanAIConnector()
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def save_image_to_drive(image: np.ndarray) -> str:
|
| 20 |
+
image_filename = f"{uuid.uuid4()}.jpeg"
|
| 21 |
+
image_directory = "data"
|
| 22 |
+
os.makedirs(image_directory, exist_ok=True)
|
| 23 |
+
image_path = os.path.join(image_directory, image_filename)
|
| 24 |
+
cv2.imwrite(image_path, image)
|
| 25 |
+
return image_path
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def respond(image: np.ndarray, prompt: str, chat_history):
|
| 30 |
+
image = np.fliplr(image)
|
| 31 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 32 |
+
image_path = save_image_to_drive(image)
|
| 33 |
+
response = connector.simple_prompt(image=image, prompt=prompt)
|
| 34 |
+
chat_history.append(((image_path,), None))
|
| 35 |
+
chat_history.append((prompt, response))
|
| 36 |
+
return "", chat_history
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Define the function that will process the inputs
|
| 40 |
+
def process(inputs):
|
| 41 |
+
image, prompt, chat_history = inputs
|
| 42 |
+
return respond(image, prompt, chat_history)
|
| 43 |
|
| 44 |
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown(MARKDOWN)
|
| 47 |
+
with gr.Row():
|
| 48 |
+
webcam = gr.Image(source="webcam", tool="select", type="numpy", streaming=True) # Correct component for webcam
|
| 49 |
+
message = gr.Textbox()
|
| 50 |
+
chatbot = gr.Chatbot()
|
| 51 |
+
clear_button = gr.Button("Clear")
|
| 52 |
+
submit_button = gr.Button("Submit")
|
| 53 |
|
| 54 |
+
submit_button.click(
|
| 55 |
+
fn=process,
|
| 56 |
+
inputs=[webcam, message, chatbot.chat_history],
|
| 57 |
+
outputs=[message, chatbot]
|
| 58 |
+
)
|
| 59 |
|
| 60 |
+
clear_button.click(
|
| 61 |
+
fn=lambda: ("", []),
|
| 62 |
+
inputs=[],
|
| 63 |
+
outputs=[message, chatbot]
|
| 64 |
+
)
|
| 65 |
|
| 66 |
+
demo.launch(debug=False, show_error=True)
|
|
|
app_old.py
DELETED
|
@@ -1,51 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
import uuid
|
| 4 |
-
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import numpy as np
|
| 7 |
-
|
| 8 |
-
import neovision
|
| 9 |
-
|
| 10 |
-
MARKDOWN = """
|
| 11 |
-
# neovision 💬 + 📸
|
| 12 |
-
|
| 13 |
-
This is a demo of neovision, a tool that allows you to chat with your webcamusinf GTP Vision.
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
connector = neovision.OpanAIConnector()
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def save_image_to_drive(image: np.ndarray) -> str:
|
| 20 |
-
image_filename = f"{uuid.uuid4()}.jpeg"
|
| 21 |
-
image_directory = "data"
|
| 22 |
-
os.makedirs(image_directory, exist_ok=True)
|
| 23 |
-
image_path = os.path.join(image_directory, image_filename)
|
| 24 |
-
cv2.imwrite(image_path, image)
|
| 25 |
-
return image_path
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def respond(image: np.ndarray, prompt: str, chat_history):
|
| 30 |
-
image = np.fliplr(image)
|
| 31 |
-
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 32 |
-
image_path = save_image_to_drive(image)
|
| 33 |
-
response = connector.simple_prompt(image=image, prompt=prompt)
|
| 34 |
-
chat_history.append(((image_path,), None))
|
| 35 |
-
chat_history.append((prompt, response))
|
| 36 |
-
return "", chat_history
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
with gr.Blocks() as demo:
|
| 40 |
-
gr.Markdown(MARKDOWN)
|
| 41 |
-
with gr.Row():
|
| 42 |
-
# webcam = gr.Image(source="webcam", streaming=True)
|
| 43 |
-
webcam = gr.Webcam(mirror_webcam=True, streaming=True)
|
| 44 |
-
with gr.Column():
|
| 45 |
-
chatbot = gr.Chatbot(height=500)
|
| 46 |
-
message = gr.Textbox()
|
| 47 |
-
clear_button = gr.ClearButton([message, chatbot])
|
| 48 |
-
|
| 49 |
-
message.submit(respond, [webcam, message, chatbot], [message, chatbot])
|
| 50 |
-
|
| 51 |
-
demo.launch(debug=False, show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app_old_old.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def snap(image, video):
|
| 5 |
+
return [image, video]
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
demo = gr.Interface(
|
| 9 |
+
snap,
|
| 10 |
+
[gr.Image(sources=["webcam"]), gr.Video(sources=["webcam"])],
|
| 11 |
+
["image", "video"],
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
if __name__ == "__main__":
|
| 15 |
+
demo.launch()
|