Spaces:
Runtime error
Runtime error
File size: 5,808 Bytes
421a7f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
import streamlit as st
from fpdf import FPDF
import base64
import openai
from streamlit_quill import st_quill
import os
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
def save_as_pdf(text):
# convert quill editor HTML to PDF
pdf_file = BytesIO()
HTML(string=text).write_pdf(pdf_file)
# 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 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.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)
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)
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() |