Abu1998 commited on
Commit
efe98bd
·
verified ·
1 Parent(s): 67a5eca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -52
app.py CHANGED
@@ -1,65 +1,60 @@
1
  import streamlit as st
2
- from youtube_transcript_api import YouTubeTranscriptApi
3
  import openai
4
  import pandas as pd
 
5
  import io
6
 
7
- # Streamlit UI
8
  st.title("YouTube Video to Shorts Script Generator")
 
9
 
10
- # User inputs for API keys and video ID
11
- youtube_api_key = st.text_input("Enter your YouTube API Key", type="password")
12
- openai_api_key = st.text_input("Enter your OpenAI API Key", type="password")
13
- video_id = st.text_input("Enter the Video ID")
14
-
15
- # Function to get transcript from a YouTube video
16
- def get_transcript(video_id):
17
- transcript = YouTubeTranscriptApi.get_transcript(video_id)
18
- full_text = " ".join([entry['text'] for entry in transcript])
19
- return full_text
20
 
21
- # Function to generate a YouTube Shorts script using OpenAI
22
- def generate_shorts_script(transcript, openai_api_key):
23
- openai.api_key = openai_api_key
24
- response = openai.ChatCompletion.create(
25
- model="gpt-3.5-turbo",
26
- messages=[
27
- {"role": "system", "content": "You are an expert short form video script writer."},
28
- {"role": "user", "content": f"Write a compelling 1 minute YouTube Shorts script based on this transcript: {transcript}"}
29
- ]
30
- )
31
- return response.choices[0].message['content']
32
-
33
- # Action button
34
  if st.button("Generate Script"):
35
- if youtube_api_key and openai_api_key and video_id:
36
- with st.spinner("Extracting transcript and generating script..."):
37
- try:
38
- transcript = get_transcript(video_id)
39
- shorts_script = generate_shorts_script(transcript, openai_api_key)
40
-
41
- # Display output
42
- st.subheader("Generated YouTube Shorts Script")
43
- st.write(shorts_script)
44
-
45
- # Prepare download options
46
- buffer = io.StringIO()
47
- buffer.write(shorts_script)
48
- st.download_button(label="Download as Text", data=buffer.getvalue(), file_name=f"{video_id}_shorts_script.txt", mime="text/plain")
49
-
50
- buffer = io.StringIO()
51
- df = pd.DataFrame([{'Script': shorts_script}])
52
- df.to_csv(buffer, index=False)
53
- st.download_button(label="Download as CSV", data=buffer.getvalue(), file_name=f"{video_id}_shorts_script.csv", mime="text/csv")
54
-
55
- buffer = io.BytesIO()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  from docx import Document
57
  doc = Document()
58
  doc.add_paragraph(shorts_script)
59
- doc.save(buffer)
60
- st.download_button(label="Download as Word Document", data=buffer.getvalue(), file_name=f"{video_id}_shorts_script.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
61
-
62
- except Exception as e:
63
- st.error(f"An error occurred: {e}")
64
  else:
65
- st.warning("Please enter all required fields (API keys and Video ID).")
 
1
  import streamlit as st
 
2
  import openai
3
  import pandas as pd
4
+ from youtube_transcript_api import YouTubeTranscriptApi
5
  import io
6
 
7
+ # Streamlit interface
8
  st.title("YouTube Video to Shorts Script Generator")
9
+ st.write("Convert YouTube video transcripts to a 1-minute script using ChatGPT.")
10
 
11
+ # API key inputs
12
+ openai_api_key = st.text_input("Enter your OpenAI API key:", type="password")
13
+ youtube_video_id = st.text_input("Enter YouTube Video ID:")
 
 
 
 
 
 
 
14
 
15
+ # Generate button
 
 
 
 
 
 
 
 
 
 
 
 
16
  if st.button("Generate Script"):
17
+ if openai_api_key and youtube_video_id:
18
+ # Loading screen
19
+ with st.spinner("Generating script..."):
20
+ # Get the transcript
21
+ transcript = YouTubeTranscriptApi.get_transcript(youtube_video_id)
22
+ full_text = " ".join([entry['text'] for entry in transcript])
23
+
24
+ # Call OpenAI API
25
+ openai.api_key = openai_api_key
26
+ response = openai.ChatCompletion.create(
27
+ model="gpt-3.5-turbo",
28
+ messages=[
29
+ {"role": "system", "content": "You are an expert short form video script writer."},
30
+ {"role": "user", "content": f"Write a compelling 1 minute YouTube Shorts script based on this transcript: {full_text}"}
31
+ ]
32
+ )
33
+ shorts_script = response['choices'][0]['message']['content']
34
+
35
+ # Display the result
36
+ st.subheader("Generated Script")
37
+ st.write(shorts_script)
38
+
39
+ # Options to download the script
40
+ download_format = st.selectbox("Choose download format:", ["CSV", "Text", "Word"])
41
+
42
+ if download_format == "CSV":
43
+ df = pd.DataFrame([shorts_script], columns=["Script"])
44
+ csv_buffer = io.StringIO()
45
+ df.to_csv(csv_buffer, index=False)
46
+ st.download_button(label="Download CSV", data=csv_buffer.getvalue(), file_name="shorts_script.csv", mime="text/csv")
47
+
48
+ elif download_format == "Text":
49
+ st.download_button(label="Download Text", data=shorts_script, file_name="shorts_script.txt", mime="text/plain")
50
+
51
+ elif download_format == "Word":
52
  from docx import Document
53
  doc = Document()
54
  doc.add_paragraph(shorts_script)
55
+ doc_buffer = io.BytesIO()
56
+ doc.save(doc_buffer)
57
+ doc_buffer.seek(0)
58
+ st.download_button(label="Download Word", data=doc_buffer, file_name="shorts_script.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
 
59
  else:
60
+ st.error("Please provide both API key and YouTube video ID.")