Darpan07 commited on
Commit
b6df1e5
1 Parent(s): 6b4877f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -7
app.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -8,8 +9,11 @@ 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
 
@@ -24,8 +28,10 @@ def get_transcript(youtube_url):
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 """
@@ -34,8 +40,9 @@ def get_title(youtube_url):
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)"""
@@ -43,6 +50,10 @@ def get_result(url,content):
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
 
@@ -67,7 +78,10 @@ def get_result(url,content):
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
 
 
1
  # importing the required libraries and classes
2
  import os
3
  import gradio as gr
4
+ from dotenv import load_dotenv
5
  from langchain_openai import OpenAI
6
  from langchain.prompts import PromptTemplate
7
  from pytube import YouTube
 
9
  from langchain.chains import LLMChain
10
  from langchain_openai import ChatOpenAI
11
 
12
+ # Load environment variables from .env file
13
+ load_dotenv()
14
+
15
+ # Access the API key
16
+ openai_api_key = os.getenv("OPENAI_API_KEY")
17
 
18
 
19
 
 
28
  transcript_text = " ".join([entry["text"] for entry in transcript])
29
 
30
  return transcript_text
31
+
32
+ # returning -1, if the URL is not valid
33
+ except:
34
+ return -1
35
 
36
  def get_title(youtube_url):
37
  """ This function will return the Title of the given YouTube URL """
 
40
  yt_video = YouTube(youtube_url)
41
  return yt_video.title
42
 
43
+ # returning -1, if the URL is not valid
44
+ except:
45
+ return -1
46
 
47
  def get_result(url,content):
48
  """ This function will return the content(type of content provided by the user)"""
 
50
  # getting the title and transcript of the Youtube URL
51
  title,transcript = get_title(url) , get_transcript(url)
52
 
53
+ # if the URL is not valid
54
+ if title == -1 and transcript == -1:
55
+ return f'The provided url: "{url}" is not valid. Please provide a valid URL.'
56
+
57
  # this template is for the Article or Stories
58
  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}"
59
 
 
78
  )
79
 
80
  # invoking the chain and return the output i.e. content
81
+ try:
82
+ return chain.invoke({'title':title,'transcript':transcript,'user_input':content})['text']
83
+ except Exception as e:
84
+ return f'Error: {e}'
85
 
86
 
87