slide_duck / app.py
jeremierostan's picture
Update app.py
6ea0a57 verified
# app.py
import gradio as gr
import os
from presentation_assistant import PresentationAssistant
import tempfile
import shutil
def create_presentation(api_key, files, query, num_slides, export_format="markdown"):
try:
if not api_key.strip():
raise ValueError("Please provide your Gemini API key")
# Create a temporary directory to store uploaded files
with tempfile.TemporaryDirectory() as temp_dir:
# Save uploaded files to temporary directory
file_paths = []
for file in files:
original_filename = os.path.basename(file.name)
file_path = os.path.join(temp_dir, original_filename)
shutil.copy(file.name, file_path)
file_paths.append(file_path)
# Initialize assistant with provided API key
assistant = PresentationAssistant(api_key=api_key)
# Get the presentation content
relevant_content = assistant.analyze_documents(temp_dir, query)
if not relevant_content:
raise ValueError("No relevant content found in documents")
presentation_content = assistant.generate_presentation_content(relevant_content, num_slides)
if not presentation_content:
raise ValueError("Failed to generate presentation content")
# Parse the content
title, subtitle, slides = assistant.parse_presentation_content(presentation_content)
# Create markdown version
assistant.write_presentation_file(title, subtitle, slides)
with open('example.md', 'r', encoding='utf-8') as f:
markdown_content = f.read()
# Create instructions and preview
instructions = f"""
# Your Presentation is Ready! πŸŽ‰
## Quick View with Slidev
1. Go to [Slidev Online Editor](https://sli.dev/new)
2. Copy and paste ALL of the content below into the editor
3. Click "Preview" to start the presentation
## Your Presentation Content:
```markdown
{markdown_content}
```
"""
# Create PowerPoint if requested
if export_format == "pptx":
pptx_path = assistant.export_to_pptx(title, subtitle, slides)
return instructions, pptx_path
else:
# Save as markdown file for download
output_path = "presentation.md"
with open(output_path, "w", encoding='utf-8') as f:
f.write(markdown_content)
return instructions, output_path
except Exception as e:
print(f"Error details: {str(e)}")
return f"Error: {str(e)}", None
# Create Gradio interface
iface = gr.Interface(
fn=create_presentation,
inputs=[
gr.Textbox(
label="Gemini API Key",
placeholder="Enter your Gemini API key here",
type="password" # This hides the API key
),
gr.File(file_count="multiple", label="Upload Documents (.txt, .pdf, .md, .doc, .docx)"),
gr.Textbox(label="Instructions"),
gr.Number(label="Number of slides", minimum=1, maximum=20, step=1, value=5),
gr.Radio(["markdown", "pptx"], label="Download Format", value="markdown")
],
outputs=[
gr.Markdown(label="Preview and Instructions"),
gr.File(label="Download Presentation")
],
title="Slide Duck πŸ₯",
description="""Upload documents and generate a presentation based on their content using AI.
You can preview directly in Slidev's online editor or download in your preferred format."""
)
# Launch the app
iface.launch()