Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 8 |
st.title("YouTube Video to Shorts Script Generator")
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 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 |
-
#
|
| 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
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
from docx import Document
|
| 57 |
doc = Document()
|
| 58 |
doc.add_paragraph(shorts_script)
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
st.error(f"An error occurred: {e}")
|
| 64 |
else:
|
| 65 |
-
st.
|
|
|
|
| 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.")
|