import openai # import library openAI/ChatGPT
import gradio as gr # import library Gradio App
from gtts import gTTS # import libary agar konversi teks menjadi suara
import os
import pygame # import untuk memainkan audio
kode API Chatgpt
openai.api_key = “”
Inisialisasi Pesan
messages = [{“role”: “system”, “content”: “Hello, Welcome to ALICE, How Can I Help You?”}]
Inisialisasi pygame
pygame.init()
Inputan pertanyaan dari user
def api_chatgpt(Masukan):
messages.append({“role”: “user”, “content”: Masukan})
response = openai.ChatCompletion.create(
model=“gpt-3.5-turbo”,
messages=messages
)
jawab_bot = response[“choices”][0][“message”][“content”]
messages.append({“role”: “assistant”, “content”: jawab_bot})
# Konversi teks menjadi suara menggunakan gTTS
tts = gTTS(text=jawab_bot, lang="id")
audio_file = "hasil_Suara.mp3"
tts.save(audio_file)
# Memutarkan suara
os.system("start " + audio_file) # Ini akan memainkan suara menggunakan aplikasi default untuk file audio di Windows
return jawab_bot, audio_file # Mengembalikan teks dan file audio bersamaan
Inisialisasi Gradio APP sebagai Interface
hasil_output = gr.Interface(fn=api_chatgpt, inputs=“text”, outputs=[“text”, “audio”], title=“ALICE”)
Ubah teks “outputs” menjadi “audio” untuk menampilkan audio
Fungsi untuk memainkan audio
def play_audio(audio_file):
pygame.mixer.music.load(audio_file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
Setel output agar memainkan audio
hasil_output.out = play_audio
Jalankan aplikasi Gradio
hasil_output.launch()
This is my code, I use Gradio and Python which can produce sound and text output.
Last month the API key still worked, when this month it was November 8th and I tried it the result was an error. then I got a notification to update the OpenAi version and was told v1.0.0 Migration.
I think my API key can still be used. because, I haven’t created the API key for a long time in this new account.
I recently checked the OpenAI usage on my account and it turns out it has expired, like this:
is this the cause? or have to migrate to that version?
by the way, i from indonesia