Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,22 +3,26 @@ from youtube_transcript_api import YouTubeTranscriptApi
|
|
| 3 |
import re
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
import tempfile
|
| 8 |
from pydub import AudioSegment
|
| 9 |
import io
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
load_dotenv()
|
| 13 |
-
groq_api_key = os.getenv('GROQ_API_KEY')
|
| 14 |
-
openai_api_key = os.getenv('OPENAI_API_KEY')
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
openai_client = OpenAI(api_key=openai_api_key)
|
| 19 |
|
| 20 |
-
if not
|
| 21 |
-
raise ValueError("API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
def get_transcript(url):
|
| 24 |
try:
|
|
@@ -33,13 +37,6 @@ def get_transcript(url):
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error: {str(e)}"
|
| 35 |
|
| 36 |
-
def convert_to_supported_format(file):
|
| 37 |
-
audio = AudioSegment.from_file(file)
|
| 38 |
-
buffer = io.BytesIO()
|
| 39 |
-
audio.export(buffer, format="mp3")
|
| 40 |
-
buffer.seek(0)
|
| 41 |
-
return buffer
|
| 42 |
-
|
| 43 |
def transcribe_audio(file):
|
| 44 |
file = convert_to_supported_format(file)
|
| 45 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
|
|
@@ -48,11 +45,8 @@ def transcribe_audio(file):
|
|
| 48 |
|
| 49 |
try:
|
| 50 |
with open(temp_file_path, "rb") as audio_file:
|
| 51 |
-
transcript =
|
| 52 |
-
|
| 53 |
-
file=audio_file
|
| 54 |
-
)
|
| 55 |
-
return transcript.text
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error in transcription: {str(e)}"
|
| 58 |
finally:
|
|
@@ -60,8 +54,8 @@ def transcribe_audio(file):
|
|
| 60 |
|
| 61 |
def answer_question(transcript, question):
|
| 62 |
try:
|
| 63 |
-
response =
|
| 64 |
-
model="
|
| 65 |
messages=[
|
| 66 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 67 |
{"role": "user", "content": f"Using the following transcript as context, please answer the question:\n\nTranscript:\n{transcript}\n\nQuestion:\n{question}"}
|
|
|
|
| 3 |
import re
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
import os
|
| 6 |
+
import openai
|
| 7 |
import tempfile
|
| 8 |
from pydub import AudioSegment
|
| 9 |
import io
|
| 10 |
|
| 11 |
+
# Load environment variables
|
| 12 |
load_dotenv()
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
# Set up OpenAI API key
|
| 15 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
|
|
|
| 16 |
|
| 17 |
+
if not openai.api_key:
|
| 18 |
+
raise ValueError("API key is not set. Please check your .env file and ensure OPENAI_API_KEY is set.")
|
| 19 |
+
|
| 20 |
+
def convert_to_supported_format(file):
|
| 21 |
+
audio = AudioSegment.from_file(file)
|
| 22 |
+
buffer = io.BytesIO()
|
| 23 |
+
audio.export(buffer, format="mp3")
|
| 24 |
+
buffer.seek(0)
|
| 25 |
+
return buffer
|
| 26 |
|
| 27 |
def get_transcript(url):
|
| 28 |
try:
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error: {str(e)}"
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def transcribe_audio(file):
|
| 41 |
file = convert_to_supported_format(file)
|
| 42 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
|
|
|
|
| 45 |
|
| 46 |
try:
|
| 47 |
with open(temp_file_path, "rb") as audio_file:
|
| 48 |
+
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 49 |
+
return transcript["text"]
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
return f"Error in transcription: {str(e)}"
|
| 52 |
finally:
|
|
|
|
| 54 |
|
| 55 |
def answer_question(transcript, question):
|
| 56 |
try:
|
| 57 |
+
response = openai.ChatCompletion.create(
|
| 58 |
+
model="gpt-3.5-turbo",
|
| 59 |
messages=[
|
| 60 |
{"role": "system", "content": "You are a helpful assistant."},
|
| 61 |
{"role": "user", "content": f"Using the following transcript as context, please answer the question:\n\nTranscript:\n{transcript}\n\nQuestion:\n{question}"}
|