Darpan07 commited on
Commit
ce1e73e
·
verified ·
1 Parent(s): fd7ecf1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # importing the required libraries and classes
2
+ import os
3
+ import gradio as gr
4
+ from langchain_openai import OpenAI
5
+ from langchain.prompts import PromptTemplate
6
+ from pytube import YouTube
7
+ from youtube_transcript_api import YouTubeTranscriptApi
8
+ from langchain.chains import LLMChain
9
+ from langchain_openai import ChatOpenAI
10
+
11
+ # setting the openai_api_key
12
+ os.environ['OPENAI_API_KEY'] = "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"
13
+
14
+
15
+
16
+ def get_transcript(youtube_url):
17
+ """ This Function will return the transcript of the given YouTube URL """
18
+ try:
19
+ # Extract the video ID from the YouTube URL
20
+ video_id = youtube_url.split("?v=")[1]
21
+
22
+ # Get the transcript
23
+ transcript = YouTubeTranscriptApi.get_transcript(video_id)
24
+ transcript_text = " ".join([entry["text"] for entry in transcript])
25
+
26
+ return transcript_text
27
+ except Exception as e:
28
+ return f"Error: {e}"
29
+
30
+ def get_title(youtube_url):
31
+ """ This function will return the Title of the given YouTube URL """
32
+ try:
33
+ # getting the title of the Youtube video
34
+ yt_video = YouTube(youtube_url)
35
+ return yt_video.title
36
+
37
+ except Exception as e:
38
+ return f'Error: {e}'
39
+
40
+ def get_result(url,content):
41
+ """ This function will return the content(type of content provided by the user)"""
42
+
43
+ # getting the title and transcript of the Youtube URL
44
+ title,transcript = get_title(url) , get_transcript(url)
45
+
46
+ # this template is for the Article or Stories
47
+ 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}"
48
+
49
+ # this template is for the Short Video Script
50
+ 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}"
51
+
52
+
53
+ if content == 'Shorts/Reel Script':
54
+ prompt = PromptTemplate(
55
+ template = reel_template,
56
+ input_variables = ['title','transcript','user_input']
57
+ )
58
+ else:
59
+ prompt = PromptTemplate(
60
+ template = basic_template,
61
+ input_variables=['title','transcript','user_input']
62
+ )
63
+
64
+ chain = LLMChain(
65
+ prompt = prompt,
66
+ llm = ChatOpenAI(temperature=0.7, model = 'gpt-3.5-turbo-16k')
67
+ )
68
+
69
+ # invoking the chain and return the output i.e. content
70
+ return chain.invoke({'title':title,'transcript':transcript,'user_input':content})['text']
71
+
72
+
73
+
74
+ if __name__ == "__main__":
75
+ # name of the WebApp
76
+ title="Creative Content Generator"
77
+
78
+ # About the WebApp
79
+ 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'
80
+
81
+ webapp = gr.Interface(
82
+ fn = get_result,
83
+ inputs=[gr.Textbox(placeholder="Provide the URL here...."),gr.Dropdown(["Stories",'Article','Shorts/Reel Script'],label="Choose the type of Content!")],
84
+ outputs=gr.TextArea(label='Content'),
85
+ title=title,
86
+ description=description
87
+ )
88
+
89
+ webapp.launch()
90
+
91
+