Spaces:
Runtime error
Runtime error
Commit
·
b9c76ce
1
Parent(s):
2258bf8
app
Browse filesapp test
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import whisper
|
2 |
+
from pytube import YouTube
|
3 |
+
from langchain.chat_models import ChatOpenai
|
4 |
+
import os
|
5 |
+
|
6 |
+
key = st.text_input("Provide Openai API Key to Chat")
|
7 |
+
@st.cache_resource
|
8 |
+
def get_whisper():
|
9 |
+
model = whisper.load_model("base")
|
10 |
+
return model
|
11 |
+
|
12 |
+
def get_audio(url):
|
13 |
+
yt = YouTube(url)
|
14 |
+
video = yt.streams.filter(only_audio=True).first()
|
15 |
+
out_file=video.download(output_path=".")
|
16 |
+
base, ext = os.path.splitext(out_file)
|
17 |
+
new_file = base+'.mp3'
|
18 |
+
os.rename(out_file, new_file)
|
19 |
+
a = new_file
|
20 |
+
return a
|
21 |
+
|
22 |
+
def get_text(url):
|
23 |
+
model = get_whisper()
|
24 |
+
result = model.transcribe(get_audio(url))
|
25 |
+
return result['text']
|
26 |
+
|
27 |
+
def get_youtube_title(y_url):
|
28 |
+
yt = Youtube(y_url)
|
29 |
+
embed_url = f"https://wwww.youtube.com/embed/{yt.video_id}"
|
30 |
+
emded_html = f'<iframe src>"{embed_url}" frameborder="0" allowfullscreen></iframe>'
|
31 |
+
return yt.title, emded_html
|
32 |
+
|
33 |
+
y_url = st.sidebar.text_input("Enter youtube url")
|
34 |
+
if st.sidebar.button('Get Youtube Content'):
|
35 |
+
if st.spinner('Loading...'):
|
36 |
+
video_title, embed_html = get_video_title(y_url)
|
37 |
+
st.markdown(f'## {video_title}')
|
38 |
+
st.markdown(embed_html, unsafe_allow_html=True)
|
39 |
+
content = get_text(y_url)
|
40 |
+
st.session_state.content = content
|
41 |
+
|
42 |
+
template = """You are Adance AI that respond on given instruction based on available content
|
43 |
+
content: {content}
|
44 |
+
instruction: {prompt}
|
45 |
+
"""
|
46 |
+
|
47 |
+
if 'content' in st.session_state:
|
48 |
+
template = template.format(content = st.session_state.content, prompt = '{prompt}')
|
49 |
+
else:
|
50 |
+
template = template.format(content = '', prompt = '{prompt}')
|
51 |
+
|
52 |
+
if prompt := st.chat_input():
|
53 |
+
if not key:
|
54 |
+
st.markdown('Provide key to chat')
|
55 |
+
st.stop()
|
56 |
+
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
57 |
+
st.chat_message('user').write(prompt)
|
58 |
+
llm = ChatOpenAI(model_name = 'gpt-3.5-turbo', openai_api_key = key)
|
59 |
+
msg = llm.predict(template.format(prompt = prompt))
|
60 |
+
st.session_state.messages.append({'role': 'assistant', 'content': msg})
|
61 |
+
st.chat_message('assistant').write(msg)
|
62 |
+
|