ai-content-maker/xtts.py

145 lines
5.5 KiB
Python

import tkinter as tk
from tkinter import filedialog
import os
import shutil
from TTS.api import TTS
import pygame
from moviepy.editor import VideoFileClip
import openai
OPENAI_API_KEY = "sk-proj-8nFPKH1bvaw0KH93XKAfT3BlbkFJUW2IOhGnIf95oCK3VZXn"
openai.api_key = OPENAI_API_KEY
def contect_gather(subject):
response = openai.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=f"Write a 2 paraghraph short story about the {subject} with a positive and adventurous tone. Avoid violence, hatred, and mature themes."
)
return response
# Initialize TTS object
tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=False)
# Function to convert text to voice
def text_to_voice(text, speaker, language):
tts.tts_to_file(text=text,
file_path="output.wav",
speaker_wav=f"sounds/{speaker}",
language=language,
split_sentences=True
)
play_audio("output.wav")
# Function to play audio using pygame
def play_audio(file_path):
pygame.mixer.init()
pygame.mixer.music.load(file_path)
pygame.mixer.music.play()
# Function to import voice
def import_voice():
file_path = filedialog.askopenfilename()
if file_path:
save_path = "C:/Users/hnk24/Documents/GitHub/ai-content-maker/sounds/"
os.makedirs(save_path, exist_ok=True)
save_path = os.path.join(save_path, os.path.basename(file_path))
shutil.copyfile(file_path, save_path)
print(f"File '{file_path}' imported and saved to '{save_path}'")
refresh_file_list()
# Function to refresh file list
def refresh_file_list():
sound_folder = "C:/Users/hnk24/Documents/GitHub/ai-content-maker/sounds/"
file_listbox.delete(0, tk.END) # Clear the listbox
for file_name in os.listdir(sound_folder):
file_listbox.insert(tk.END, file_name)
# Function to process data
def process_data():
selected_file = file_listbox.get(tk.ACTIVE) # Get the selected file name
selected_language = language_var.get() # Get the selected language
if var.get() == 1:
# Code to send data from single line textbox to API
single_line_data = single_line_textbox.get()
text_ai = contect_gather(single_line_data)
text_to_voice(text_ai, selected_file, selected_language)
print("Sending single line data, selected file, and selected language to API:", single_line_data, selected_file, selected_language)
elif var.get() == 2:
# Code to process data from multi-line textbox
multi_line_data = multi_line_textbox.get("1.0", tk.END)
text_to_voice(multi_line_data, selected_file, selected_language)
print("Processing multi-line data, selected file, and selected language:", multi_line_data, selected_file, selected_language)
# Function to save video
def save_video():
# Code to save video
pass
# Create the main window
root = tk.Tk()
root.title("Make a story")
root.geometry("800x800")
# Create radio buttons for textboxes
var = tk.IntVar()
single_line_radio = tk.Radiobutton(root, text="Single Line Textbox", variable=var, value=1)
multi_line_radio = tk.Radiobutton(root, text="Multi Line Textbox", variable=var, value=2)
single_line_radio.grid(row=0, column=0, padx=10, pady=10, sticky="w")
multi_line_radio.grid(row=1, column=0, padx=10, pady=10, sticky="w")
# Create radio buttons for language selection
language_var = tk.StringVar()
language_var.set("AR") # Default selection
ar_radio = tk.Radiobutton(root, text="AR", variable=language_var, value="ar")
en_radio = tk.Radiobutton(root, text="EN", variable=language_var, value="en")
ar_radio.grid(row=2, column=0, padx=10, pady=5, sticky="w")
en_radio.grid(row=2, column=1, padx=10, pady=5, sticky="w")
# Create textboxes
single_line_textbox = tk.Entry(root)
multi_line_textbox = tk.Text(root, height=5, width=30)
single_line_textbox.grid(row=0, column=1, padx=10, pady=10, sticky="ew")
multi_line_textbox.grid(row=1, column=1, padx=10, pady=10, sticky="ew")
# Create import button
import_button = tk.Button(root, text="Import Voice", command=import_voice)
import_button.grid(row=4, column=0, padx=10, pady=10, sticky="ew")
# Create listbox for file names
file_listbox = tk.Listbox(root)
file_listbox.grid(row=4, column=1, padx=10, pady=10, sticky="nsew")
refresh_file_list() # Refresh the file list initially
# Create process button
process_button = tk.Button(root, text="Process Data", command=process_data)
process_button.grid(row=5, column=0, columnspan=2, padx=10, pady=10, sticky="ew")
# Create canvas for video and audio player
video_canvas = tk.Canvas(root, bg="black", width=400, height=300)
video_canvas.grid(row=6, column=0, columnspan=2, padx=10, pady=10)
# Function to play video
def play_video(file_path):
clip = VideoFileClip(file_path)
clip.preview()
# Function to play audio or video based on the file type
def play_media(file_path):
if file_path.endswith(".mp3") or file_path.endswith(".wav"):
play_audio(file_path)
elif file_path.endswith(".mp4") or file_path.endswith(".avi"):
play_video(file_path)
# Function to display output file in the canvas and play media
def display_output():
file_path = "output.wav"
if os.path.exists(file_path):
play_media(file_path)
# Display output on canvas
# You need to implement this part using a suitable library like pygame for displaying images or videos on canvas.
# Create save video button
save_button = tk.Button(root, text="Save Video", command=save_video)
save_button.grid(row=7, column=0, columnspan=2, padx=10, pady=10, sticky="ew")
root.mainloop()