Spaces:
Runtime error
Runtime error
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 | |
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'<a href="data:application/octet-stream;base64,{b64}" download="{file_name}">Download PDF</a>' | |
# 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'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>' | |
# st.markdown(href, unsafe_allow_html=True) | |
def save_as_pdf(text): | |
# convert quill editor HTML to PDF | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".html") as tmpfile: | |
tmpfile.write(text.encode("utf-8")) | |
tmpfile.flush() | |
pdf_file = HTML(tmpfile.name).write_pdf() | |
# encode the PDF to base64 | |
b64 = base64.b64encode(pdf_file).decode('utf-8') | |
# generate a download link for the PDF | |
href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>' | |
st.markdown(href, unsafe_allow_html=True) | |
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("<h1 style='text-align: center; color: Red;'>Editor</h1>", 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(""" | |
# <style> | |
# #toolbar { | |
# background-color: #f3f3f3; | |
# border-radius: 5px; | |
# padding: 5px; | |
# } | |
# .ql-container { | |
# border-radius: 5px; | |
# border: 1px solid #ccc; | |
# height: 400px; | |
# } | |
# .ql-editor { | |
# height: 100%; | |
# } | |
# </style> | |
# """, unsafe_allow_html=True) | |
#from quill.delta import Delta, Renderer | |
def editor_page(): | |
st.markdown("<h1 style='text-align: center; color: Red;'>Editor</h1>", 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.edited_text) | |
st.write("The custom obituary has been saved as a PDF.") | |
# Add some custom CSS to style the editor | |
st.markdown(""" | |
<style> | |
#toolbar { | |
background-color: #f3f3f3; | |
border-radius: 5px; | |
padding: 5px; | |
} | |
.ql-container { | |
border-radius: 5px; | |
border: 1px solid #ccc; | |
height: 400px; | |
} | |
.ql-editor { | |
height: 100%; | |
} | |
{edited_text.get('css')} | |
</style> | |
""", unsafe_allow_html=True) | |
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() |