-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatYT.py
192 lines (159 loc) · 6.23 KB
/
ChatYT.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
from deepgram import Deepgram
import json
import youtube_dl
import streamlit as st
from languages import languages
from itranslate import itranslate as itrans
from pathlib import Path
from fpdf import FPDF
import base64
from streamlit_chat import message
import requests
def YTapp():
st.header("CogniTube")
st.caption("Cognitive + YouTube")
DEEPGRAM_API_KEY = st.secrets["DEEPGRAM_API_KEY"]
AI21_API_KEY = st.secrets["AI21_API_KEY"]
PATH_TO_FILE = ''
def get_questions(transcript):
questions_response = requests.post("https://api.ai21.com/studio/v1/experimental/j1-grande-instruct/complete",
headers={"Authorization": "Bearer " + AI21_API_KEY},
json={
"prompt": "Create a list of questions from the given context\nContext: " + transcript + "\nQuestions:",
"numResults": 1,
"maxTokens": 200,
"temperature": 0.7,
"topKReturn": 0,
"topP":1,
"countPenalty": {
"scale": 0,
"applyToNumbers": False,
"applyToPunctuations": False,
"applyToStopwords": False,
"applyToWhitespaces": False,
"applyToEmojis": False
},
"frequencyPenalty": {
"scale": 0,
"applyToNumbers": False,
"applyToPunctuations": False,
"applyToStopwords": False,
"applyToWhitespaces": False,
"applyToEmojis": False
},
"presencePenalty": {
"scale": 0,
"applyToNumbers": False,
"applyToPunctuations": False,
"applyToStopwords": False,
"applyToWhitespaces": False,
"applyToEmojis": False
},
"stopSequences":[]
}
)
return questions_response.json()
def get_summary(transcript):
summary_response = requests.post("https://api.ai21.com/studio/v1/experimental/summarize",
headers={"Authorization": "Bearer "+ AI21_API_KEY},
json={
"text": transcript,
}
)
return summary_response.json()
def get_answer(transcript, user_input):
response = requests.post("https://api.ai21.com/studio/v1/experimental/j1-grande-instruct/complete",
headers={"Authorization": "Bearer " + AI21_API_KEY },
json={
"prompt": transcript + "\nQ: " + user_input + "\nA:",
"numResults": 1,
"maxTokens": 10,
"temperature": 0,
"topKReturn": 0,
"topP":1,
"countPenalty": {
"scale": 0,
"applyToNumbers": False,
"applyToPunctuations": False,
"applyToStopwords": False,
"applyToWhitespaces": False,
"applyToEmojis": False
},
"frequencyPenalty": {
"scale": 0,
"applyToNumbers": False,
"applyToPunctuations": False,
"applyToStopwords": False,
"applyToWhitespaces": False,
"applyToEmojis": False
},
"presencePenalty": {
"scale": 0,
"applyToNumbers": False,
"applyToPunctuations": False,
"applyToStopwords": False,
"applyToWhitespaces": False,
"applyToEmojis": False
},
"stopSequences":["↵↵"]
}
)
return response.json()
@st.cache
def download_video(link):
videoinfo = youtube_dl.YoutubeDL().extract_info(url = link, download=False)
filename = f"{videoinfo['id']}.mp3"
options = {
'format': 'bestaudio/best',
'keepvideo': False,
'outtmpl': filename,
}
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([videoinfo['webpage_url']])
base = Path.cwd()
PATH_TO_FILE = f"{base}/{filename}"
return PATH_TO_FILE
@st.cache
def transcribe(PATH_TO_FILE):
# Initializes the Deepgram SDK
deepgram = Deepgram(DEEPGRAM_API_KEY)
# Open the audio file
with open(PATH_TO_FILE, 'rb') as audio:
# ...or replace mimetype as appropriate
source = {'buffer': audio, 'mimetype': 'audio/wav'}
response = deepgram.transcription.sync_prerecorded(source, {'summarize': True, 'punctuate': True, "diarize": True, "utterances": True, "detect_topics": True, "numerals": True})
# response_result = json.dumps(response, indent=4)
return response
@st.cache
def translate(text, to_lang):
return itrans(text, to_lang = to_lang)
link = st.text_input("Enter the YT URL", value="https://youtu.be/JYs_94znYy0")
st.video(link)
PATH_TO_FILE = download_video(link)
response = transcribe(PATH_TO_FILE)
tab1, tab2, tab3, tab4 = st.tabs(["Summary", "Chat", "Translate", "Questions"])
with tab1:
transcript = response["results"]["channels"][0]["alternatives"][0]["transcript"]
summary_response = get_summary(transcript)
summary = summary_response["summaries"][0]["text"]
with st.expander("TL;DW"):
st.write(summary, expanded=True)
with st.expander("Transcript", expanded=False):
st.write(transcript)
with tab2:
user_input = st.text_input("You: ","What does the context conveys?")
if user_input:
res = get_answer(transcript, user_input)
# st.write(res)
answer = res["completions"][0]["data"]["text"]
message(user_input, is_user=True)
message(answer)
with tab3:
to_lang = st.selectbox("Select the language", languages.values())
dest = list(languages.keys())[list(languages.values()).index(to_lang)]
st.write("language: ", dest)
st.write(translate(transcript, dest))
with tab4:
questions_response = get_questions(transcript)
# st.write(questions_response)
st.markdown(questions_response["completions"][0]["data"]["text"])