Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,98 @@
|
|
| 1 |
|
| 2 |
-
|
| 3 |
-
|
| 4 |
'''
|
| 5 |
This script calls the ada model from openai api to predict the next few words.
|
| 6 |
'''
|
| 7 |
import os
|
| 8 |
-
import openai
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
| 2 |
'''
|
| 3 |
This script calls the ada model from openai api to predict the next few words.
|
| 4 |
'''
|
| 5 |
import os
|
| 6 |
+
#import openai
|
| 7 |
+
import os
|
| 8 |
+
from pprint import pprint
|
| 9 |
+
os.system("pip install git+https://github.com/openai/whisper.git")
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import whisper
|
| 12 |
+
from transformers import pipeline
|
| 13 |
+
import torch
|
| 14 |
+
from transformers import AutoModelForCausalLM
|
| 15 |
+
from transformers import AutoTokenizer
|
| 16 |
+
import time
|
| 17 |
+
# import streaming.py
|
| 18 |
+
# from next_word_prediction import GPT2
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
#gpt2 = AutoModelForCausalLM.from_pretrained("gpt2", return_dict_in_generate=True)
|
| 24 |
+
#tokenizer = AutoTokenizer.from_pretrained("gpt2")
|
| 25 |
+
|
| 26 |
+
### /code snippet
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# get gpt2 model
|
| 30 |
+
generator = pipeline('text-generation', model='gpt2')
|
| 31 |
+
|
| 32 |
+
# whisper model specification
|
| 33 |
+
model = whisper.load_model("tiny")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def inference(audio, state=""):
|
| 38 |
+
|
| 39 |
+
#time.sleep(2)
|
| 40 |
+
#text = p(audio)["text"]
|
| 41 |
+
#state += text + " "
|
| 42 |
+
# load audio data
|
| 43 |
+
audio = whisper.load_audio(audio)
|
| 44 |
+
# ensure sample is in correct format for inference
|
| 45 |
+
audio = whisper.pad_or_trim(audio)
|
| 46 |
+
|
| 47 |
+
# generate a log-mel spetrogram of the audio data
|
| 48 |
+
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
| 49 |
+
|
| 50 |
+
_, probs = model.detect_language(mel)
|
| 51 |
+
|
| 52 |
+
# decode audio data
|
| 53 |
+
options = whisper.DecodingOptions(fp16 = False)
|
| 54 |
+
# transcribe speech to text
|
| 55 |
+
result = whisper.decode(model, mel, options)
|
| 56 |
+
|
| 57 |
+
PROMPT = """The following is a transcript of a conversation. Predict a few nouns, verbs, or adjectives that may be used next. Predict the next few words as a list of options.
|
| 58 |
+
A few examples are provided below and then the current transcript is provided.
|
| 59 |
+
Examples:
|
| 60 |
+
Transcript: Tomorrow night we're going out to
|
| 61 |
+
Next: The Movies, A Restaurant, A Baseball Game, The Theater, A Party
|
| 62 |
+
Transcript: I would like to order a cheeseburger with a side of
|
| 63 |
+
Next: Fries, Milkshake, Apples, Salad, Katsup
|
| 64 |
+
"""
|
| 65 |
+
text = PROMPT + result.text
|
| 66 |
+
|
| 67 |
+
openai.api_key = os.environ["Openai_APIkey"]
|
| 68 |
+
|
| 69 |
+
response = openai.Completion.create(
|
| 70 |
+
model="text-ada-001",
|
| 71 |
+
prompt=text,
|
| 72 |
+
temperature=1,
|
| 73 |
+
max_tokens=4,
|
| 74 |
+
n=4)
|
| 75 |
+
for i in range(4):
|
| 76 |
+
print(response['choices'][i]['text'])
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# result.text
|
| 80 |
+
#return getText, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
|
| 81 |
+
return result.text, state, response
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# get audio from microphone
|
| 86 |
+
|
| 87 |
+
gr.Interface(
|
| 88 |
+
fn=inference,
|
| 89 |
+
inputs=[
|
| 90 |
+
gr.inputs.Audio(source="microphone", type="filepath"),
|
| 91 |
+
"state"
|
| 92 |
+
],
|
| 93 |
+
outputs=[
|
| 94 |
+
"textbox",
|
| 95 |
+
"state",
|
| 96 |
+
"textbox"
|
| 97 |
+
],
|
| 98 |
+
live=True).launch()
|