File size: 4,862 Bytes
43f7580
 
 
e14ebc1
43f7580
61cca29
 
 
 
 
 
 
9e61d46
 
 
 
61cca29
 
c7b96b9
327b9b1
61cca29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
os.system('pip install moviepy imageio[ffmpeg]')

from moviepy.editor import *

import streamlit as st
import openai
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
import requests
from PIL import Image
from io import BytesIO
import os

openai.api_key = os.getenv("OPENAI_API_KEY")


# Setup API keys
google_api_key = AIzaSyBYfDKsd8jGFs3dNijZo62n9fiD4D2ewmI
google_drive_folder_id = "1kE94BgYdAiCHFSCDa6jhCyJcPEv94cQ-"

def generate_presentation_text(input_data):
    """Generate presentation content based on input data using GPT."""
    prompt = f"Generate a professional presentation based on the following details:\n{input_data}\nInclude slide titles, bullet points, and a brief summary for each slide."
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7
    )
    return response['choices'][0]['message']['content']

def fetch_background_image(query):
    """Fetch background image from Unsplash or another source."""
    url = f"https://source.unsplash.com/1920x1080/?{query}"
    response = requests.get(url)
    if response.status_code == 200:
        return Image.open(BytesIO(response.content))
    else:
        return None

def create_google_docs_presentation(presentation_text, output_file):
    """Create a Google Docs presentation using the Google Drive API."""
    try:
        # Google Docs API setup
        service = build('drive', 'v3', developerKey=google_api_key)
        
        # Write the text to a .docx file
        with open(output_file, "w") as doc_file:
            doc_file.write(presentation_text)
        
        # Upload the document to Google Drive
        file_metadata = {
            "name": "Presentation.docx",
            "parents": [google_drive_folder_id],
            "mimeType": "application/vnd.google-apps.document"
        }
        media = MediaFileUpload(output_file, mimetype="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
        uploaded_file = service.files().create(body=file_metadata, media_body=media, fields="id").execute()
        
        return f"https://docs.google.com/document/d/{uploaded_file.get('id')}/edit"
    except Exception as e:
        return str(e)

def generate_presentation_video(slides):
    """Generate a video presentation from slides using MoviePy."""
    clips = []
    for slide in slides:
        text = slide.get("title") + "\n\n" + "\n".join(slide.get("points"))
        img = fetch_background_image(slide.get("image_query"))
        if img:
            img.save("slide.jpg")
        else:
            img = Image.new("RGB", (1920, 1080), color=(255, 255, 255))
            img.save("slide.jpg")
        
        # Add text overlay to the image
        clip = ImageClip("slide.jpg").set_duration(5).fx(vfx.fadein, 1)
        text_clip = TextClip(text, fontsize=50, color='white', size=clip.size).set_duration(5)
        text_clip = text_clip.set_position("center")
        combined = CompositeVideoClip([clip, text_clip])
        clips.append(combined)
    
    # Combine all clips
    final_video = concatenate_videoclips(clips, method="compose")
    final_video.write_videofile("presentation_video.mp4", fps=24)

    return "presentation_video.mp4"

# Streamlit app UI
st.title("Presentation Builder App")
st.subheader("Generate a Google Docs presentation and an AI-powered video presentation.")

# Input form
with st.form("presentation_form"):
    st.write("**Enter your presentation details:**")
    input_data = st.text_area("Input Details (e.g., project goals, objectives, etc.)")
    video = st.checkbox("Generate AI video of presentation (optional)")
    submitted = st.form_submit_button("Generate Presentation")

if submitted:
    st.write("Generating presentation...")
    presentation_text = generate_presentation_text(input_data)
    st.success("Presentation text generated!")

    # Display generated text
    st.write("**Generated Presentation:**")
    st.text_area("Generated Presentation", value=presentation_text, height=300)

    # Create Google Docs output
    doc_url = create_google_docs_presentation(presentation_text, "presentation.docx")
    if "http" in doc_url:
        st.success("Google Docs presentation created!")
        st.write(f"[View Document]({doc_url})")
    else:
        st.error("Error creating Google Docs presentation.")
        st.write(doc_url)

    # Generate video (optional)
    if video:
        slides = [
            {"title": "Slide 1", "points": ["Point 1", "Point 2"], "image_query": "technology"},
            {"title": "Slide 2", "points": ["Point 1", "Point 2"], "image_query": "business"},
        ]
        video_path = generate_presentation_video(slides)
        st.success("Video presentation created!")
        st.video(video_path)