Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,13 @@
|
|
1 |
import cv2
|
2 |
import gradio as gr
|
3 |
-
import edge_tts
|
4 |
import tempfile
|
5 |
-
import
|
6 |
from torchvision.models.detection import fasterrcnn_resnet50_fpn
|
7 |
import torchvision.transforms as transforms
|
8 |
from PIL import Image
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
def __init__(self, weights_path, cfg_path, names_path):
|
13 |
-
self.net = cv2.dnn.readNet(weights_path, cfg_path)
|
14 |
-
self.classes = []
|
15 |
-
with open(names_path, "r") as f:
|
16 |
-
self.classes = [line.strip() for line in f.readlines()]
|
17 |
-
self.layer_names = self.net.getLayerNames()
|
18 |
-
self.output_layers = [self.layer_names[i[0] - 1] for i in self.net.getUnconnectedOutLayers()]
|
19 |
-
|
20 |
-
def detect_objects(self, frame):
|
21 |
-
height, width, channels = frame.shape
|
22 |
-
blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
|
23 |
-
self.net.setInput(blob)
|
24 |
-
outs = self.net.forward(self.output_layers)
|
25 |
-
|
26 |
-
class_ids = []
|
27 |
-
confidences = []
|
28 |
-
boxes = []
|
29 |
-
for out in outs:
|
30 |
-
for detection in out:
|
31 |
-
scores = detection[5:]
|
32 |
-
class_id = np.argmax(scores)
|
33 |
-
confidence = scores[class_id]
|
34 |
-
if confidence > 0.5:
|
35 |
-
center_x = int(detection[0] * width)
|
36 |
-
center_y = int(detection[1] * height)
|
37 |
-
w = int(detection[2] * width)
|
38 |
-
h = int(detection[3] * height)
|
39 |
-
x = int(center_x - w / 2)
|
40 |
-
y = int(center_y - h / 2)
|
41 |
-
boxes.append([x, y, w, h])
|
42 |
-
confidences.append(float(confidence))
|
43 |
-
class_ids.append(class_id)
|
44 |
-
|
45 |
-
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
46 |
-
font = cv2.FONT_HERSHEY_PLAIN
|
47 |
-
for i in range(len(boxes)):
|
48 |
-
if i in indexes:
|
49 |
-
x, y, w, h = boxes[i]
|
50 |
-
label = str(self.classes[class_ids[i]])
|
51 |
-
color = (0, 255, 0)
|
52 |
-
cv2.rectangle(frame, (x, y), (x + w, y + h), color, 2)
|
53 |
-
cv2.putText(frame, label, (x, y + 30), font, 3, color, 2)
|
54 |
-
|
55 |
-
return frame
|
56 |
-
|
57 |
-
class JarvisModels:
|
58 |
-
def __init__(self):
|
59 |
-
self.client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
60 |
-
self.detector = YoloDetector("yolov3.weights", "yolov3.cfg", "coco.names")
|
61 |
-
|
62 |
-
async def generate_model1(self, prompt):
|
63 |
-
generate_kwargs = dict(
|
64 |
-
temperature=0.6,
|
65 |
-
max_new_tokens=256,
|
66 |
-
top_p=0.95,
|
67 |
-
repetition_penalty=1,
|
68 |
-
do_sample=True,
|
69 |
-
seed=42,
|
70 |
-
)
|
71 |
-
formatted_prompt = system_instructions1 + prompt + "[JARVIS]"
|
72 |
-
stream = self.client1.text_generation(
|
73 |
-
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
74 |
-
output = ""
|
75 |
-
for response in stream:
|
76 |
-
output += response.token.text
|
77 |
-
|
78 |
-
communicate = edge_tts.Communicate(output)
|
79 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
80 |
-
tmp_path = tmp_file.name
|
81 |
-
communicate.save(tmp_path)
|
82 |
-
return tmp_path
|
83 |
|
84 |
class FasterRCNNDetector:
|
85 |
def __init__(self):
|
@@ -119,16 +47,51 @@ class FasterRCNNDetector:
|
|
119 |
|
120 |
return image
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
def generate_response(frame):
|
123 |
jarvis = JarvisModels()
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
129 |
-
tmp_path = tmp_file.name
|
130 |
-
communicate.save(tmp_path)
|
131 |
-
return tmp_path
|
132 |
|
133 |
-
iface = gr.
|
134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
import gradio as gr
|
|
|
3 |
import tempfile
|
4 |
+
import torch
|
5 |
from torchvision.models.detection import fasterrcnn_resnet50_fpn
|
6 |
import torchvision.transforms as transforms
|
7 |
from PIL import Image
|
8 |
+
import deepspeech
|
9 |
+
import numpy as np
|
10 |
+
import soundfile as sf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
class FasterRCNNDetector:
|
13 |
def __init__(self):
|
|
|
47 |
|
48 |
return image
|
49 |
|
50 |
+
class JarvisModels:
|
51 |
+
def __init__(self):
|
52 |
+
self.client1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
53 |
+
self.model = deepspeech.Model("deepspeech-0.9.3-models.pbmm")
|
54 |
+
self.model.setBeamWidth(500)
|
55 |
+
|
56 |
+
async def generate_response(self, prompt):
|
57 |
+
generate_kwargs = dict(
|
58 |
+
temperature=0.6,
|
59 |
+
max_new_tokens=256,
|
60 |
+
top_p=0.95,
|
61 |
+
repetition_penalty=1,
|
62 |
+
do_sample=True,
|
63 |
+
seed=42,
|
64 |
+
)
|
65 |
+
formatted_prompt = system_instructions1 + prompt + "[JARVIS]"
|
66 |
+
stream = self.client1.text_generation(
|
67 |
+
formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
|
68 |
+
output = ""
|
69 |
+
for response in stream:
|
70 |
+
output += response.token.text
|
71 |
+
|
72 |
+
communicate = edge_tts.Communicate(output)
|
73 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
74 |
+
tmp_path = tmp_file.name
|
75 |
+
communicate.save(tmp_path)
|
76 |
+
return tmp_path
|
77 |
+
|
78 |
+
def transcribe_audio(audio_file):
|
79 |
+
model = JarvisModels().model
|
80 |
+
audio, sample_rate = sf.read(audio_file)
|
81 |
+
return model.stt(audio)
|
82 |
+
|
83 |
def generate_response(frame):
|
84 |
jarvis = JarvisModels()
|
85 |
+
response_model = await jarvis.generate_response("Hello, I see some interesting objects!")
|
86 |
+
return response_model
|
87 |
+
|
88 |
+
detector = FasterRCNNDetector()
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
iface = gr.Interface(
|
91 |
+
fn=[detector.detect_objects, transcribe_audio],
|
92 |
+
inputs=gr.inputs.Video(label="Webcam", parameters={"fps": 30}),
|
93 |
+
outputs=[gr.outputs.Image(), "text"],
|
94 |
+
title="Vision and Speech Interface",
|
95 |
+
description="This interface detects objects in the webcam feed and transcribes speech recorded through the microphone."
|
96 |
+
)
|
97 |
+
iface.launch()
|