Abu1998 commited on
Commit
64284f9
·
verified ·
1 Parent(s): bcd2206

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ from youtube_transcript_api import YouTubeTranscriptApi
4
+
5
+ # Function to get YouTube transcript
6
+ def get_transcript(video_id):
7
+ """Fetches the transcript for a given YouTube video ID."""
8
+ try:
9
+ transcript_list = YouTubeTranscriptApi.get_transcript(video_id)
10
+ transcript = " ".join([entry['text'] for entry in transcript_list])
11
+ return transcript
12
+ except Exception as e:
13
+ st.error(f"Error fetching transcript: {e}")
14
+ return None
15
+
16
+ # Function to summarize the transcript using OpenAI's GPT API
17
+ def summarize_transcript(transcript):
18
+ """Summarizes the given transcript using OpenAI's GPT API."""
19
+ if transcript is None:
20
+ return None
21
+
22
+ response = openai.ChatCompletion.create(
23
+ model="gpt-3.5-turbo",
24
+ messages=[
25
+ {"role": "system", "content": "You are a helpful AI assistant that summarizes YouTube videos."},
26
+ {"role": "user", "content": f"Please provide a concise summary of the following YouTube video transcript:\n\n{transcript}"}
27
+ ]
28
+ )
29
+ summary = response.choices[0].message["content"].strip()
30
+ return summary
31
+
32
+ # Function to generate a 1-minute YouTube Shorts script based on the summary
33
+ def generate_shorts_script(summary):
34
+ """Generates a 1-minute shorts script based on the summary."""
35
+ if summary is None:
36
+ return None
37
+
38
+ response = openai.ChatCompletion.create(
39
+ model="gpt-3.5-turbo",
40
+ messages=[
41
+ {"role": "system", "content": "You are a creative script writer for YouTube Shorts."},
42
+ {"role": "user", "content": f"Write a captivating and engaging 1-minute script for a YouTube Short based on this summary: {summary}. Focus on impactful visuals and concise storytelling."}
43
+ ]
44
+ )
45
+ script = response.choices[0].message["content"].strip()
46
+ return script
47
+
48
+ # Streamlit UI
49
+ st.title("YouTube Video to Shorts Script Converter")
50
+
51
+ # Input fields
52
+ openai_api_key = st.text_input("OpenAI API Key", type="password")
53
+ video_id = st.text_input("YouTube Video ID")
54
+
55
+ if st.button("Generate Shorts Script"):
56
+ if openai_api_key and video_id:
57
+ try:
58
+ # Set OpenAI API key
59
+ openai.api_key = openai_api_key
60
+
61
+ # Get transcript
62
+ transcript = get_transcript(video_id)
63
+
64
+ # Summarize the transcript
65
+ summary = summarize_transcript(transcript)
66
+
67
+ # Generate shorts script
68
+ if summary:
69
+ shorts_script = generate_shorts_script(summary)
70
+
71
+ # Display the generated shorts script
72
+ if shorts_script:
73
+ st.write("Generated YouTube Shorts Script:")
74
+ st.write(shorts_script)
75
+
76
+ except Exception as e:
77
+ st.error(f"An error occurred: {e}")
78
+ else:
79
+ st.error("Please fill in both the OpenAI API Key and YouTube Video ID.")
80
+