Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,7 @@ import re
|
|
5 |
from datetime import datetime
|
6 |
import openai
|
7 |
import random
|
8 |
-
|
9 |
-
# import tempfile
|
10 |
|
11 |
user_db = {
|
12 |
os.environ["username"]: os.environ["password"],
|
@@ -86,47 +85,14 @@ def format_cocktail_output(name, quote, ingredients, instruction, notes):
|
|
86 |
'''
|
87 |
return html_output
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
# # Write the HTML content to a temporary file
|
95 |
-
# with open(html_path, 'w') as f:
|
96 |
-
# f.write(html_content)
|
97 |
-
|
98 |
-
# # Convert HTML to PDF
|
99 |
-
# pdfkit.from_file(html_path, pdf_path)
|
100 |
-
|
101 |
-
# # Encode the PDF file in base64
|
102 |
-
# with open(pdf_path, "rb") as pdf_file:
|
103 |
-
# encoded_pdf = base64.b64encode(pdf_file.read()).decode("utf-8")
|
104 |
-
|
105 |
-
# # Create a Data URL for the PDF
|
106 |
-
# pdf_data_url = f"data:application/pdf;base64,{encoded_pdf}"
|
107 |
-
|
108 |
-
# # Return HTML anchor tag for the download link
|
109 |
-
# return f'<a href="{pdf_data_url}" download="CocktailRecipe.pdf" style="color: white; font-size: 20px;">Download PDF</a>'
|
110 |
|
111 |
-
#
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
# page.setContent(html_content)
|
116 |
-
|
117 |
-
# # Create a temporary file to save the PDF
|
118 |
-
# with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_file:
|
119 |
-
# pdf_path = tmp_file.name
|
120 |
-
|
121 |
-
# page.pdf({'path': pdf_path, 'format': 'A4'})
|
122 |
-
|
123 |
-
# # Close browser
|
124 |
-
# browser.close()
|
125 |
-
|
126 |
-
# # Generate URL for the temporary PDF file
|
127 |
-
# pdf_url = f'file://{pdf_path}'
|
128 |
-
|
129 |
-
# return pdf_url, True
|
130 |
|
131 |
with open('style.css', 'r') as file:
|
132 |
css_styles = file.read()
|
@@ -164,9 +130,9 @@ with gr.Blocks(css=css_styles) as MoodShaker:
|
|
164 |
play_button = gr.Button("Play Music", visible=False, elem_classes=["generate-button"], scale=1) # Initially not visible
|
165 |
background_music = gr.Audio(label="Background Music", autoplay=True, visible=False, scale=4) # Initially not visible
|
166 |
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
|
171 |
def on_generate_click(*args):
|
172 |
recipe, show_play_button = generate_cocktail(*args)
|
@@ -183,7 +149,7 @@ with gr.Blocks(css=css_styles) as MoodShaker:
|
|
183 |
|
184 |
play_button.click(fn=play_music, inputs=[], outputs=[background_music, background_music])
|
185 |
|
186 |
-
|
187 |
|
188 |
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])
|
189 |
|
|
|
5 |
from datetime import datetime
|
6 |
import openai
|
7 |
import random
|
8 |
+
from html2image import Html2Image
|
|
|
9 |
|
10 |
user_db = {
|
11 |
os.environ["username"]: os.environ["password"],
|
|
|
85 |
'''
|
86 |
return html_output
|
87 |
|
88 |
+
def save_as_png(html_content):
|
89 |
+
"""Converts HTML content to PDF, encodes it in base64, and returns a download link."""
|
90 |
+
hti = Html2Image()
|
91 |
+
css = 'body {background: url("https://images.unsplash.com/photo-1514361726087-38371321b5cd?q=80&w=2370&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D");}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
+
# screenshot an HTML string (css is optional)
|
94 |
+
hti.screenshot(html_str=html_content, css_str=css, save_as="CocktailRecipe.png")
|
95 |
+
return 'CocktailRecipe.png'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
with open('style.css', 'r') as file:
|
98 |
css_styles = file.read()
|
|
|
130 |
play_button = gr.Button("Play Music", visible=False, elem_classes=["generate-button"], scale=1) # Initially not visible
|
131 |
background_music = gr.Audio(label="Background Music", autoplay=True, visible=False, scale=4) # Initially not visible
|
132 |
|
133 |
+
with gr.Row():
|
134 |
+
save_pdf_button = gr.Button("Download Recipe as PNG", visible=False)
|
135 |
+
pdf_download_link = gr.File(label="Download Link", visible=False) # For displaying the PDF download link
|
136 |
|
137 |
def on_generate_click(*args):
|
138 |
recipe, show_play_button = generate_cocktail(*args)
|
|
|
149 |
|
150 |
play_button.click(fn=play_music, inputs=[], outputs=[background_music, background_music])
|
151 |
|
152 |
+
save_pdf_button.click(fn=save_as_png, inputs=[output_recipe], outputs=[output_path])
|
153 |
|
154 |
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])
|
155 |
|