-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenai_journal.py
509 lines (379 loc) · 18.9 KB
/
genai_journal.py
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import streamlit as st
import requests
import json
from groq import Groq
from gtts import gTTS
import io
import base64
import random
import matplotlib.pyplot as plt
import pandas as pd
from openai import OpenAI, AzureOpenAI
# client = Groq(
# api_key=st.secrets["GROQ_KEY"],
# )
client = OpenAI(api_key = st.secrets["OPEANAI_KEY"])
with open("qna_bank.json","r") as file:
question_bank = json.load(file)
# Initialize session state for questions
if "questions" not in st.session_state:
q_index = random.randint(0, len(question_bank) - 1)
st.session_state.questions = question_bank[q_index]
questions = st.session_state.questions
# Function to simulate data storage
def store_responses(question_index, user_response, free_text):
if "responses" not in st.session_state:
st.session_state["responses"] = []
# Check if the response for this question index is already stored and update it if needed
response_found = False
for i, (idx, _, _) in enumerate(st.session_state["responses"]):
if idx == question_index:
st.session_state["responses"][i] = (question_index, user_response, free_text)
response_found = True
break
# If response not found, add it
if not response_found:
st.session_state["responses"].append((question_index, user_response, free_text))
# Function to generate script
def generate_script(qna_pairs):
qna_pairs_string = json.dumps(qna_pairs)
query = """
below are the few details for user's that day's activity, so please create it super creative & uniqe 30-60 seconds script , it should only contains text which user can directly read & speak. Or with bot can directly convert from text to speech. Don't add anything except script in output. User is going to do voice over with this, so use I instead of You.
QnA:
%s
output formate:
{
"script" : <script>
}
""" % (qna_pairs_string)
# chat_completion = client.chat.completions.create(
# messages=[
# {
# "role": "user",
# "content": query,
# }
# ],
# model="llama3-8b-8192",
# )
# script = chat_completion.choices[0].message.content
# script = script.split('"')[1]
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": query},
]
)
script = json.loads(response.choices[0].message.content)
script = script["script"]
return script
# Function to simulate text-to-audio conversion
def text_to_audio(script):
# Simulate text-to-audio conversion
tts = gTTS(text=script, lang='en', slow=False)
tts.save("day1.mp3")
def genai_result(prompt):
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
)
return response.choices[0].message.content
def calculate_ikigai_score(passion, mission, profession, vocation):
return passion + mission + profession + vocation
def display_ikigai_score(score):
st.subheader(f"Your Ikigai Score: {score}")
passion = st.session_state["report_data"].get("passion")
mission = st.session_state["report_data"].get("mission")
profession = st.session_state["report_data"].get("profession")
vocation = st.session_state["report_data"].get("vocation")
# Create a data frame for the pie chart
data = pd.DataFrame({'Category': ['Passion', 'Mission', 'Profession', 'Vocation'],
'Score': [passion, mission, profession, vocation]})
# Create the pie chart
fig, ax = plt.subplots()
ax.pie(data['Score'], labels=data['Category'], autopct='%1.1f%%')
ax.axis('equal') # Ensure the pie chart is circular
# Display the pie chart in Streamlit
st.pyplot(fig)
# Function to display the report
def display_report():
ikigai_score = st.session_state["report_data"].get("ikigai_score")
passion = st.session_state["report_data"].get("passion")
mission = st.session_state["report_data"].get("mission")
profession = st.session_state["report_data"].get("profession")
vocation = st.session_state["report_data"].get("vocation")
actionable_steps_for_tomorrow = st.session_state["actionable_steps_for_tomorrow"]
st.title("Ikigai Scoring")
# Display Passion, Mission, Profession, and Vocation scores
# Get user inputs
passion = st.slider("Passion", 0, 10, int(passion))
mission = st.slider("Mission", 0, 10, int(mission))
profession = st.slider("Profession", 0, 10, int(profession))
vocation = st.slider("Vocation", 0, 10, int(vocation))
# Display Ikigai score and pie chart if available
if ikigai_score is not None:
display_ikigai_score(ikigai_score)
# Display actionable steps if available
if actionable_steps_for_tomorrow:
with st.expander("Actionable Steps for Tomorrow:"):
st.write(actionable_steps_for_tomorrow)
# Function to navigate to a different section
def navigate_to_section(section):
st.session_state["selected_section"] = section
st.experimental_rerun()
# Initialize session state
if "responses" not in st.session_state:
st.session_state["responses"] = []
if "current_question" not in st.session_state:
st.session_state["current_question"] = 0
# Left Panel with sections
st.sidebar.title("GenAI Journal")
# Initialize the session state for the button label
if "script_button_label" not in st.session_state:
st.session_state.script_button_label = "Generate Script"
# Initialize the session state for the generated script
if "generated_script" not in st.session_state:
st.session_state.generated_script = ""
# Initialize session state variables if they don't exist
if "report_generated" not in st.session_state:
st.session_state["report_generated"] = False
if "report_data" not in st.session_state:
st.session_state["report_data"] = {}
if "actionable_steps_for_tomorrow" not in st.session_state:
st.session_state["actionable_steps_for_tomorrow"] = ""
# Initialize selected_section with the stored value if it exists, otherwise use the radio button
if "selected_section" not in st.session_state:
st.session_state["selected_section"] = "Intro"
# Use the stored value to set the default selection in the radio button
selected_section = st.sidebar.radio(
"Select a Section",
["Intro","Question - Answering", "Script generation", "Convert Script to Audio", "Create Video", "Report"],
index=["Intro","Question - Answering", "Script generation", "Convert Script to Audio", "Create Video", "Report"].index(st.session_state["selected_section"])
)
# # Update the session state with the new selection
# st.session_state["selected_section"] = selected_section
# Section 1: Intro
if selected_section == "Intro":
st.sidebar.header("1. Intro")
st.header("Welcome to the GenAI Journal!")
st.write("""
* Revolutionize your journaling experience with cutting-edge AI technology
* Engage in daily conversations with a thoughtful AI assistant about your day
* Receive a personalized multimedia journal entry in video format, complete with audio and visuals
* Gain valuable insights into your productivity levels and track your progress over time
* Discover tools and guidance to help you unlock your ikigai (life's purpose)
* Option for personalized mentorship to stay motivated and achieve your goals
* #75harddaychallenge video can be created in few clicks.
""")
if st.button("Get Started"):
navigate_to_section("Question - Answering")
pass
# Section 1: Question - Answering
elif selected_section == "Question - Answering":
st.sidebar.header("2. Question - Answering")
# Display current question
def display_question(question_index):
question = questions[question_index]
# Display progress bar
progress = st.progress((question_index + 1) / len(questions))
st.subheader(question["question"])
# Check if there's already a stored response for this question index
stored_response = next((response for idx, response, _ in st.session_state["responses"] if idx == question_index), None)
default_index = question["options"].index(stored_response) if stored_response in question["options"] else 0
user_response = st.radio("", question["options"], index=default_index, key=f"question_{question_index}")
st.subheader("Any explanation or additional input?")
# Check if there's already a stored free text response for this question index
stored_text = next((text for idx, _, text in st.session_state["responses"] if idx == question_index), "")
free_text = st.text_area("", value=stored_text, key=f"text_{question_index}", height=30)
# Store responses immediately
store_responses(question_index, user_response, free_text)
return user_response, free_text
if "responses" in st.session_state and st.session_state["responses"]:
qna_pair = []
for question_index, response, text in st.session_state["responses"]:
temp = {"questions" : questions[question_index]["question"],
"answer" : response,
"extra_inputs" : text }
qna_pair.append(temp)
json_string = json.dumps(qna_pair)
# Navigation buttons
col1, col2, col3 = st.columns([1, 1, 1])
with col1:
if st.session_state["current_question"] > 0:
if st.button("Previous"):
st.session_state["current_question"] -= 1
with col3:
if st.session_state["current_question"] < len(questions) - 1:
if st.button("Next"):
st.session_state["current_question"] += 1
with col2:
if st.session_state["current_question"] == len(questions) - 1:
# st.download_button(
# label="Download JSON",
# file_name="data.json",
# mime="application/json",
# data=json_string,
# )
if st.button("Submit"):
st.session_state["current_question"] += 1
# st.text(json_string)
# Display current question
if st.session_state["current_question"] < len(questions):
current_question_index = st.session_state["current_question"]
display_question(current_question_index)
else:
st.subheader("Thank you for answering all the questions!")
if st.button("Go To Generate Script"):
navigate_to_section("Script generation")
# st.session_state["selected_section"] = "Script generation"
# Section 2: Script generation from previously answered questions
elif selected_section == "Script generation":
st.sidebar.header("4. Script generation from previously answered questions")
if st.button(st.session_state.script_button_label):
with st.spinner("Generating script..."):
if "responses" in st.session_state and st.session_state["responses"]:
qna_pair = []
for question_index, response, text in st.session_state["responses"]:
temp = {"questions" : questions[question_index]["question"],
"answer" : response,
"extra_inputs" : text }
qna_pair.append(temp)
st.session_state.generated_script = generate_script(qna_pair)
# st.write(st.session_state.generated_script)
st.session_state.script_button_label = "Re-generate Script"
else:
st.write("Answer some questions to generate the script.")
if st.session_state.generated_script:
st.subheader("Generated Script:")
st.write(st.session_state.generated_script)
if st.button("Go to Convert Script to Audio"):
navigate_to_section("Convert Script to Audio")
# st.session_state["selected_section"] = "Convert Script to Audio"
# Section 3: Convert Script to Audio
elif selected_section == "Convert Script to Audio":
st.sidebar.header("5. Convert Script to Audio")
if st.session_state["responses"] and st.session_state.generated_script :
if st.button("Convert Script to Audio"):
with st.spinner("Generating Audio..."):
audio_content = text_to_audio(st.session_state.generated_script)
st.audio("day1.mp3")
st.session_state["audio_file"] = audio_content
if st.button("Go to Create Video"):
st.session_state["selected_section"] = "Create Video"
st.experimental_rerun()
else:
st.write("Answer some questions to generate the script and convert it to audio.")
if st.session_state.get("audio_file"):
if st.button("Go to Create Video"):
# Update the selected section and force rerun
# st.session_state["selected_section"] = "Create Video"
navigate_to_section("Create Video")
# Section 4: Create Video from Audio & lip sync api
elif selected_section == "Create Video":
st.sidebar.header("6. Create Video from Audio & lip sync api")
if st.session_state["responses"]:
# if st.button("Create Video"):
# # video creation
# pass
# Define a list of avatar video options
avatar_videos = {
"Avatar 1": "./avatar_video/avatar1.mp4",
"Avatar 2": "./avatar_video/avatar2.mp4",
"Avatar 3": "./avatar_video/avatar3.mp4",
"Avatar 4": "./avatar_video/avatar4.mp4",
# "Avatar 5": "avatar5.mp4"
}
# Create two columns
col1, col2 = st.columns(2)
# Display the select box in the first column
with col1:
selected_avatar = st.selectbox("Select an Avatar", list(avatar_videos.keys()), index=0)
# Display the avatar video in the second column
with col2:
pass
else:
st.write("Answer some questions to generate the script and create the video.")
# Section 4: Create Report
elif selected_section == "Report":
st.sidebar.header("7. Create Report")
if "responses" in st.session_state and st.session_state["responses"]:
qna_pair = []
for question_index, response, text in st.session_state["responses"]:
temp = {"questions" : questions[question_index]["question"],
"answer" : response,
"extra_inputs" : text }
qna_pair.append(temp)
json_string = json.dumps(qna_pair)
# Check if the report is already generated
if st.session_state["report_generated"]:
display_report()
else:
if st.session_state["responses"] :
create_report_button = st.button("Create Report", disabled=st.session_state["report_generated"])
if create_report_button:
# Your logic to generate the report
with st.spinner("Generating report for your ikigai..."):
qna = json_string
prompt = """
Below are the qna collected from user for his daily activity.
QnA:
%s
---------------------------------------------------------------
Want results in below json form. just need overall score not for each individual question. And share only json form result nothing else in result. All score is out of 10.
{
'Passion' : <passion score>,
'Mission' : <mission score>,
'Profession' : <profession score>,
'Vocation': <vocation score>
}
""" %(qna)
# Genai Result
report_result = genai_result(prompt)
prompt = f""" Please give only json result only from below text \n\n {report_result}"""
report_result = json.loads(genai_result(prompt))
# Get user inputs
passion = int(report_result["Passion"])
mission = int(report_result["Mission"])
profession = int(report_result["Profession"])
vocation = int(report_result["Vocation"])
# Calculate Ikigai score
ikigai_score = calculate_ikigai_score(passion, mission, profession, vocation)
# After generating the report, store the data in st.session_state
st.session_state["report_data"] = {
"ikigai_score": ikigai_score,
"passion": passion,
"mission": mission,
"profession": profession,
"vocation": vocation,
}
qna = json_string
tomorrow_prompt = f"""
Below are the qna collected from user for his daily activity.
QnA:
{qna}
---------------------------------------------
Please I am trying to achieve ikigai, so please help provide a way what can I do better tomorrow from my today's routine. Instead of giving generlize answer give some specific & personalize answer. I need few points 4-5 catagories, :
Give me only results in some catagories without adding any extra text.
output formate:
1. catagory1:
- <suggestion>
- <suggestion>
.
.
2. catagory2:
.
.
.
.
.
"""
actionable_steps_for_tomorrow = genai_result(tomorrow_prompt)
# After generating the report, store the data in st.session_state
st.session_state["actionable_steps_for_tomorrow"]= actionable_steps_for_tomorrow
st.session_state["report_generated"] = True
display_report()
else:
st.write("Answer some questions to generate the script and create the video.")