import gradio as gr from openai import OpenAI import tempfile from pydub import AudioSegment # Hàm kiểm tra API key hợp lệ def check_api_key(api_key): try: client = OpenAI(api_key=api_key) client.models.list() # Kiểm tra kết nối API return True except Exception as e: print(f"Error: {e}") return False # Hàm điều chỉnh pitch def adjust_pitch(audio_path, pitch_factor=1.0): """ Điều chỉnh pitch của file âm thanh. :param audio_path: Đường dẫn đến file âm thanh. :param pitch_factor: Hệ số điều chỉnh pitch: - pitch_factor > 1.0: Tăng pitch (giọng cao hơn). - pitch_factor < 1.0: Giảm pitch (giọng trầm hơn). :return: Đường dẫn đến file âm thanh đã điều chỉnh. """ audio = AudioSegment.from_file(audio_path) new_frame_rate = int(audio.frame_rate * pitch_factor) pitched_audio = audio._spawn(audio.raw_data, overrides={ "frame_rate": new_frame_rate }).set_frame_rate(audio.frame_rate) pitched_audio.export(audio_path, format="mp3") return audio_path # Hàm TTS (Text to Speech) def tts(text, model, voice, speed, api_key, audio_file=None, pitch_factor=1.0): # Kiểm tra tính hợp lệ của API key if not api_key or api_key.strip() == "": raise gr.Error('Please enter your OpenAI API Key') if not check_api_key(api_key): raise gr.Error('Invalid OpenAI API Key. Please enter a valid API key.') try: client = OpenAI(api_key=api_key) # Nếu người dùng tải lên tệp âm thanh, sử dụng Whisper để chuyển thành văn bản if audio_file: audio_file = open(audio_file, 'rb') transcript = client.audio.transcriptions.create(model='whisper-1', file=audio_file, response_format='text') text = transcript # Lấy văn bản từ tệp âm thanh # Tạo yêu cầu TTS với tốc độ điều chỉnh response = client.audio.speech.create( model=model, voice=voice, input=text, speed=speed ) except Exception as error: print(str(error)) raise gr.Error("An error occurred while generating speech. Please check your API key and try again.") # Lưu âm thanh vào tệp tạm thời with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_file: temp_file.write(response.content) temp_file_path = temp_file.name # Điều chỉnh pitch nếu pitch_factor khác 1.0 if pitch_factor != 1.0: temp_file_path = adjust_pitch(temp_file_path, pitch_factor) return temp_file_path # Hàm giao diện Gradio def gradio_interface(): with gr.Blocks() as demo: gr.Markdown("#