|
|
|
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_dotenv() |
|
|
|
|
|
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: |
|
|
|
video_id = youtube_url.split("?v=")[1] |
|
|
|
|
|
transcript = YouTubeTranscriptApi.get_transcript(video_id) |
|
transcript_text = " ".join([entry["text"] for entry in transcript]) |
|
|
|
return transcript_text |
|
|
|
|
|
except: |
|
return -1 |
|
|
|
def get_title(youtube_url): |
|
""" This function will return the Title of the given YouTube URL """ |
|
try: |
|
|
|
yt_video = YouTube(youtube_url) |
|
return yt_video.title |
|
|
|
|
|
except: |
|
return -1 |
|
|
|
def get_result(url,content): |
|
""" This function will return the content(type of content provided by the user)""" |
|
|
|
|
|
title,transcript = get_title(url) , get_transcript(url) |
|
|
|
|
|
if title == -1 and transcript == -1: |
|
return f'The provided url: "{url}" is not valid. Please provide a valid URL.' |
|
|
|
|
|
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}" |
|
|
|
|
|
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') |
|
) |
|
|
|
|
|
try: |
|
return chain.invoke({'title':title,'transcript':transcript,'user_input':content})['text'] |
|
except Exception as e: |
|
return f'Error: {e}' |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
title="Creative Content Generator" |
|
|
|
|
|
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() |
|
|
|
|
|
|