llm_tube / app.py
statisticalplumber's picture
changes of session message
eb3b875
raw
history blame
No virus
2.12 kB
import whisper
from pytube import YouTube
from langchain.chat_models import ChatOpenAI
import os
import streamlit as st
key = st.sidebar.text_input("Provide Openai API Key to Chat", type = 'password')
@st.cache_resource
def get_whisper():
model = whisper.load_model("base")
return model
def get_audio(url):
yt = YouTube(url)
video = yt.streams.filter(only_audio=True).first()
out_file=video.download(output_path=".")
base, ext = os.path.splitext(out_file)
new_file = 'input_audio'+'.mp3'
os.rename(out_file, new_file)
a = new_file
return a
def get_text(url):
model = get_whisper()
result = model.transcribe(get_audio(url))
return result['text']
def get_youtube_title(y_url):
yt = YouTube(y_url)
embed_url = f"https://wwww.youtube.com/embed/{yt.video_id}"
emded_html = f'<iframe src>"{embed_url}" frameborder="0" allowfullscreen></iframe>'
return yt.title, emded_html
y_url = st.sidebar.text_input("Enter youtube url")
if st.sidebar.button('Get Youtube Content'):
if st.spinner('Loading...'):
video_title, embed_html = get_youtube_title(y_url)
st.markdown(f'## {video_title}')
st.markdown(embed_html, unsafe_allow_html=True)
content = get_text(y_url)
st.session_state.content = content
template = """You are Adance AI that respond on given instruction based on available content
content: {content}
instruction: {prompt}
"""
if 'content' in st.session_state:
template = template.format(content = st.session_state.content, prompt = '{prompt}')
else:
template = template.format(content = '', prompt = '{prompt}')
if prompt := st.chat_input():
if not key:
st.markdown('Provide key to chat')
st.stop()
# st.session_state.messages.append({'role': 'user', 'content': prompt})
st.chat_message('user').write(prompt)
llm = ChatOpenAI(model_name = 'gpt-3.5-turbo', openai_api_key = key)
msg = llm.predict(template.format(prompt = prompt))
# st.session_state.messages.append({'role': 'assistant', 'content': msg})
st.chat_message('assistant').write(msg)