import streamlit as st from fpdf import FPDF import base64 import openai from streamlit_quill import st_quill import os from io import BytesIO #from weasyprint import HTML import tempfile import weasyprint from streamlit_quill import st_quill_component os.environ["OPENAI_API_KEY"] = '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' openai.api_key = os.getenv("OPENAI_API_KEY") def get_chatgpt_response(messages): response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) return response['choices'][0]['message']['content'] def process_text(inputs): # Perform some processing on the input dictionary here # For example, you could concatenate all the inputs into a single string processed_text = "" for key, value in inputs.items(): processed_text += f"{key}: {value}\n\n" info= str(processed_text) messages=[ {"role": "system", "content": "You are a obituary writer given information in the from of json"}, {"role": "user", "content": "Please write custom obituary based on this information. Make sure you write little long obituary using the information provided. Here is information: \n{}".format(info)} ] model_response = get_chatgpt_response(messages) return model_response # def save_as_pdf(text): # pdf = FPDF() # pdf.add_page() # pdf.set_font("Arial", size=12) # encoded_text = text.encode('latin-1', 'replace').decode('latin-1') # pdf.multi_cell(0, 10, txt=encoded_text) # file_name = "output.pdf" # pdf.output(file_name) # with open(file_name, "rb") as pdf_file: # b64 = base64.b64encode(pdf_file.read()).decode('utf-8') # href = f'Download PDF' # st.markdown(href, unsafe_allow_html=True) # import base64 # from io import BytesIO # from weasyprint import HTML # #from streamlit_quill import Delta, Renderer # from quill.delta import Delta # def save_as_pdf(text): # # convert quill editor HTML to PDF # pdf_file = BytesIO() # HTML(string=text).write_pdf(pdf_file, stylesheets=[CSS(string=""" # .ql-editor { # font-size: 16px; # line-height: 1.6; # } # """)], style_tags=True) # # encode the PDF to base64 # b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8') # # generate a download link for the PDF # href = f'Download PDF' # st.markdown(href, unsafe_allow_html=True) import tempfile import weasyprint from bs4 import BeautifulSoup import pdfkit def save_as_pdf(text): # Save the edited text as a temporary HTML file with open("temp.html", "w", encoding="utf-8") as f: f.write(text) # Configure options for PDF generation options = { "page-size": "Letter", "margin-top": "0.75in", "margin-right": "0.75in", "margin-bottom": "0.75in", "margin-left": "0.75in", "encoding": "UTF-8", "no-outline": None } # Generate the PDF from the HTML file pdfkit.from_file("temp.html", "output.pdf", options=options) # Read the generated PDF file with open("output.pdf", "rb") as f: pdf_data = f.read() # Encode the PDF data to base64 b64 = base64.b64encode(pdf_data).decode("utf-8") # Generate a download link for the PDF href = f'Download PDF' st.markdown(href, unsafe_allow_html=True) # Delete the temporary HTML and PDF files os.remove("temp.html") os.remove("output.pdf") def form_page(): st.markdown("### Your custom obituary writer :pencil:") st.markdown("### Please fill the details to get custom obituary") inputs = { "Full name, including any nicknames": "", "Age at the time of passing": "", "Date and place of birth": "", "Date and place of passing": "", "Family members who have preceded them in death": "", "Surviving family members": "", "Education, career, hobbies and interests": "", "Accomplishments or contributions they made to their community or society": "", "Personal traits that made them special": "", "Service or memorial arrangements": "", "Any personal message or special requests you would like to include": "", "Additional information, like childhood or special bond": "", } for key in inputs: inputs[key] = st.text_input(key) if st.button("Process"): output_text = process_text(inputs) st.session_state.output_text = output_text st.write(output_text) st.session_state.form = True if st.session_state.form: if st.button("I want to add more information"): additional_info = st.text_input("What else would you like to add?") if additional_info: message = f"{additional_info}" output_text = get_chatgpt_response(message) st.session_state.output_text += "\n" + output_text st.write(st.session_state.output_text) if st.button("I want to export and edit manually"): st.session_state.export_manually = True st.session_state.form = False st.experimental_rerun() # def editor_page(): # #st.markdown("### Editor :smile:") # st.markdown("

Editor

", unsafe_allow_html=True) # st.write("Use the editor below to edit the obituary:") # quill_text = st.session_state.output_text # edited_text = st_quill(quill_text) # st.write("Here is the edited obituary:") # #st.write(edited_text) # st.session_state.edited_text= edited_text # if st.button("Save as PDF"): # # Save the output text as a PDF # #save_as_pdf(st.session_state.output_text) # save_as_pdf(st.session_state.edited_text) # st.write("The custom obituary has been saved as a PDF.") # # Add some custom CSS to style the editor # st.markdown(""" # # """, unsafe_allow_html=True) #from quill.delta import Delta, Renderer def editor_page(): st.markdown("

Editor

", unsafe_allow_html=True) st.write("Use the editor below to edit the obituary:") quill_text = st.session_state.output_text edited_text = st_quill(quill_text) st.write("Here is the edited obituary:") #st.write(edited_text) st.session_state.edited_text = edited_text if st.button("Save as PDF"): # Save the edited text as a PDF save_as_pdf(edited_text.get("content")) st.write("The custom obituary has been saved as a PDF.") PAGES = { "Form Page": form_page, "Editor Page": editor_page, } def app(): if "form" not in st.session_state: st.session_state.form = False if "export_manually" not in st.session_state: st.session_state.export_manually = False if not st.session_state.export_manually: st.sidebar.radio("",["form"]) form_page() else: st.sidebar.radio("",["Editor"]) editor_page() if __name__ == "__main__": app()