Skip to content

Commit

Permalink
fixing video chatbot
Browse files Browse the repository at this point in the history
  • Loading branch information
RayVentura committed Jul 28, 2023
1 parent 9977914 commit 170933a
Showing 1 changed file with 43 additions and 43 deletions.
86 changes: 43 additions & 43 deletions gui/video_automation_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from shortGPT.gpt import gpt_chat_video


class ChatState(Enum):
class Chatstate(Enum):
ASK_ORIENTATION = 1
ASK_VOICE_MODULE = 2
ASK_LANGUAGE = 3
Expand All @@ -29,7 +29,7 @@ class ChatState(Enum):
class VideoAutomationUI:
def __init__(self, shortGptUI: gr.Blocks):
self.shortGptUI = shortGptUI
self.state = ChatState.ASK_ORIENTATION
self.state = Chatstate.ASK_ORIENTATION
self.isVertical = None
self.voice_module = None
self.language = None
Expand All @@ -49,12 +49,8 @@ def is_key_missing(self):
if not openai_key:
return "Your OpenAI key is missing. Please go to the config tab and enter the API key."

eleven_labs_key = ApiKeyManager.get_api_key("ELEVEN LABS")
if not eleven_labs_key:
return "Your Eleven Labs API key is missing. Please go to the config tab and enter the API key."

eleven_labs_key = ApiKeyManager.get_api_key("PEXELS")
if not eleven_labs_key:
pexels_api_key = ApiKeyManager.get_api_key("PEXELS")
if not pexels_api_key:
return "Your Pexels API key is missing. Please go to the config tab and enter the API key."

def generate_script(self, message, language):
Expand Down Expand Up @@ -83,55 +79,59 @@ def reset_components(self):

def chatbot_conversation(self):
def respond(message, chat_history, progress=gr.Progress()):
global state, isVertical, voice_module, language, script, videoVisible, video_html
#global self.state, isVertical, voice_module, language, script, videoVisible, video_html
error_html = ""
errorVisible = False
inputVisible = True
folderVisible = False
if state == ChatState.ASK_ORIENTATION:
if self.state == Chatstate.ASK_ORIENTATION:
errorMessage = self.is_key_missing()
if errorMessage:
bot_message = errorMessage
else:
isVertical = "vertical" in message.lower() or "short" in message.lower()
state = ChatState.ASK_VOICE_MODULE
self.isVertical = "vertical" in message.lower() or "short" in message.lower()
self.state = Chatstate.ASK_VOICE_MODULE
bot_message = "Which voice module do you want to use? Please type 'ElevenLabs' for high quality voice or 'EdgeTTS' for free but medium quality voice."
elif state == ChatState.ASK_VOICE_MODULE:
elif self.state == Chatstate.ASK_VOICE_MODULE:
if "elevenlabs" in message.lower():
voice_module = ElevenLabsVoiceModule
eleven_labs_key = ApiKeyManager.get_api_key("ELEVEN LABS")
if not eleven_labs_key:
bot_message = "Your Eleven Labs API key is missing. Please go to the config tab and enter the API key."
return
self.voice_module = ElevenLabsVoiceModule
language_choices = [lang.value for lang in ELEVEN_SUPPORTED_LANGUAGES]
elif "edgetts" in message.lower():
voice_module = EdgeTTSVoiceModule
self.voice_module = EdgeTTSVoiceModule
language_choices = [lang.value for lang in Language]
else:
bot_message = "Invalid voice module. Please type 'ElevenLabs' or 'EdgeTTS'."
return
state = ChatState.ASK_LANGUAGE
self.state = Chatstate.ASK_LANGUAGE
bot_message = f"🌐What language will be used in the video?🌐 Choose from one of these ({', '.join(language_choices)})"
elif state == ChatState.ASK_LANGUAGE:
language = next((lang for lang in Language if lang.value.lower() in message.lower()), None)
language = language if language else Language.ENGLISH
if voice_module == ElevenLabsVoiceModule:
voice_module = ElevenLabsVoiceModule(ApiKeyManager.get_api_key('ELEVEN LABS'), "Antoni", checkElevenCredits=True)
elif voice_module == EdgeTTSVoiceModule:
voice_module = EdgeTTSVoiceModule(EDGE_TTS_VOICENAME_MAPPING[language]['male'])
state = ChatState.ASK_DESCRIPTION
elif self.state == Chatstate.ASK_LANGUAGE:
self.language = next((lang for lang in Language if lang.value.lower() in message.lower()), None)
self.language = self.language if self.language else Language.ENGLISH
if self.voice_module == ElevenLabsVoiceModule:
self.voice_module = ElevenLabsVoiceModule(ApiKeyManager.get_api_key('ELEVEN LABS'), "Antoni", checkElevenCredits=True)
elif self.voice_module == EdgeTTSVoiceModule:
self.voice_module = EdgeTTSVoiceModule(EDGE_TTS_VOICENAME_MAPPING[self.language]['male'])
self.state = Chatstate.ASK_DESCRIPTION
bot_message = "Amazing 🔥 ! 📝Can you describe thoroughly the subject of your video?📝 I will next generate you a script based on that description"
elif state == ChatState.ASK_DESCRIPTION:
script = self.generate_script(message, language.value)
state = ChatState.ASK_SATISFACTION
bot_message = f"📝 Here is your generated script: \n\n--------------\n{script}\n\n・Are you satisfied with the script and ready to proceed with creating the video? Please respond with 'YES' or 'NO'. 👍👎"
elif state == ChatState.ASK_SATISFACTION:
elif self.state == Chatstate.ASK_DESCRIPTION:
self.script = self.generate_script(message, self.language.value)
self.state = Chatstate.ASK_SATISFACTION
bot_message = f"📝 Here is your generated script: \n\n--------------\n{self.script}\n\n・Are you satisfied with the script and ready to proceed with creating the video? Please respond with 'YES' or 'NO'. 👍👎"
elif self.state == Chatstate.ASK_SATISFACTION:
if "yes" in message.lower():
state = ChatState.MAKE_VIDEO
self.state = Chatstate.MAKE_VIDEO
inputVisible = False
yield gr.update(visible=False), gr.Chatbot.update(value=[[None, "Your video is being made now! 🎬"]]), gr.HTML.update(value="", visible=False), gr.HTML.update(value=error_html, visible=errorVisible), gr.update(visible=folderVisible), gr.update(visible=False)
try:
video_path = self.make_video(script, voice_module, isVertical, progress=progress)
video_path = self.make_video(self.script, self.voice_module, self.isVertical, progress=progress)
file_name = video_path.split("/")[-1].split("\\")[-1]
current_url = shortGptUI.share_url+"/" if shortGptUI.share else shortGptUI.local_url
current_url = self.shortGptUI.share_url+"/" if self.shortGptUI.share else self.shortGptUI.local_url
file_url_path = f"{current_url}file={video_path}"
video_html = f'''
self.video_html = f'''
<div style="display: flex; flex-direction: column; align-items: center;">
<video width="{600}" height="{300}" style="max-height: 100%;" controls>
<source src="{file_url_path}" type="video/mp4">
Expand All @@ -141,34 +141,34 @@ def respond(message, chat_history, progress=gr.Progress()):
<button style="font-size: 1em; padding: 10px; border: none; cursor: pointer; color: white; background: #007bff;">Download Video</button>
</a>
</div>'''
videoVisible = True
self.videoVisible = True
folderVisible = True
bot_message = "Your video is completed !🎬. Scroll down below to open its file location."
except Exception as e:
traceback_str = ''.join(traceback.format_tb(e.__traceback__))
error_name = type(e).__name__.capitalize() + " : " + f"{e.args[0]}"
errorVisible = True
gradio_content_automation_ui_error_template = GradioComponentsHTML.ERROR_TEMPLATE
gradio_content_automation_ui_error_template = GradioComponentsHTML.get_html_error_template()
error_html = gradio_content_automation_ui_error_template.format(error_message=error_name, stack_trace=traceback_str)
bot_message = "We encountered an error while making this video ❌"
print("Error", traceback_str)
yield gr.update(visible=False), gr.Chatbot.update(value=[[None, "Your video is being made now! 🎬"]]), gr.HTML.update(value="", visible=False),
gr.HTML.update(value=error_html, visible=errorVisible), gr.update(visible=folderVisible), gr.update(visible=True)

else:
state = ChatState.ASK_CORRECTION # change state to ASK_CORRECTION
self.state = Chatstate.ASK_CORRECTION # change self.state to ASK_CORRECTION
bot_message = "Explain me what you want different in the script"
elif state == ChatState.ASK_CORRECTION: # new state
script = self.correct_script(script, message) # call generateScript with correct=True
state = ChatState.ASK_SATISFACTION
bot_message = f"📝 Here is your corrected script: \n\n--------------\n{script}\n\n・Are you satisfied with the script and ready to proceed with creating the video? Please respond with 'YES' or 'NO'. 👍👎"
elif self.state == Chatstate.ASK_CORRECTION: # new self.state
self.script = self.correct_script(self.script, message) # call generateScript with correct=True
self.state = Chatstate.ASK_SATISFACTION
bot_message = f"📝 Here is your corrected script: \n\n--------------\n{self.script}\n\n・Are you satisfied with the script and ready to proceed with creating the video? Please respond with 'YES' or 'NO'. 👍👎"
chat_history.append((message, bot_message))
yield gr.update(value="", visible=inputVisible), gr.Chatbot.update(value=chat_history), gr.HTML.update(value=video_html, visible=videoVisible), gr.HTML.update(value=error_html, visible=errorVisible), gr.update(visible=folderVisible), gr.update(visible=True)
yield gr.update(value="", visible=inputVisible), gr.Chatbot.update(value=chat_history), gr.HTML.update(value=self.video_html, visible=self.videoVisible), gr.HTML.update(value=error_html, visible=errorVisible), gr.update(visible=folderVisible), gr.update(visible=True)

return respond

def initialize_conversation(self):
self.state = ChatState.ASK_ORIENTATION
self.state = Chatstate.ASK_ORIENTATION
self.isVertical = None
self.language = None
self.script = ""
Expand All @@ -177,7 +177,7 @@ def initialize_conversation(self):
return [[None, "🤖 Welcome to ShortGPT! 🚀 I'm a python framework aiming to simplify and automate your video editing tasks.\nLet's get started! 🎥🎬\n\n Do you want your video to be in landscape or vertical format? (landscape OR vertical)"]]

def reset_conversation(self):
self.state = ChatState.ASK_ORIENTATION
self.state = Chatstate.ASK_ORIENTATION
self.isVertical = None
self.language = None
self.script = ""
Expand Down

0 comments on commit 170933a

Please sign in to comment.