'전체 글'에 해당되는 글 74건

  1. 2024.04.05 소라군2
  2. 2024.04.05 소라군
  3. 2023.11.23 babel fish 1
2024. 4. 5. 15:15

잘 잡니다... 쿨쿨

'Etc' 카테고리의 다른 글

소라군  (0) 2024.04.05
Posted by 다만사
2024. 4. 5. 15:12

훌륭한 가족 소라군

'Etc' 카테고리의 다른 글

소라군2  (0) 2024.04.05
Posted by 다만사
2023. 11. 23. 00:52

# babel_fish.py
import tkinter as tk
from tkinter import ttk
import openai
import sounddevice as sd
from gtts import gTTS
import pygame
import wavio
from openai import OpenAI
import os

API_KEY = os.environ.get('OPEN_API_KEY')
client = OpenAI(
api_key = API_KEY,
)

# create a dictionary to store the language and
# their corresponding codes
languages = {'English': 'en', 'Spanish': 'es', 'French': 'fr', 'Korean': 'ko', 'Japanese': 'ja'}

app = tk.Tk()
app.title("Babel Fish")
style = ttk.Style()

def set_wait_cursor():
    submit_btn.config(cursor="watch")
    app.update_idletasks()  # Force an immediate update of the window


def set_normal_cursor():
    submit_btn.config(cursor="")

def translate(language1, language2, text):
    set_label("Translating...")
    prompt = f"Translate the following from {language1} to {language2}: {text}"
    # prompt = 'Hi There'
    messages = [{'role': 'user', 'content': prompt}]
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0.8,
        top_p=1.0,
        frequency_penalty=0.0,
        presence_penalty=0.6,
    )

    chat_gpt_translation = response.choices[0].message.content
    print('translation: ' + chat_gpt_translation)
    return chat_gpt_translation


def text_to_speech(translated_text, language):
    set_label("Playing...")
    tts = gTTS(translated_text, lang=languages[language], slow=False)
    tts.save('C:\\temp\\translation.mp3')
    # 5. Convert Translated Text to Speech
    # Placeholder for a TTS service (like Google Cloud TTS).

    # Initialize pygame mixer
    pygame.mixer.init()
    pygame.mixer.music.load('C:\\temp\\translation.mp3')
    pygame.mixer.music.play()


# If you want to keep the program running until the audio is done playing:
    while pygame.mixer.music.get_busy():
        pygame.time.Clock().tick(10)  # This will wait and let the music play.

    # close the mp3 file
    pygame.mixer.music.stop()
    pygame.mixer.quit()
    os.remove('C:\\temp\\translation.mp3')


def capture_audio():
     # Indicate start of recording
    label_recording.config(text="Recording...", bg="red")

    app.update()
    # get number of seconds from dropdown
    duration = int(combo_duration.get())
    samplerate = 44100
    audio = sd.rec(int(samplerate * duration),
                   samplerate=samplerate, channels=2, dtype='int16')
    sd.wait()

    label_recording.config(text="Finished Recording", bg="green")

    app.update()

    # Save the numpy array to a WAV file using wavio
    wav_path = "c:\\temp\\myrecording.wav"
    wavio.write(wav_path, audio, samplerate, sampwidth=2)


def set_label(text):
    label_recording.config(text=text, bg="green", fg="white")
    label_recording.update()


def transcribe():
    audio_file = open("c:\\temp\\myrecording.wav", "rb")
    set_label("Transcribing...")
    transcription = client.audio.transcriptions.create(
     model='whisper-1', file=audio_file, response_format='text')
    audio_file.close()
    print('transcription: ' + f'{transcription}\n\n')
    return transcription

def reset_status():
    label_recording.config(text="Click the button to start recording",
                           bg="lightgray", fg="white")
    label_recording.update()


def submit():

set_wait_cursor()

# 1. Capture Audio
capture_audio()

# 2. Transcibe the Audio
transcription = transcribe()

# 3. Translate the Transcribed Audio
resulting_traslation = translate(combo1.get(), combo2.get(), transcription)

# 4, Voice the Translated Text
text_to_speech(resulting_traslation, combo2.get())
reset_status()
set_normal_cursor()

# Label and ComboBox for the Known Language
label1 = ttk.Label(app, text="Select Known Language")
label1.grid(column=0, row=0, padx=10, pady=5)
combo1 = ttk.Combobox(app, values=list(languages.keys()))
combo1.grid(column=1, row=0, padx=10, pady=5)
combo1.set("English")

# Label and ComboBox for the Translated Language
label2 = ttk.Label(app, text="Select Traslated Language")
label2.grid(column=0, row=1, padx=10, pady=5)
combo2 = ttk.Combobox(app, values=list(languages.keys()))
combo2.grid(column=1, row=1, padx=10, pady=5)
combo2.set("Spanish")

label_recording_duration = tk.Label(app, text="Recoding Duration(seconds):")
label_recording_duration.grid(column=0, row=2, padx=10, pady=5)
combo_duration = ttk.Combobox(app, values=[5, 10, 15, 20, 25, 30])
combo_duration.grid(column=1, row=2, padx=10, pady=5)
combo_duration.set(5)

# Button to submit the text to traslate
submit_btn = ttk.Button(app, text="Record", command=submit)
submit_btn.grid(column=1, row=3, padx=10, pady=20)
label_recording = tk.Label(app, text="Click the button to start recording",
bg="lightgray", fg="white", width=60, height=2)
label_recording.grid(column=0, columnspan=2, row=4, padx=10, pady=20)

app.mainloop()



'python' 카테고리의 다른 글

pdf 출력  (0) 2023.11.22
사자와 코끼리가 휴대폰 대리점에 나누는 대화  (2) 2023.11.22
챗gpt 전체소스  (1) 2023.11.22
dall-e-3 image 생성 - 한국어로 변경  (1) 2023.11.22
dall-e-3를 활용한 이미지 생성  (0) 2023.11.22
Posted by 다만사