Spaces:
Running
Running
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) | |