Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,86 +2,69 @@ import streamlit as st
|
|
| 2 |
import openai
|
| 3 |
import pandas as pd
|
| 4 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 5 |
-
import
|
| 6 |
from docx import Document
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Generate button
|
| 22 |
if st.button("Generate Script"):
|
| 23 |
-
if openai_api_key and
|
| 24 |
-
|
| 25 |
-
|
| 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
|
| 31 |
openai.api_key = openai_api_key
|
| 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 |
-
|
| 50 |
-
|
| 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 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 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 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
df.to_csv(
|
| 74 |
-
st.download_button(
|
| 75 |
|
| 76 |
-
|
| 77 |
-
st.download_button(
|
| 78 |
|
| 79 |
-
|
| 80 |
doc = Document()
|
| 81 |
doc.add_paragraph(shorts_script)
|
| 82 |
-
|
| 83 |
-
doc.save(
|
| 84 |
-
|
| 85 |
-
st.download_button(
|
|
|
|
|
|
|
| 86 |
else:
|
| 87 |
-
st.error("Please
|
|
|
|
| 2 |
import openai
|
| 3 |
import pandas as pd
|
| 4 |
from youtube_transcript_api import YouTubeTranscriptApi
|
| 5 |
+
from io import StringIO
|
| 6 |
from docx import Document
|
| 7 |
|
| 8 |
+
# Function to get YouTube transcript
|
| 9 |
+
def get_transcript(video_id):
|
| 10 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 11 |
+
full_text = " ".join([entry['text'] for entry in transcript])
|
| 12 |
+
return full_text
|
| 13 |
|
| 14 |
+
# Function to generate a script using OpenAI
|
| 15 |
+
def generate_shorts_script(transcript, ai_system_content, user_content):
|
| 16 |
+
openai.api_key = st.secrets["OPENAI_API_KEY"] # API Key from Streamlit secrets
|
| 17 |
+
response = openai.ChatCompletion.create(
|
| 18 |
+
model="gpt-3.5-turbo",
|
| 19 |
+
messages=[
|
| 20 |
+
{"role": "system", "content": ai_system_content},
|
| 21 |
+
{"role": "user", "content": f"{user_content}: {transcript}"}
|
| 22 |
+
]
|
| 23 |
+
)
|
| 24 |
+
return response.choices[0].message["content"]
|
| 25 |
|
| 26 |
+
# Streamlit UI
|
| 27 |
+
st.title("YouTube Shorts Script Generator")
|
| 28 |
+
|
| 29 |
+
# Input fields
|
| 30 |
+
openai_api_key = st.text_input("OpenAI API Key", type="password")
|
| 31 |
+
video_id = st.text_input("YouTube Video ID")
|
| 32 |
+
ai_system_content = st.text_area("AI System Content")
|
| 33 |
+
user_content = st.text_area("User Content")
|
| 34 |
|
|
|
|
| 35 |
if st.button("Generate Script"):
|
| 36 |
+
if openai_api_key and video_id and ai_system_content and user_content:
|
| 37 |
+
try:
|
| 38 |
+
# Set OpenAI API key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
openai.api_key = openai_api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Get transcript
|
| 42 |
+
transcript = get_transcript(video_id)
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Generate script
|
| 45 |
+
shorts_script = generate_shorts_script(transcript, ai_system_content, user_content)
|
| 46 |
|
| 47 |
+
# Display script
|
| 48 |
+
st.write("Generated Script:")
|
| 49 |
+
st.write(shorts_script)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Options to download
|
| 52 |
+
if st.button("Download as CSV"):
|
| 53 |
+
df = pd.DataFrame({"Script": [shorts_script]})
|
| 54 |
+
csv = df.to_csv(index=False)
|
| 55 |
+
st.download_button("Download CSV", csv, "script.csv", "text/csv")
|
| 56 |
|
| 57 |
+
if st.button("Download as Text"):
|
| 58 |
+
st.download_button("Download Text", shorts_script, "script.txt", "text/plain")
|
| 59 |
|
| 60 |
+
if st.button("Download as Word"):
|
| 61 |
doc = Document()
|
| 62 |
doc.add_paragraph(shorts_script)
|
| 63 |
+
buffer = StringIO()
|
| 64 |
+
doc.save(buffer)
|
| 65 |
+
docx = buffer.getvalue()
|
| 66 |
+
st.download_button("Download Word", docx, "script.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.error(f"An error occurred: {e}")
|
| 69 |
else:
|
| 70 |
+
st.error("Please fill all the input fields.")
|