Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ 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")
|
|
@@ -10,15 +11,20 @@ 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
|
|
@@ -26,30 +32,51 @@ if st.button("Generate Script"):
|
|
| 26 |
response = openai.ChatCompletion.create(
|
| 27 |
model="gpt-3.5-turbo",
|
| 28 |
messages=[
|
| 29 |
-
{"role": "system", "content":
|
| 30 |
-
{"role": "user", "content": f"
|
| 31 |
]
|
| 32 |
)
|
| 33 |
-
shorts_script = response
|
| 34 |
|
| 35 |
# Display the result
|
| 36 |
st.subheader("Generated Script")
|
| 37 |
st.write(shorts_script)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Options to download the script
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
if
|
| 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
|
| 49 |
st.download_button(label="Download Text", data=shorts_script, file_name="shorts_script.txt", mime="text/plain")
|
| 50 |
|
| 51 |
-
elif
|
| 52 |
-
from docx import Document
|
| 53 |
doc = Document()
|
| 54 |
doc.add_paragraph(shorts_script)
|
| 55 |
doc_buffer = io.BytesIO()
|
|
@@ -57,4 +84,4 @@ if st.button("Generate Script"):
|
|
| 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
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 5 |
import io
|
| 6 |
+
from docx import Document
|
| 7 |
|
| 8 |
# Streamlit interface
|
| 9 |
st.title("YouTube Video to Shorts Script Generator")
|
|
|
|
| 11 |
|
| 12 |
# API key inputs
|
| 13 |
openai_api_key = st.text_input("Enter your OpenAI API key:", type="password")
|
| 14 |
+
youtube_transcript_api_key = st.text_input("Enter your YouTube Transcript API key:", type="password")
|
| 15 |
+
|
| 16 |
+
# Video ID and content inputs
|
| 17 |
youtube_video_id = st.text_input("Enter YouTube Video ID:")
|
| 18 |
+
ai_agent_system_content = st.text_area("Enter AI agent system content:")
|
| 19 |
+
user_content = st.text_area("Enter user content:")
|
| 20 |
|
| 21 |
# Generate button
|
| 22 |
if st.button("Generate Script"):
|
| 23 |
+
if openai_api_key and youtube_transcript_api_key and youtube_video_id and ai_agent_system_content and user_content:
|
| 24 |
# Loading screen
|
| 25 |
with st.spinner("Generating script..."):
|
| 26 |
# Get the transcript
|
| 27 |
+
transcript = YouTubeTranscriptApi.get_transcript(youtube_video_id, api_key=youtube_transcript_api_key)
|
| 28 |
full_text = " ".join([entry['text'] for entry in transcript])
|
| 29 |
|
| 30 |
# Call OpenAI API
|
|
|
|
| 32 |
response = openai.ChatCompletion.create(
|
| 33 |
model="gpt-3.5-turbo",
|
| 34 |
messages=[
|
| 35 |
+
{"role": "system", "content": ai_agent_system_content},
|
| 36 |
+
{"role": "user", "content": f"{user_content}: {full_text}"}
|
| 37 |
]
|
| 38 |
)
|
| 39 |
+
shorts_script = response.choices[0].message['content']
|
| 40 |
|
| 41 |
# Display the result
|
| 42 |
st.subheader("Generated Script")
|
| 43 |
st.write(shorts_script)
|
| 44 |
|
| 45 |
+
# Options to download the transcript
|
| 46 |
+
st.subheader("Download Transcript")
|
| 47 |
+
download_transcript = st.selectbox("Choose download format for transcript:", ["CSV", "Text", "Word"])
|
| 48 |
+
|
| 49 |
+
if download_transcript == "CSV":
|
| 50 |
+
df = pd.DataFrame([full_text], columns=["Transcript"])
|
| 51 |
+
csv_buffer = io.StringIO()
|
| 52 |
+
df.to_csv(csv_buffer, index=False)
|
| 53 |
+
st.download_button(label="Download CSV", data=csv_buffer.getvalue(), file_name="transcript.csv", mime="text/csv")
|
| 54 |
+
|
| 55 |
+
elif download_transcript == "Text":
|
| 56 |
+
st.download_button(label="Download Text", data=full_text, file_name="transcript.txt", mime="text/plain")
|
| 57 |
+
|
| 58 |
+
elif download_transcript == "Word":
|
| 59 |
+
doc = Document()
|
| 60 |
+
doc.add_paragraph(full_text)
|
| 61 |
+
doc_buffer = io.BytesIO()
|
| 62 |
+
doc.save(doc_buffer)
|
| 63 |
+
doc_buffer.seek(0)
|
| 64 |
+
st.download_button(label="Download Word", data=doc_buffer, file_name="transcript.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
| 65 |
+
|
| 66 |
# Options to download the script
|
| 67 |
+
st.subheader("Download Script")
|
| 68 |
+
download_script = st.selectbox("Choose download format for script:", ["CSV", "Text", "Word"], index=1)
|
| 69 |
|
| 70 |
+
if download_script == "CSV":
|
| 71 |
df = pd.DataFrame([shorts_script], columns=["Script"])
|
| 72 |
csv_buffer = io.StringIO()
|
| 73 |
df.to_csv(csv_buffer, index=False)
|
| 74 |
st.download_button(label="Download CSV", data=csv_buffer.getvalue(), file_name="shorts_script.csv", mime="text/csv")
|
| 75 |
|
| 76 |
+
elif download_script == "Text":
|
| 77 |
st.download_button(label="Download Text", data=shorts_script, file_name="shorts_script.txt", mime="text/plain")
|
| 78 |
|
| 79 |
+
elif download_script == "Word":
|
|
|
|
| 80 |
doc = Document()
|
| 81 |
doc.add_paragraph(shorts_script)
|
| 82 |
doc_buffer = io.BytesIO()
|
|
|
|
| 84 |
doc_buffer.seek(0)
|
| 85 |
st.download_button(label="Download Word", data=doc_buffer, file_name="shorts_script.docx", mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
| 86 |
else:
|
| 87 |
+
st.error("Please provide all required inputs.")
|