Spaces:
Sleeping
Sleeping
Update save button
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ from datetime import datetime
|
|
6 |
import openai
|
7 |
import pdfkit
|
8 |
import random
|
9 |
-
|
10 |
|
11 |
music_files = [
|
12 |
"RPReplay_Final1712757356.mp3",
|
@@ -82,6 +82,28 @@ def format_cocktail_output(name, quote, ingredients, instruction, notes):
|
|
82 |
'''
|
83 |
return html_output
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
with open('style.css', 'r') as file:
|
86 |
css_styles = file.read()
|
87 |
|
@@ -117,26 +139,15 @@ with gr.Blocks(css=css_styles) as demo:
|
|
117 |
|
118 |
play_button = gr.Button("Play Music", visible=False, elem_classes=["generate-button"], scale=1) # Initially not visible
|
119 |
background_music = gr.Audio(label="Background Music", autoplay=True, visible=False, scale=4) # Initially not visible
|
|
|
120 |
|
121 |
with gr.Row():
|
122 |
-
save_pdf_button = gr.Button("Download Recipe", visible=False)
|
123 |
|
124 |
def on_generate_click(*args):
|
125 |
recipe, show_play_button, show_save_button = generate_cocktail(*args)
|
126 |
return recipe, gr.update(visible=show_play_button), gr.update(visible=show_save_button)
|
127 |
|
128 |
-
def save_as_pdf(html_content):
|
129 |
-
# Define path for temporary HTML and PDF files
|
130 |
-
html_path = "output_recipe.html"
|
131 |
-
pdf_path = "output_recipe.pdf"
|
132 |
-
|
133 |
-
# Write the HTML content to a temporary HTML file
|
134 |
-
with open(html_path, 'w') as f:
|
135 |
-
f.write(html_content)
|
136 |
-
|
137 |
-
# Convert HTML to PDF
|
138 |
-
pdfkit.from_file(html_path, pdf_path)
|
139 |
-
|
140 |
def reset():
|
141 |
return "", 0, 0, 0, 0, [], [], 10, "", "", "", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
142 |
|
@@ -148,7 +159,7 @@ with gr.Blocks(css=css_styles) as demo:
|
|
148 |
|
149 |
play_button.click(fn=play_music, inputs=[], outputs=[background_music, background_music])
|
150 |
|
151 |
-
save_pdf_button.click(fn=save_as_pdf, inputs=[output_recipe], outputs=[])
|
152 |
|
153 |
clear_button.click(fn=reset, inputs=[], outputs=[mood, sweetness, sour, savory, bitter, flavor_association, drinking_experience, soberness_level, allergies, additional_requests, output_recipe, play_button, background_music, save_pdf_button])
|
154 |
|
|
|
6 |
import openai
|
7 |
import pdfkit
|
8 |
import random
|
9 |
+
import base64
|
10 |
|
11 |
music_files = [
|
12 |
"RPReplay_Final1712757356.mp3",
|
|
|
82 |
'''
|
83 |
return html_output
|
84 |
|
85 |
+
def save_as_pdf(html_content):
|
86 |
+
"""Converts HTML content to PDF, encodes it in base64, and returns a download link."""
|
87 |
+
html_path = "output_recipe.html"
|
88 |
+
pdf_path = "output_recipe.pdf"
|
89 |
+
|
90 |
+
# Write the HTML content to a temporary file
|
91 |
+
with open(html_path, 'w') as f:
|
92 |
+
f.write(html_content)
|
93 |
+
|
94 |
+
# Convert HTML to PDF
|
95 |
+
pdfkit.from_file(html_path, pdf_path)
|
96 |
+
|
97 |
+
# Encode the PDF file in base64
|
98 |
+
with open(pdf_path, "rb") as pdf_file:
|
99 |
+
encoded_pdf = base64.b64encode(pdf_file.read()).decode("utf-8")
|
100 |
+
|
101 |
+
# Create a Data URL for the PDF
|
102 |
+
pdf_data_url = f"data:application/pdf;base64,{encoded_pdf}"
|
103 |
+
|
104 |
+
# Return HTML anchor tag for the download link
|
105 |
+
return f'<a href="{pdf_data_url}" download="CocktailRecipe.pdf" style="color: white; font-size: 20px;">Download PDF</a>'
|
106 |
+
|
107 |
with open('style.css', 'r') as file:
|
108 |
css_styles = file.read()
|
109 |
|
|
|
139 |
|
140 |
play_button = gr.Button("Play Music", visible=False, elem_classes=["generate-button"], scale=1) # Initially not visible
|
141 |
background_music = gr.Audio(label="Background Music", autoplay=True, visible=False, scale=4) # Initially not visible
|
142 |
+
pdf_download_link = gr.HTML(visible=False) # For displaying the PDF download link
|
143 |
|
144 |
with gr.Row():
|
145 |
+
save_pdf_button = gr.Button("Download Recipe as PDF", visible=False)
|
146 |
|
147 |
def on_generate_click(*args):
|
148 |
recipe, show_play_button, show_save_button = generate_cocktail(*args)
|
149 |
return recipe, gr.update(visible=show_play_button), gr.update(visible=show_save_button)
|
150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
def reset():
|
152 |
return "", 0, 0, 0, 0, [], [], 10, "", "", "", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
153 |
|
|
|
159 |
|
160 |
play_button.click(fn=play_music, inputs=[], outputs=[background_music, background_music])
|
161 |
|
162 |
+
save_pdf_button.click(fn=save_as_pdf, inputs=[output_recipe], outputs=[pdf_download_link])
|
163 |
|
164 |
clear_button.click(fn=reset, inputs=[], outputs=[mood, sweetness, sour, savory, bitter, flavor_association, drinking_experience, soberness_level, allergies, additional_requests, output_recipe, play_button, background_music, save_pdf_button])
|
165 |
|