loganmann24 commited on
Commit
e82f66d
·
verified ·
1 Parent(s): 6252dc7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import cv2
3
+ import uuid
4
+ import gradio as gr
5
+ import numpy as np
6
+ import webbrowser
7
+ import webcamgpt
8
+ from gtts import gTTS
9
+ import speech_recognition as sr
10
+ from pydub import AudioSegment
11
+
12
+ MARKDOWN = """
13
+ # Webcam with GPT
14
+
15
+ Visual analysis of live webcam footage
16
+ """
17
+
18
+ connector = webcamgpt.OpanAIConnector()
19
+ duration_in_seconds=0
20
+
21
+ def save_image_to_drive(image: np.ndarray) -> str:
22
+ image_filename = f"{uuid.uuid4()}.jpeg"
23
+ image_directory = "data"
24
+ os.makedirs(image_directory, exist_ok=True)
25
+ image_path = os.path.join(image_directory, image_filename)
26
+ cv2.imwrite(image_path, image)
27
+ return image_path
28
+
29
+ def speech_to_text():
30
+ recognizer = sr.Recognizer()
31
+ with sr.Microphone() as source:
32
+ recognizer.adjust_for_ambient_noise(source)
33
+ print("Say something...")
34
+ audio = recognizer.listen(source, timeout=5)
35
+ try:
36
+ return recognizer.recognize_google(audio)
37
+ except sr.UnknownValueError:
38
+ return "Could not understand audio"
39
+ except sr.RequestError as e:
40
+ return f"Error with the speech recognition service; {e}"
41
+
42
+ def respond(image: np.ndarray, prompt: str, chat_history):
43
+ image = np.fliplr(image)
44
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
45
+ image_path = save_image_to_drive(image)
46
+
47
+ # Add speech-to-text for the prompt
48
+ speech_input = speech_to_text()
49
+ chat_history.append(((image_path,), None))
50
+ chat_history.append((speech_input, None))
51
+
52
+ response = connector.simple_prompt(image=image, prompt=speech_input)
53
+ chat_history.append((speech_input, response))
54
+
55
+ # Initialize gTTS with the text to convert
56
+ speech = gTTS(response, lang='en', slow=False)
57
+
58
+ # Save the audio file to a temporary file
59
+ speech_file = 'speech.mp3'
60
+ speech.save(speech_file)
61
+
62
+
63
+ audio = AudioSegment.from_file(speech_file)
64
+ global duration_in_seconds
65
+ duration_in_seconds = len(audio) / 1000
66
+ print(f"Speech duration: {duration_in_seconds} seconds")
67
+
68
+
69
+ # Play the audio file
70
+ webbrowser.open(speech_file)
71
+
72
+ return "", chat_history
73
+
74
+ with gr.Blocks() as demo:
75
+ gr.Markdown(MARKDOWN)
76
+ with gr.Row():
77
+ webcam = gr.Image(source="webcam", streaming=True)
78
+ with gr.Column():
79
+ chatbot = gr.Chatbot(height=500)
80
+ message = gr.Textbox(autofocus=True)
81
+ clear_button = gr.ClearButton([message, chatbot])
82
+
83
+ message.submit(respond, [webcam, message, chatbot], [message, chatbot])
84
+
85
+ demo.launch(debug=False, show_error=True, share=True)