-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
119 lines (107 loc) · 5.86 KB
/
main.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
import streamlit as st
from utils import get_topic_data, get_yt_transcript, request_summary_from_gpt3, request_qa_from_gpt3
if 'my_api' not in st.session_state:
st.session_state.my_api_key = None
if 'my_topic' not in st.session_state:
st.session_state.my_topic = None
if 'my_previous_topic' not in st.session_state:
st.session_state.my_previous_topic = ''
if 'my_prompt' not in st.session_state:
st.session_state.my_prompt = ''
if 'my_summary' not in st.session_state:
st.session_state.my_summary = None
if 'my_thumbnails' not in st.session_state:
st.session_state.my_thumbnails = []
if 'my_content_ids' not in st.session_state:
st.session_state.my_content_ids = []
if 'my_raw_summary' not in st.session_state:
st.session_state.my_raw_summary = []
st.markdown("<h1 style='text-align: center; color: red;'>Synopysor-Pro</h1>", unsafe_allow_html=True)
st.markdown("<h4 style='text-align: center; color: black;'>Content summarization tool</h4>", unsafe_allow_html=True)
st.divider()
with st.sidebar:
with st.container():
api_key = st.text_input(
"Enter your OpenAI API",
type="password")
if api_key:
st.session_state.my_api_key = api_key
st.markdown("This is a tool to help you summarise the content of a video with ease")
st.markdown("1. Please enter the topic you want to search for and click on the button to obtain the contents")
st.markdown("2. Once you have obtained the contents, click on the button to start the extraction process")
st.markdown("3. Once the extraction process is completed, the bot will be ready for QnA")
st.markdown("4. Please enter your question and click on the send button to get your answer")
st.markdown(
"5. Please note that the bot will only be able to answer questions related to the topic you have searched")
# topic_input = st.text_input("Enter the topic you want to search the content for: ")
st.session_state.my_topic = st.text_input("Enter the topic you want to search the content for: ")
if st.session_state.my_topic is not None and st.session_state.my_topic != '':
if st.session_state.my_api_key is None or st.session_state.my_api_key == '':
st.error("Please enter your OpenAI API key")
st.stop()
if st.session_state.my_previous_topic != st.session_state.my_topic:
st.session_state.my_previous_topic = st.session_state.my_topic
st.session_state.my_content_ids = []
st.session_state.my_thumbnails = []
st.session_state.my_raw_summary = []
st.session_state.my_summary = None
st.session_state.my_prompt = ''
wcol2 = st.slider("Number of contents to be obtained", min_value=1, max_value=50, value=2, step=1)
if wcol2:
with st.spinner("Searching for the contents..."):
results = get_topic_data(st.session_state.my_topic, wcol2)
if results.__len__() == 0:
st.error("No results found")
st.stop()
st.success("Contents obtained successfully")
st.session_state.my_content_ids = [result['id'] for result in results]
st.session_state.my_thumbnails = [result['thumbnails'] for result in results]
colm1, colm2 = st.columns(2)
colm3, colm4 = st.columns(2)
img_grid = [colm1, colm2, colm3, colm4]
if st.session_state.my_thumbnails.__len__() <= 2:
if st.session_state.my_thumbnails.__len__() == 1:
img_grid = [colm1]
else:
img_grid = [colm1, colm2]
if st.session_state.my_thumbnails.__len__() > 0:
for col in img_grid:
col.image(st.session_state.my_thumbnails[img_grid.index(col)])
if (st.session_state.my_content_ids is not None
and st.session_state.my_content_ids != []):
with st.spinner("Obtaining transcriptions..."):
transcript_datas = get_yt_transcript(st.session_state.my_content_ids)
if transcript_datas.__len__() == 0:
st.error("No transcripts found")
st.stop()
st.success("Transcripts obtained successfully")
if st.button("Summarize", type="primary"):
with st.spinner("Converting transcripts to summaries..."):
for transcript_data in transcript_datas:
st.session_state.my_raw_summary.append(
request_summary_from_gpt3(st.session_state.my_api_key, st.session_state.my_topic,
transcript_data,
1))
st.session_state.my_summary = request_summary_from_gpt3(st.session_state.my_api_key,
st.session_state.my_topic,
''.join(st.session_state.my_raw_summary), 2)
# st.success("Done")
if st.session_state.my_summary is not None and st.session_state.my_summary != '':
st.divider()
st.subheader("💬 Synopysor-Bot")
if "messages" not in st.session_state:
st.session_state["messages"] = [{ "role": "assistant", "content": "How can I help you?" }]
for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])
# Create an input field at the bottom
if prompt := st.chat_input():
st.session_state.my_prompt = prompt
st.session_state.messages.append({ "role": "user", "content": st.session_state.my_prompt })
st.chat_message("user").write(st.session_state.my_prompt)
# Add assistant response to message
with st.spinner("Thinking..."):
response = request_qa_from_gpt3(
st.session_state.my_api_key, st.session_state.my_topic,
st.session_state.my_summary, st.session_state.my_prompt)
st.session_state["messages"].append({ "role": "assistant", "content": response })
st.chat_message("assistant").write(response)