forked from mrspiggot/prompts_and_completions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpt3_ft.py
61 lines (55 loc) · 1.78 KB
/
gpt3_ft.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
import youtube_dl
import openai
import os
# Set up OpenAI credentials
openai.api_key = "YOUR_API_KEY"
# Define function to transcribe audio using OpenAI
def transcribe_audio(audio_file):
response = openai.Completion.create(
engine="davinci",
prompt="Transcribe the following audio:\n" + audio_file,
max_tokens=2048,
n=1,
stop=None,
temperature=0.5,
)
return response.choices[0].text.strip()
# Download YouTube video and extract audio using youtube_dl
url = "https://www.youtube.com/watch?v=YOUR_VIDEO_ID"
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': '%(id)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
# Transcribe audio and chop up text into prompts and completions
audio_file = "YOUR_VIDEO_ID.wav"
transcribed_text = transcribe_audio(audio_file)
prompt_length = 50
completion_length = 20
prompts = [transcribed_text[i:i+prompt_length] for i in range(0, len(transcribed_text), prompt_length)]
completions = [transcribed_text[i:i+completion_length] for i in range(prompt_length, len(transcribed_text), completion_length)]
# Fine-tune GPT-3 using prompts and completions
model_engine = "davinci"
model_name = "YOUR_MODEL_NAME"
model_prompt = "\n".join(prompts)
model_completion = "\n".join(completions)
fine_tuned_model = openai.FineTune.create(
model=model_name,
prompt=model_prompt,
examples=[{"text": model_completion}],
temperature=0.7,
max_tokens=1024,
n_epochs=5,
batch_size=4,
learning_rate=1e-5,
labels=["transcription"],
create= True
)
# Print the fine-tuned model's ID
print(f"Fine-tuned model ID: {fine_tuned_model.id}")