amoldwalunj commited on
Commit
421a7f9
·
1 Parent(s): 812dbef

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +180 -0
app.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from fpdf import FPDF
4
+ import base64
5
+ import openai
6
+ from streamlit_quill import st_quill
7
+ import os
8
+
9
+ os.environ["OPENAI_API_KEY"] = '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'
10
+ openai.api_key = os.getenv("OPENAI_API_KEY")
11
+
12
+ def get_chatgpt_response(messages):
13
+ response = openai.ChatCompletion.create(
14
+ model="gpt-3.5-turbo",
15
+ messages=messages
16
+ )
17
+ return response['choices'][0]['message']['content']
18
+
19
+ def process_text(inputs):
20
+ # Perform some processing on the input dictionary here
21
+ # For example, you could concatenate all the inputs into a single string
22
+ processed_text = ""
23
+ for key, value in inputs.items():
24
+ processed_text += f"{key}: {value}\n\n"
25
+
26
+ info= str(processed_text)
27
+
28
+ messages=[
29
+ {"role": "system", "content": "You are a obituary writer given information in the from of json"},
30
+ {"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)}
31
+ ]
32
+
33
+ model_response = get_chatgpt_response(messages)
34
+
35
+ return model_response
36
+
37
+ # def save_as_pdf(text):
38
+ # pdf = FPDF()
39
+ # pdf.add_page()
40
+ # pdf.set_font("Arial", size=12)
41
+ # encoded_text = text.encode('latin-1', 'replace').decode('latin-1')
42
+ # pdf.multi_cell(0, 10, txt=encoded_text)
43
+ # file_name = "output.pdf"
44
+ # pdf.output(file_name)
45
+ # with open(file_name, "rb") as pdf_file:
46
+ # b64 = base64.b64encode(pdf_file.read()).decode('utf-8')
47
+ # href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_name}">Download PDF</a>'
48
+ # st.markdown(href, unsafe_allow_html=True)
49
+
50
+ import base64
51
+ from io import BytesIO
52
+ from weasyprint import HTML
53
+
54
+ def save_as_pdf(text):
55
+ # convert quill editor HTML to PDF
56
+ pdf_file = BytesIO()
57
+ HTML(string=text).write_pdf(pdf_file)
58
+
59
+ # encode the PDF to base64
60
+ b64 = base64.b64encode(pdf_file.getvalue()).decode('utf-8')
61
+
62
+ # generate a download link for the PDF
63
+ href = f'<a href="data:application/octet-stream;base64,{b64}" download="output.pdf">Download PDF</a>'
64
+ st.markdown(href, unsafe_allow_html=True)
65
+
66
+
67
+ def form_page():
68
+
69
+ st.markdown("### Your custom obituary writer :pencil:")
70
+ st.markdown("### Please fill the details to get custom obituary")
71
+
72
+ inputs = {
73
+ "Full name, including any nicknames": "",
74
+ "Age at the time of passing": "",
75
+ "Date and place of birth": "",
76
+ "Date and place of passing": "",
77
+ "Family members who have preceded them in death": "",
78
+ "Surviving family members": "",
79
+ "Education, career, hobbies and interests": "",
80
+ "Accomplishments or contributions they made to their community or society": "",
81
+ "Personal traits that made them special": "",
82
+ "Service or memorial arrangements": "",
83
+ "Any personal message or special requests you would like to include": "",
84
+ "Additional information, like childhood or special bond": "",
85
+ }
86
+
87
+ for key in inputs:
88
+ inputs[key] = st.text_input(key)
89
+
90
+ if st.button("Process"):
91
+ output_text = process_text(inputs)
92
+ st.session_state.output_text = output_text
93
+ st.write(output_text)
94
+ st.session_state.form = True
95
+
96
+ if st.session_state.form:
97
+ if st.button("I want to add more information"):
98
+ additional_info = st.text_input("What else would you like to add?")
99
+ if additional_info:
100
+ message = f"{additional_info}"
101
+ output_text = get_chatgpt_response(message)
102
+ st.session_state.output_text += "\n" + output_text
103
+ st.write(st.session_state.output_text)
104
+
105
+ if st.button("I want to export and edit manually"):
106
+ st.session_state.export_manually = True
107
+ st.session_state.form = False
108
+ st.experimental_rerun()
109
+
110
+ # def editor_page():
111
+ # st.write("Use the editor below to edit the obituary:")
112
+ # quill_text = st.session_state.output_text
113
+ # edited_text = st_quill(quill_text)
114
+ # st.write("Here is the edited obituary:")
115
+ # st.write(edited_text)
116
+
117
+ def editor_page():
118
+ #st.markdown("### Editor :smile:")
119
+ st.markdown("<h1 style='text-align: center; color: Red;'>Editor</h1>", unsafe_allow_html=True)
120
+
121
+ st.write("Use the editor below to edit the obituary:")
122
+
123
+
124
+ quill_text = st.session_state.output_text
125
+ edited_text = st_quill(quill_text)
126
+
127
+
128
+ st.write("Here is the edited obituary:")
129
+ #st.write(edited_text)
130
+
131
+ st.session_state.edited_text= edited_text
132
+
133
+ if st.button("Save as PDF"):
134
+ # Save the output text as a PDF
135
+ #save_as_pdf(st.session_state.output_text)
136
+ save_as_pdf(st.session_state.edited_text)
137
+ st.write("The custom obituary has been saved as a PDF.")
138
+
139
+ # Add some custom CSS to style the editor
140
+ st.markdown("""
141
+ <style>
142
+ #toolbar {
143
+ background-color: #f3f3f3;
144
+ border-radius: 5px;
145
+ padding: 5px;
146
+ }
147
+ .ql-container {
148
+ border-radius: 5px;
149
+ border: 1px solid #ccc;
150
+ height: 400px;
151
+ }
152
+ .ql-editor {
153
+ height: 100%;
154
+ }
155
+ </style>
156
+ """, unsafe_allow_html=True)
157
+
158
+
159
+
160
+
161
+ PAGES = {
162
+ "Form Page": form_page,
163
+ "Editor Page": editor_page,
164
+ }
165
+
166
+ def app():
167
+ if "form" not in st.session_state:
168
+ st.session_state.form = False
169
+ if "export_manually" not in st.session_state:
170
+ st.session_state.export_manually = False
171
+
172
+ if not st.session_state.export_manually:
173
+ st.sidebar.radio("",["form"])
174
+ form_page()
175
+ else:
176
+ st.sidebar.radio("",["Editor"])
177
+ editor_page()
178
+
179
+ if __name__ == "__main__":
180
+ app()