Updated to Open AI SDK 1.1.1
Browse files
app.py
CHANGED
|
@@ -1,8 +1,3 @@
|
|
| 1 |
-
import subprocess
|
| 2 |
-
subprocess.run(["pip", "install", "openai"])
|
| 3 |
-
subprocess.run(["pip", "install", "soundfile"])
|
| 4 |
-
subprocess.run(["pip","install","pydub"])
|
| 5 |
-
|
| 6 |
import os
|
| 7 |
import openai
|
| 8 |
import time
|
|
@@ -11,8 +6,11 @@ import gradio as gr
|
|
| 11 |
import soundfile as sf
|
| 12 |
from pydub import AudioSegment
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
note_transcript = ""
|
| 18 |
|
|
@@ -64,7 +62,7 @@ def transcribe(audio, history_type):
|
|
| 64 |
attempt = 0
|
| 65 |
while attempt < max_attempts:
|
| 66 |
try:
|
| 67 |
-
audio_transcript =
|
| 68 |
break
|
| 69 |
except openai.error.APIConnectionError as e:
|
| 70 |
print(f"Attempt {attempt + 1} failed with error: {e}")
|
|
@@ -73,8 +71,8 @@ def transcribe(audio, history_type):
|
|
| 73 |
else:
|
| 74 |
print("Failed to transcribe audio after multiple attempts")
|
| 75 |
|
| 76 |
-
print(audio_transcript)
|
| 77 |
-
messages.append({"role": "user", "content": audio_transcript
|
| 78 |
|
| 79 |
#Create Sample Dialogue Transcript from File (for debugging)
|
| 80 |
#with open('Audio_Files/Test_Elbow.txt', 'r') as file:
|
|
@@ -87,15 +85,15 @@ def transcribe(audio, history_type):
|
|
| 87 |
mp3_megabytes = file_size / (1024 * 1024)
|
| 88 |
mp3_megabytes = round(mp3_megabytes, 2)
|
| 89 |
|
| 90 |
-
audio_transcript_words = audio_transcript
|
| 91 |
#audio_transcript_words = audio_transcript.split() #Use when using file
|
| 92 |
|
| 93 |
num_words = len(audio_transcript_words)
|
| 94 |
|
| 95 |
|
| 96 |
#Ask OpenAI to create note transcript
|
| 97 |
-
response =
|
| 98 |
-
note_transcript =
|
| 99 |
print(note_transcript)
|
| 100 |
return [note_transcript, num_words,mp3_megabytes]
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import openai
|
| 3 |
import time
|
|
|
|
| 6 |
import soundfile as sf
|
| 7 |
from pydub import AudioSegment
|
| 8 |
|
| 9 |
+
from openai import OpenAI
|
| 10 |
+
|
| 11 |
+
# Load API key from an environment variable
|
| 12 |
+
OPENAI_SECRET_KEY = os.environ.get("OPENAI_SECRET_KEY")
|
| 13 |
+
client = OpenAI(api_key = OPENAI_SECRET_KEY)
|
| 14 |
|
| 15 |
note_transcript = ""
|
| 16 |
|
|
|
|
| 62 |
attempt = 0
|
| 63 |
while attempt < max_attempts:
|
| 64 |
try:
|
| 65 |
+
audio_transcript = client.audio.transcriptions.create(model="whisper-1", file=audio_file)
|
| 66 |
break
|
| 67 |
except openai.error.APIConnectionError as e:
|
| 68 |
print(f"Attempt {attempt + 1} failed with error: {e}")
|
|
|
|
| 71 |
else:
|
| 72 |
print("Failed to transcribe audio after multiple attempts")
|
| 73 |
|
| 74 |
+
print(audio_transcript.text)
|
| 75 |
+
messages.append({"role": "user", "content": audio_transcript.text})
|
| 76 |
|
| 77 |
#Create Sample Dialogue Transcript from File (for debugging)
|
| 78 |
#with open('Audio_Files/Test_Elbow.txt', 'r') as file:
|
|
|
|
| 85 |
mp3_megabytes = file_size / (1024 * 1024)
|
| 86 |
mp3_megabytes = round(mp3_megabytes, 2)
|
| 87 |
|
| 88 |
+
audio_transcript_words = audio_transcript.text.split() # Use when using mic input
|
| 89 |
#audio_transcript_words = audio_transcript.split() #Use when using file
|
| 90 |
|
| 91 |
num_words = len(audio_transcript_words)
|
| 92 |
|
| 93 |
|
| 94 |
#Ask OpenAI to create note transcript
|
| 95 |
+
response = client.chat.completions.create(model="gpt-3.5-turbo-1106", temperature=0, messages=messages)
|
| 96 |
+
note_transcript = response.choices[0].message.content
|
| 97 |
print(note_transcript)
|
| 98 |
return [note_transcript, num_words,mp3_megabytes]
|
| 99 |
|