Spaces:
Sleeping
Sleeping
Eric Z commited on
Commit ·
f007505
1
Parent(s): 0643685
fixes for local and api based streaming
Browse files- stream_app.py +57 -14
stream_app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import argparse
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from openai import OpenAI
|
| 5 |
import whisper
|
|
@@ -8,6 +9,18 @@ import io
|
|
| 8 |
import dotenv
|
| 9 |
dotenv.load_dotenv()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
whisper_model = None
|
| 12 |
|
| 13 |
|
|
@@ -18,11 +31,18 @@ def run_gradio(config:dict):
|
|
| 18 |
# transcription of audio
|
| 19 |
def audio_transcribe(input_audio_model:str, input_audio:str, input_text:str):
|
| 20 |
global whisper_model
|
|
|
|
|
|
|
| 21 |
if "offline" in input_audio_model.lower():
|
| 22 |
if whisper_model is None:
|
| 23 |
whisper_model = whisper.load_model("base")
|
| 24 |
audio = whisper.load_audio(input_audio)
|
| 25 |
result = whisper_model.transcribe(audio)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
elif "online" in input_audio_model.lower():
|
| 27 |
with open(input_audio, 'rb') as file_audio:
|
| 28 |
result = client.audio.translations.create(
|
|
@@ -32,8 +52,11 @@ def run_gradio(config:dict):
|
|
| 32 |
return ""
|
| 33 |
result = result.to_dict()
|
| 34 |
prompt = result["text"]
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# reset transcribed text
|
| 39 |
def audio_reset(input_text):
|
|
@@ -46,6 +69,7 @@ def run_gradio(config:dict):
|
|
| 46 |
if not prompt:
|
| 47 |
return "Please enter a prompt for interaction."
|
| 48 |
|
|
|
|
| 49 |
response = client.chat.completions.create(model=config['model'],
|
| 50 |
stream=True,
|
| 51 |
temperature=config['temperature'],
|
|
@@ -64,7 +88,7 @@ def run_gradio(config:dict):
|
|
| 64 |
partial_response += token
|
| 65 |
yield partial_response
|
| 66 |
|
| 67 |
-
with gr.Blocks() as demo:
|
| 68 |
gr.Markdown("""
|
| 69 |
# GPT-4 Gradio Demo
|
| 70 |
Enter your prompt below and see the AI-generated response.
|
|
@@ -91,8 +115,7 @@ def run_gradio(config:dict):
|
|
| 91 |
output_text = gr.Textbox(
|
| 92 |
label="Output",
|
| 93 |
interactive=False,
|
| 94 |
-
lines=
|
| 95 |
-
max_lines=10,
|
| 96 |
)
|
| 97 |
submit_button = gr.Button("Submit", variant='primary')
|
| 98 |
input_audio.stream(audio_transcribe,
|
|
@@ -100,27 +123,47 @@ def run_gradio(config:dict):
|
|
| 100 |
outputs=input_text)
|
| 101 |
input_audio.clear(audio_reset, inputs=input_text, outputs=input_text)
|
| 102 |
input_audio.start_recording(audio_reset, inputs=input_text, outputs=input_text)
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
submit_button.click(get_ai_response,
|
| 107 |
inputs=[input_text, input_audio],
|
| 108 |
outputs=output_text)
|
| 109 |
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
demo.queue()
|
| 112 |
demo.launch(share=False, debug=True, server_port=config["port"])
|
| 113 |
|
| 114 |
|
| 115 |
def parse_args() -> dict:
|
| 116 |
parser = argparse.ArgumentParser()
|
| 117 |
-
parser.
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
args = parser.parse_args()
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import argparse
|
| 3 |
+
import logging
|
| 4 |
import gradio as gr
|
| 5 |
from openai import OpenAI
|
| 6 |
import whisper
|
|
|
|
| 9 |
import dotenv
|
| 10 |
dotenv.load_dotenv()
|
| 11 |
|
| 12 |
+
|
| 13 |
+
# Set up logging
|
| 14 |
+
logging.basicConfig(
|
| 15 |
+
level=logging.INFO,
|
| 16 |
+
format="%(asctime)s [%(levelname)s] %(message)s",
|
| 17 |
+
handlers=[
|
| 18 |
+
logging.StreamHandler()
|
| 19 |
+
]
|
| 20 |
+
)
|
| 21 |
+
logger = logging.getLogger(__name__)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
whisper_model = None
|
| 25 |
|
| 26 |
|
|
|
|
| 31 |
# transcription of audio
|
| 32 |
def audio_transcribe(input_audio_model:str, input_audio:str, input_text:str):
|
| 33 |
global whisper_model
|
| 34 |
+
global logger
|
| 35 |
+
|
| 36 |
if "offline" in input_audio_model.lower():
|
| 37 |
if whisper_model is None:
|
| 38 |
whisper_model = whisper.load_model("base")
|
| 39 |
audio = whisper.load_audio(input_audio)
|
| 40 |
result = whisper_model.transcribe(audio)
|
| 41 |
+
result["no_speech_prob"] = 0
|
| 42 |
+
prob_scores = [x['no_speech_prob'] for x in result['segments']]
|
| 43 |
+
if len(prob_scores) > 0: # average the probs
|
| 44 |
+
result["no_speech_prob"] = sum(prob_scores)/len(prob_scores)
|
| 45 |
+
|
| 46 |
elif "online" in input_audio_model.lower():
|
| 47 |
with open(input_audio, 'rb') as file_audio:
|
| 48 |
result = client.audio.translations.create(
|
|
|
|
| 52 |
return ""
|
| 53 |
result = result.to_dict()
|
| 54 |
prompt = result["text"]
|
| 55 |
+
logger.warning(f"Transcription: {result}")
|
| 56 |
+
|
| 57 |
+
if result["no_speech_prob"] < (1 - config['speech_threshold']): # threshold to avoid bad output
|
| 58 |
+
return input_text + " " + prompt
|
| 59 |
+
return input_text
|
| 60 |
|
| 61 |
# reset transcribed text
|
| 62 |
def audio_reset(input_text):
|
|
|
|
| 69 |
if not prompt:
|
| 70 |
return "Please enter a prompt for interaction."
|
| 71 |
|
| 72 |
+
logger.warning(f"Prompt: {prompt}")
|
| 73 |
response = client.chat.completions.create(model=config['model'],
|
| 74 |
stream=True,
|
| 75 |
temperature=config['temperature'],
|
|
|
|
| 88 |
partial_response += token
|
| 89 |
yield partial_response
|
| 90 |
|
| 91 |
+
with gr.Blocks(css="footer{display:none !important}") as demo:
|
| 92 |
gr.Markdown("""
|
| 93 |
# GPT-4 Gradio Demo
|
| 94 |
Enter your prompt below and see the AI-generated response.
|
|
|
|
| 115 |
output_text = gr.Textbox(
|
| 116 |
label="Output",
|
| 117 |
interactive=False,
|
| 118 |
+
lines=10,
|
|
|
|
| 119 |
)
|
| 120 |
submit_button = gr.Button("Submit", variant='primary')
|
| 121 |
input_audio.stream(audio_transcribe,
|
|
|
|
| 123 |
outputs=input_text)
|
| 124 |
input_audio.clear(audio_reset, inputs=input_text, outputs=input_text)
|
| 125 |
input_audio.start_recording(audio_reset, inputs=input_text, outputs=input_text)
|
| 126 |
+
input_audio.stop_recording(get_ai_response,
|
| 127 |
+
inputs=[input_text, input_audio],
|
| 128 |
+
outputs=output_text)
|
| 129 |
submit_button.click(get_ai_response,
|
| 130 |
inputs=[input_text, input_audio],
|
| 131 |
outputs=output_text)
|
| 132 |
|
| 133 |
+
# demo.set_api_mode(enabled=False) # Disable API exposure
|
| 134 |
+
# demo.set_footer(enabled=False) # Disable Gradio footers
|
| 135 |
+
|
| 136 |
demo.queue()
|
| 137 |
demo.launch(share=False, debug=True, server_port=config["port"])
|
| 138 |
|
| 139 |
|
| 140 |
def parse_args() -> dict:
|
| 141 |
parser = argparse.ArgumentParser()
|
| 142 |
+
opt_group = parser.add_argument_group("Model Configuration")
|
| 143 |
+
opt_group.add_argument("--model", type=str, default="gpt-4o",
|
| 144 |
+
help="Model to use for chat completion.")
|
| 145 |
+
opt_group.add_argument("--temperature", type=float, default=1.0,
|
| 146 |
+
help="Temperature for chat completion. ")
|
| 147 |
+
opt_group.add_argument("--max_tokens", type=int, default=2000,
|
| 148 |
+
help="Maximum number of tokens to generate in chat completion.")
|
| 149 |
|
| 150 |
+
opt_group = parser.add_argument_group("Speech Processing")
|
| 151 |
+
opt_group.add_argument("--speech_threshold", type=float, default=0.5,
|
| 152 |
+
help="Speech threshold for recognition to add text to a prompt. ")
|
| 153 |
+
|
| 154 |
+
opt_group = parser.add_argument_group("App Settings")
|
| 155 |
+
opt_group.add_argument("--port", type=int, default=7860,
|
| 156 |
+
help="Port to run Gradio server on.")
|
| 157 |
+
opt_group.add_argument("--log_file", type=str,
|
| 158 |
+
help="Path to log file to write to. Empty will prevent any logging.")
|
| 159 |
+
|
| 160 |
args = parser.parse_args()
|
| 161 |
+
dict_vars = vars(args)
|
| 162 |
+
if dict_vars['log_file']: # create new logger to output
|
| 163 |
+
logger.addHandler(
|
| 164 |
+
logging.FileHandler(dict_vars['log_file']),
|
| 165 |
+
)
|
| 166 |
+
return dict_vars
|
| 167 |
|
| 168 |
|
| 169 |
if __name__ == "__main__":
|