-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytchat.py
93 lines (74 loc) · 3.22 KB
/
ytchat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
from pytube import YouTube
from moviepy.editor import AudioFileClip
from openai import OpenAI
from dotenv import load_dotenv
import os
import requests
load_dotenv('.env')
api_key = os.getenv('OPENAI_API_KEY')
client = OpenAI(api_key=api_key)
# Set the API key from an environment variable for security
# Function to download a YouTube video
def download_youtube_video(video_url):
try:
yt = YouTube(video_url)
video_stream = yt.streams.filter(file_extension='mp4').first()
if video_stream:
video_path = video_stream.download(filename = 'temp_vid.mp4') # This will use the default download path and filename
return video_path, None # No error
else:
return None, "No available video streams match the criteria."
except Exception as e:
return None, f"An error occurred: {e}"
# Function to convert MP4 to MP3
def convert_mp4_to_mp3(mp4_file_path, mp3_file_path):
try:
video_clip = AudioFileClip(mp4_file_path)
video_clip.write_audiofile(mp3_file_path)
video_clip.close()
return mp3_file_path, None # Indicate that there was no error
except Exception as e:
return None, f"An error occurred during conversion: {e}"
# Function to transcribe audio to text
def transcribe_audio_to_text(audio_file_path):
"""
Transcribe the given audio file to text using OpenAI's Whisper API.
:param audio_file_path: str - Path to the audio file to transcribe.
:return: Tuple (transcribed_text, error_message)
"""
headers = {
'Authorization': f'Bearer {api_key}'
}
try:
with open(audio_file_path, 'rb') as audio_file:
files = {
'file': audio_file,
'model': (None, 'whisper-1'),
# If you know the language, uncomment the next line
# 'language': (None, 'en') # Replace 'en' with the appropriate language code if needed
}
response = requests.post('https://api.openai.com/v1/audio/transcriptions', headers=headers, files=files)
if response.status_code == 200:
# Extract the transcription text
text = response.json()["text"]
return text, None
else:
# Return None and the error message
return None, f"An error occurred: {response.status_code} - {response.text}"
except Exception as e:
# Catch any exceptions and return None and the error message
return None, f"An exception occurred: {e}"
# Function to create chatbot context from transcribed text
def create_chatbot_context(transcribed_text):
return [{"role": "system", "content": transcribed_text}]
# Function to chat with the bot
def chat_with_bot(conversation, user_message):
try:
conversation.append({"role": "user", "content": user_message})
response = client.chat.completions.create(model="gpt-3.5-turbo", # replace with the correct model ID if different
messages=conversation)
assistant_message = response.choices[0].message.content
conversation.append({"role": "assistant", "content": assistant_message})
return assistant_message, None
except Exception as e:
return None, f"An error occurred: {e}"