tywei08 commited on
Commit
0aac908
·
verified ·
1 Parent(s): 26ea859

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -4,9 +4,8 @@ import json
4
  import re
5
  from datetime import datetime
6
  import openai
7
- import pdfkit
8
- import random
9
- import base64
10
 
11
  music_files = [
12
  "RPReplay_Final1712757356.mp3",
@@ -103,6 +102,26 @@ def save_as_pdf(html_content):
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()
@@ -139,10 +158,10 @@ with gr.Blocks(css=css_styles) as demo:
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)
@@ -159,7 +178,7 @@ with gr.Blocks(css=css_styles) as demo:
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
 
 
4
  import re
5
  from datetime import datetime
6
  import openai
7
+ from pyppeteer import launch
8
+ import tempfile
 
9
 
10
  music_files = [
11
  "RPReplay_Final1712757356.mp3",
 
102
 
103
  # Return HTML anchor tag for the download link
104
  return f'<a href="{pdf_data_url}" download="CocktailRecipe.pdf" style="color: white; font-size: 20px;">Download PDF</a>'
105
+
106
+ def generate_pdf_from_html(html_content):
107
+ browser = launch()
108
+ page = browser.newPage()
109
+
110
+ page.setContent(html_content)
111
+
112
+ # Create a temporary file to save the PDF
113
+ with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_file:
114
+ pdf_path = tmp_file.name
115
+
116
+ page.pdf({'path': pdf_path, 'format': 'A4'})
117
+
118
+ # Close browser
119
+ browser.close()
120
+
121
+ # Generate URL for the temporary PDF file
122
+ pdf_url = f'file://{pdf_path}'
123
+
124
+ return pdf_url, True
125
 
126
  with open('style.css', 'r') as file:
127
  css_styles = file.read()
 
158
 
159
  play_button = gr.Button("Play Music", visible=False, elem_classes=["generate-button"], scale=1) # Initially not visible
160
  background_music = gr.Audio(label="Background Music", autoplay=True, visible=False, scale=4) # Initially not visible
 
161
 
162
  with gr.Row():
163
  save_pdf_button = gr.Button("Download Recipe as PDF", visible=False)
164
+ pdf_download_link = gr.File(label="Download Link", visible=False) # For displaying the PDF download link
165
 
166
  def on_generate_click(*args):
167
  recipe, show_play_button, show_save_button = generate_cocktail(*args)
 
178
 
179
  play_button.click(fn=play_music, inputs=[], outputs=[background_music, background_music])
180
 
181
+ save_pdf_button.click(fn=generate_pdf_from_html, inputs=[output_recipe], outputs=[pdf_download_link, pdf_download_link])
182
 
183
  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])
184