tywei08 commited on
Commit
4b4e74e
·
verified ·
1 Parent(s): 0aac908

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -37
app.py CHANGED
@@ -4,8 +4,12 @@ import json
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",
@@ -81,53 +85,53 @@ def format_cocktail_output(name, quote, ingredients, instruction, notes):
81
  '''
82
  return html_output
83
 
84
- def save_as_pdf(html_content):
85
- """Converts HTML content to PDF, encodes it in base64, and returns a download link."""
86
- html_path = "output_recipe.html"
87
- pdf_path = "output_recipe.pdf"
88
 
89
- # Write the HTML content to a temporary file
90
- with open(html_path, 'w') as f:
91
- f.write(html_content)
92
 
93
- # Convert HTML to PDF
94
- pdfkit.from_file(html_path, pdf_path)
95
 
96
- # Encode the PDF file in base64
97
- with open(pdf_path, "rb") as pdf_file:
98
- encoded_pdf = base64.b64encode(pdf_file.read()).decode("utf-8")
99
 
100
- # Create a Data URL for the PDF
101
- pdf_data_url = f"data:application/pdf;base64,{encoded_pdf}"
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()
128
 
129
  # Creating the Gradio interface
130
- with gr.Blocks(css=css_styles) as demo:
131
  with gr.Row():
132
  gr.HTML('''
133
  <div style="text-align: center; margin: 0;">
@@ -159,9 +163,9 @@ with gr.Blocks(css=css_styles) as demo:
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)
@@ -183,7 +187,8 @@ with gr.Blocks(css=css_styles) as demo:
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
 
185
  if __name__ == "__main__":
186
- demo.launch(#enable_queue=False,
187
  # Creates an auth screen
188
- auth_message="Welcome! Enter a Username and Password"
 
189
  ).queue()
 
4
  import re
5
  from datetime import datetime
6
  import openai
7
+ # from pyppeteer import launch
8
+ # import tempfile
9
+
10
+ user_db = {
11
+ os.environ["username"]: os.environ["password"],
12
+ }
13
 
14
  music_files = [
15
  "RPReplay_Final1712757356.mp3",
 
85
  '''
86
  return html_output
87
 
88
+ # def save_as_pdf(html_content):
89
+ # """Converts HTML content to PDF, encodes it in base64, and returns a download link."""
90
+ # html_path = "output_recipe.html"
91
+ # pdf_path = "output_recipe.pdf"
92
 
93
+ # # Write the HTML content to a temporary file
94
+ # with open(html_path, 'w') as f:
95
+ # f.write(html_content)
96
 
97
+ # # Convert HTML to PDF
98
+ # pdfkit.from_file(html_path, pdf_path)
99
 
100
+ # # Encode the PDF file in base64
101
+ # with open(pdf_path, "rb") as pdf_file:
102
+ # encoded_pdf = base64.b64encode(pdf_file.read()).decode("utf-8")
103
 
104
+ # # Create a Data URL for the PDF
105
+ # pdf_data_url = f"data:application/pdf;base64,{encoded_pdf}"
106
 
107
+ # # Return HTML anchor tag for the download link
108
+ # return f'<a href="{pdf_data_url}" download="CocktailRecipe.pdf" style="color: white; font-size: 20px;">Download PDF</a>'
109
 
110
+ # def generate_pdf_from_html(html_content):
111
+ # browser = launch()
112
+ # page = browser.newPage()
113
 
114
+ # page.setContent(html_content)
115
 
116
+ # # Create a temporary file to save the PDF
117
+ # with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as tmp_file:
118
+ # pdf_path = tmp_file.name
119
 
120
+ # page.pdf({'path': pdf_path, 'format': 'A4'})
121
 
122
+ # # Close browser
123
+ # browser.close()
124
 
125
+ # # Generate URL for the temporary PDF file
126
+ # pdf_url = f'file://{pdf_path}'
127
 
128
+ # return pdf_url, True
129
 
130
  with open('style.css', 'r') as file:
131
  css_styles = file.read()
132
 
133
  # Creating the Gradio interface
134
+ with gr.Blocks(css=css_styles) as MoodShaker:
135
  with gr.Row():
136
  gr.HTML('''
137
  <div style="text-align: center; margin: 0;">
 
163
  play_button = gr.Button("Play Music", visible=False, elem_classes=["generate-button"], scale=1) # Initially not visible
164
  background_music = gr.Audio(label="Background Music", autoplay=True, visible=False, scale=4) # Initially not visible
165
 
166
+ # with gr.Row():
167
+ # save_pdf_button = gr.Button("Download Recipe as PDF", visible=False)
168
+ # pdf_download_link = gr.File(label="Download Link", visible=False) # For displaying the PDF download link
169
 
170
  def on_generate_click(*args):
171
  recipe, show_play_button, show_save_button = generate_cocktail(*args)
 
187
  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])
188
 
189
  if __name__ == "__main__":
190
+ MoodShaker.launch(#enable_queue=False,
191
  # Creates an auth screen
192
+ auth=lambda u, p: user_db.get(u) == p,
193
+ auth_message="Welcome to MoodShaker! Enter a Username and Password"
194
  ).queue()