File size: 2,621 Bytes
341e236
 
 
efe98bd
272eb45
4a1a7a0
341e236
272eb45
 
 
 
 
341e236
272eb45
 
 
 
 
 
 
 
 
 
 
4a1a7a0
272eb45
 
 
 
 
 
 
 
341e236
 
272eb45
 
 
efe98bd
4a1a7a0
272eb45
 
4a1a7a0
272eb45
 
4a1a7a0
272eb45
 
 
efe98bd
272eb45
 
 
 
 
efe98bd
272eb45
 
efe98bd
272eb45
341e236
 
272eb45
 
 
 
 
 
341e236
272eb45
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
import openai
import pandas as pd
from youtube_transcript_api import YouTubeTranscriptApi
from io import StringIO
from docx import Document

# Function to get YouTube transcript
def get_transcript(video_id):
    transcript = YouTubeTranscriptApi.get_transcript(video_id)
    full_text = " ".join([entry['text'] for entry in transcript])
    return full_text

# Function to generate a script using OpenAI
def generate_shorts_script(transcript, ai_system_content, user_content):
    openai.api_key = st.secrets["OPENAI_API_KEY"]  # API Key from Streamlit secrets
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": ai_system_content},
            {"role": "user", "content": f"{user_content}: {transcript}"}
        ]
    )
    return response.choices[0].message["content"]

# Streamlit UI
st.title("YouTube Shorts Script Generator")

# Input fields
openai_api_key = st.text_input("OpenAI API Key", type="password")
video_id = st.text_input("YouTube Video ID")
ai_system_content = st.text_area("AI System Content")
user_content = st.text_area("User Content")

if st.button("Generate Script"):
    if openai_api_key and video_id and ai_system_content and user_content:
        try:
            # Set OpenAI API key
            openai.api_key = openai_api_key

            # Get transcript
            transcript = get_transcript(video_id)

            # Generate script
            shorts_script = generate_shorts_script(transcript, ai_system_content, user_content)

            # Display script
            st.write("Generated Script:")
            st.write(shorts_script)

            # Options to download
            if st.button("Download as CSV"):
                df = pd.DataFrame({"Script": [shorts_script]})
                csv = df.to_csv(index=False)
                st.download_button("Download CSV", csv, "script.csv", "text/csv")

            if st.button("Download as Text"):
                st.download_button("Download Text", shorts_script, "script.txt", "text/plain")

            if st.button("Download as Word"):
                doc = Document()
                doc.add_paragraph(shorts_script)
                buffer = StringIO()
                doc.save(buffer)
                docx = buffer.getvalue()
                st.download_button("Download Word", docx, "script.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
        except Exception as e:
            st.error(f"An error occurred: {e}")
    else:
        st.error("Please fill all the input fields.")