Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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).")
|