# importing the required libraries and classes import os import gradio as gr from dotenv import load_dotenv from langchain_openai import OpenAI from langchain.prompts import PromptTemplate from pytube import YouTube from youtube_transcript_api import YouTubeTranscriptApi from langchain.chains import LLMChain from langchain_openai import ChatOpenAI # Load environment variables from .env file load_dotenv() # Access the API key openai_api_key = os.getenv("OPENAI_API_KEY") def get_transcript(youtube_url): """ This Function will return the transcript of the given YouTube URL """ try: # Extract the video ID from the YouTube URL video_id = youtube_url.split("?v=")[1] # Get the transcript transcript = YouTubeTranscriptApi.get_transcript(video_id) transcript_text = " ".join([entry["text"] for entry in transcript]) return transcript_text # returning -1, if the URL is not valid except: return -1 def get_title(youtube_url): """ This function will return the Title of the given YouTube URL """ try: # getting the title of the Youtube video yt_video = YouTube(youtube_url) return yt_video.title # returning -1, if the URL is not valid except: return -1 def get_result(url,content): """ This function will return the content(type of content provided by the user)""" # getting the title and transcript of the Youtube URL title,transcript = get_title(url) , get_transcript(url) # if the URL is not valid if title == -1 and transcript == -1: return f'The provided url: "{url}" is not valid. Please provide a valid URL.' # this template is for the Article or Stories basic_template = "Imagine you're a content creator. Your task is to create {user_input} by leveraging the provided title and transcript creatively. Craft a compelling {user_input} that engages the audience and leverages the information from the video in an about 800-1000 words. Your output should be creative. Specifics: The YouTube video is titled '{title}', and you have access to its transcript: {transcript}" # this template is for the Short Video Script reel_template = "Imagine you're a content writer. Your task is to produce a concise and engaging Short Video Script. You can leverage the provided video title and transcript creatively. Create a concise and powerful script for a 1-minute short video, tailored for the user's use in crafting their own video. Specifics: The YouTube video is titled '{title}', and you have access to its transcript: {transcript}" if content == 'Shorts/Reel Script': prompt = PromptTemplate( template = reel_template, input_variables = ['title','transcript','user_input'] ) else: prompt = PromptTemplate( template = basic_template, input_variables=['title','transcript','user_input'] ) chain = LLMChain( prompt = prompt, llm = ChatOpenAI(temperature=0.7, model = 'gpt-3.5-turbo-16k') ) # invoking the chain and return the output i.e. content try: return chain.invoke({'title':title,'transcript':transcript,'user_input':content})['text'] except Exception as e: return f'Error: {e}' if __name__ == "__main__": # name of the WebApp title="Creative Content Generator" # About the WebApp description='An AI platform capable of generating creative content such as articles, stories, short reel script for content creators.\n\n How does it works?\n\n Provide one YouTube link and type, type could be articles, stories, and script for short video and wait for the result' webapp = gr.Interface( fn = get_result, inputs=[gr.Textbox(placeholder="Provide the URL here...."),gr.Dropdown(["Stories",'Article','Shorts/Reel Script'],label="Choose the type of Content!")], outputs=gr.TextArea(label='Content'), title=title, description=description ) webapp.launch()