2024-05-11 22:46:56 +03:00
import tkinter as tk
from tkinter import filedialog
import os
import shutil
import pygame
from moviepy . editor import VideoFileClip
import openai
2024-05-12 06:17:42 +03:00
from open_ai_voice import text_to_voice_openai
2024-05-11 22:46:56 +03:00
2024-05-12 06:17:42 +03:00
from Combined import combine_audio_video
tts = False
if tts :
from TTS . api import TTS
client = openai . OpenAI ( api_key = " sk-proj-8nFPKH1bvaw0KH93XKAfT3BlbkFJUW2IOhGnIf95oCK3VZXn " )
2024-05-11 22:46:56 +03:00
def contect_gather ( subject ) :
2024-05-12 06:17:42 +03:00
selected_language = language_var . get ( )
if selected_language == " en " :
response = client . chat . completions . create (
model = " gpt-3.5-turbo " ,
messages = [
{ " role " : " system " , " content " : " You are a helpful assistant. " } ,
{ " role " : " user " , " content " : f " Write a About 200 words paraghraph short story about the { subject } with a positive and adventurous tone. Avoid violence, hatred, and mature themes? " } ,
]
)
else :
response = client . chat . completions . create (
model = " gpt-3.5-turbo " ,
messages = [
{ " role " : " system " , " content " : " انت كاتب قصص. " } ,
{ " role " : " user " , " content " : f " اكتب قصة صغيره عن { subject } " } ,
]
)
print ( response . choices [ 0 ] . message . content )
return response . choices [ 0 ] . message . content
2024-05-12 17:52:39 +03:00
tts = True # Initialize tts as None initially
# Initialize TTS object if tts is True
2024-05-12 06:17:42 +03:00
if tts :
2024-05-12 17:52:39 +03:00
from TTS . api import TTS
2024-05-12 06:17:42 +03:00
tts = TTS ( " tts_models/multilingual/multi-dataset/xtts_v2 " , gpu = False )
2024-05-03 04:18:51 +03:00
2024-05-11 22:46:56 +03:00
# Function to convert text to voice
def text_to_voice ( text , speaker , language ) :
2024-05-12 06:17:42 +03:00
selected_ai = model_var . get ( )
if selected_ai == 1 :
text_to_voice_openai ( text )
2024-05-12 17:52:39 +03:00
elif tts : # Check if tts is initialized before calling tts_to_file
2024-05-12 06:17:42 +03:00
tts . tts_to_file ( text = text ,
file_path = " output.wav " ,
speaker_wav = f " sounds/ { speaker } " ,
language = language ,
split_sentences = True
)
play_audio ( " output.wav " )
2024-05-12 17:52:39 +03:00
else :
print ( " TTS is not initialized. Set ' tts ' to True and provide appropriate configuration. " )
2024-05-11 22:46:56 +03:00
# 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 :
2024-05-12 06:17:42 +03:00
save_path = " sounds/ "
2024-05-11 22:46:56 +03:00
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 ( ) :
2024-05-12 06:17:42 +03:00
sound_folder = " sounds/ "
2024-05-11 22:46:56 +03:00
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 )
2024-05-12 17:52:39 +03:00
combine_audio_video ( " output.wav " , " original_video.webm " , " combined_video.mp4 " )
2024-05-11 22:46:56 +03:00
# 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 " )
2024-05-12 06:17:42 +03:00
model_var = tk . IntVar ( )
model_var . set ( 1 ) # Default to OpenAI
openai_radio = tk . Radiobutton ( root , text = " OpenAI Model " , variable = model_var , value = 1 )
xtts_radio = tk . Radiobutton ( root , text = " xtts " , variable = model_var , value = 2 )
openai_radio . grid ( row = 3 , column = 0 , padx = 10 , pady = 5 , sticky = " w " )
xtts_radio . grid ( row = 3 , column = 1 , padx = 10 , pady = 5 , sticky = " w " )
2024-05-11 22:46:56 +03:00
# 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 )
2024-05-12 06:17:42 +03:00
2024-05-11 22:46:56 +03:00
# 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 ( ) :
2024-05-12 06:17:42 +03:00
file_path = " combined_video.mp4 "
2024-05-11 22:46:56 +03:00
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.
2024-05-12 06:17:42 +03:00
2024-05-11 22:46:56 +03:00
# Create save video button
2024-05-12 06:17:42 +03:00
save_button = tk . Button ( root , text = " Save Video " , command = combine_audio_video ( " output.wav " , " original_video.webm " , " combined_video.mp4 " ) )
2024-05-11 22:46:56 +03:00
save_button . grid ( row = 7 , column = 0 , columnspan = 2 , padx = 10 , pady = 10 , sticky = " ew " )
2024-05-12 17:52:39 +03:00
combine_audio_video ( " output.wav " , " original_video.webm " , " combined_video.mp4 " )
2024-05-11 22:46:56 +03:00
root . mainloop ( )