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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.llms import LlamaCpp
|
| 2 |
+
from langchain.vectorstores import FAISS
|
| 3 |
+
from langchain.prompts import PromptTemplate
|
| 4 |
+
from langchain.docstore.document import Document
|
| 5 |
+
from langchain.memory import ConversationBufferMemory
|
| 6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 7 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 8 |
+
from langchain.chains.summarize import load_summarize_chain
|
| 9 |
+
from langchain.document_loaders import YoutubeLoader, TextLoader
|
| 10 |
+
from langchain.llms.huggingface_pipeline import HuggingFacePipeline
|
| 11 |
+
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
|
| 12 |
+
import os
|
| 13 |
+
import sys
|
| 14 |
+
import torch
|
| 15 |
+
import tempfile
|
| 16 |
+
import transformers
|
| 17 |
+
from PIL import Image
|
| 18 |
+
from datetime import datetime
|
| 19 |
+
from torch import cuda, bfloat16
|
| 20 |
+
from transformers import pipeline
|
| 21 |
+
import streamlit as st
|
| 22 |
+
from streamlit_chat import message
|
| 23 |
+
from streamlit_js_eval import streamlit_js_eval
|
| 24 |
+
|
| 25 |
+
def save_feedback(feedback):
|
| 26 |
+
feedback_dir = "feedback"
|
| 27 |
+
if not os.path.exists(feedback_dir):
|
| 28 |
+
os.makedirs(feedback_dir)
|
| 29 |
+
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 30 |
+
filename = f"feedback_{timestamp}.txt"
|
| 31 |
+
filepath = os.path.join(feedback_dir, filename)
|
| 32 |
+
with open(filepath, "w") as file:
|
| 33 |
+
file.write(feedback)
|
| 34 |
+
|
| 35 |
+
def transcribe(tmp_audio_path):
|
| 36 |
+
|
| 37 |
+
device = "cuda:1" if torch.cuda.is_available() else "cpu"
|
| 38 |
+
|
| 39 |
+
pipe = pipeline(
|
| 40 |
+
"automatic-speech-recognition",
|
| 41 |
+
model="openai/whisper-small.en",
|
| 42 |
+
chunk_length_s=30,
|
| 43 |
+
device=device,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
audio = tmp_audio_path
|
| 47 |
+
prediction = pipe(audio, batch_size=8)["text"]
|
| 48 |
+
|
| 49 |
+
return prediction.strip()
|
| 50 |
+
|
| 51 |
+
def summarize(transcript, option):
|
| 52 |
+
# Laod model
|
| 53 |
+
llm = LlamaCpp(
|
| 54 |
+
streaming = True,
|
| 55 |
+
model_path="mistral-7b-instruct-v0.2.Q4_K_M.gguf",
|
| 56 |
+
#model_path="mistral-7b-openorca.Q4_K_M.gguf",
|
| 57 |
+
#model_path="starling-lm-7b-alpha.Q4_K_M.gguf",
|
| 58 |
+
#model_path="zephyr-7b-beta.Q4_K_M.gguf",
|
| 59 |
+
#model_path="dolphin-2.1-mistral-7b.Q4_K_M.gguf",
|
| 60 |
+
#model_path="llama-2-7b.Q4_K_M.gguf",
|
| 61 |
+
temperature=0,
|
| 62 |
+
top_p=1,
|
| 63 |
+
n_ctx=4096,
|
| 64 |
+
max_tokens=-1,#output word count limitation
|
| 65 |
+
verbose=True,
|
| 66 |
+
#n_gpu_layers=35,
|
| 67 |
+
#n_gpu_layers=-1,#all move to gpu
|
| 68 |
+
#n_batch=256,
|
| 69 |
+
context_length = 6000
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
### Text preprocessing
|
| 73 |
+
target_len = 600
|
| 74 |
+
chunk_size = 3000
|
| 75 |
+
chunk_overlap = 200
|
| 76 |
+
text_splitter = CharacterTextSplitter(
|
| 77 |
+
chunk_size=chunk_size,
|
| 78 |
+
chunk_overlap=chunk_overlap,
|
| 79 |
+
length_function=len,
|
| 80 |
+
)
|
| 81 |
+
texts = text_splitter.split_text(transcript,)
|
| 82 |
+
docs = [Document(page_content=t) for t in texts[:]]
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
general_prompt_template = """
|
| 88 |
+
Do not explain what you are doing. Do not self reference. You are a professional summary writer.
|
| 89 |
+
Write a concise summary of the text that cover the key points of the text. and present the results as follows:
|
| 90 |
+
- Serveral paragraphs with the following content: Topic, Outline, Description
|
| 91 |
+
- A key point list in the format of one key point in one paragraph
|
| 92 |
+
- A markdown list with the definition of the important key terms mentioned
|
| 93 |
+
```{text}```
|
| 94 |
+
SUMMARY:
|
| 95 |
+
|
| 96 |
+
"""
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
lecture_prompt_template = """
|
| 100 |
+
Do not explain what you are doing. Do not self reference. You are tasked with summarizing a lecture. Write a concise summary covering the lecture's key points and organize the results as follows:
|
| 101 |
+
- Lecture Topic: Provide a brief overview of the main subject discussed in the lecture.
|
| 102 |
+
- Lecture Description: Summarize the content and purpose of the lecture in a few sentences.
|
| 103 |
+
- Outline: Present an outline of the lecture's structure, including main sections and subtopics.
|
| 104 |
+
- Key Points: List the most important points discussed in the lecture, each presented in a separate paragraph.
|
| 105 |
+
- Formulas and Equations: Include any significant formulas or equations introduced in the lecture.
|
| 106 |
+
- Markdown Table: Create a markdown table to define and explain important terms and concepts mentioned in the lecture.
|
| 107 |
+
```{text}```
|
| 108 |
+
SUMMARY:
|
| 109 |
+
|
| 110 |
+
"""
|
| 111 |
+
|
| 112 |
+
tutorial_prompt_template = """
|
| 113 |
+
Do not explain what you are doing. Do not self reference. You have been assigned to summarize a tutorial video. Your task is to provide a concise summary covering the tutorial's main points and organize the results as follows:
|
| 114 |
+
- Tutorial Topic: Briefly introduce the main subject matter covered in the tutorial.
|
| 115 |
+
- Tutorial Description: Summarize the purpose and objectives of the tutorial in a few sentences.
|
| 116 |
+
- Tutorial Structure: Outline the tutorial's structure, including main sections, steps, or modules.
|
| 117 |
+
- Key Points: List the essential concepts or techniques explained in the tutorial, with each concept presented in its paragraph.
|
| 118 |
+
- Practical Examples: Include any practical examples or demonstrations provided in the tutorial.
|
| 119 |
+
- Tips and Tricks: Highlight any useful tips or tricks shared by the tutorial presenter.
|
| 120 |
+
- Markdown Table: Create a markdown table to define and explain important terms and concepts introduced in the tutorial.
|
| 121 |
+
```{text}```
|
| 122 |
+
SUMMARY:
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
speech_prompt_template = """
|
| 126 |
+
Do not explain what you are doing. Do not self reference. Your task is to summarize a speech. Write a concise summary covering the key points of the speech and organize the results as follows:
|
| 127 |
+
- Speech Topic: Provide a brief introduction to the main subject matter addressed in the speech.
|
| 128 |
+
- Speaker Introduction: Briefly introduce the speaker, including their background and credentials.
|
| 129 |
+
- Speech Overview: Summarize the main themes or objectives of the speech in a few sentences.
|
| 130 |
+
- Key Messages: List the key messages or arguments conveyed in the speech, with each message presented in its paragraph.
|
| 131 |
+
- Examples and Illustrations: Include any relevant examples or illustrations provided by the speaker to support their points.
|
| 132 |
+
- Closing Remarks: Summarize any concluding remarks or calls to action made by the speaker.
|
| 133 |
+
- Markdown Table: Create a markdown table to define and explain important terms or concepts mentioned in the speech.
|
| 134 |
+
```{text}```
|
| 135 |
+
SUMMARY:
|
| 136 |
+
"""
|
| 137 |
+
|
| 138 |
+
documentary_prompt_template = """
|
| 139 |
+
Do not explain what you are doing. Do not self reference. Your task is to summarize a documentary. Write a concise summary covering the main points of the documentary and organize the results as follows:
|
| 140 |
+
- Documentary Title: Provide the title of the documentary.
|
| 141 |
+
- Documentary Overview: Briefly introduce the subject matter and purpose of the documentary in a few sentences.
|
| 142 |
+
- Director's Background: Provide background information about the director or creators of the documentary.
|
| 143 |
+
- Key Themes: List the key themes or topics explored in the documentary, with each theme presented in its paragraph.
|
| 144 |
+
- Interviews and Testimonials: Highlight any interviews or testimonials featured in the documentary.
|
| 145 |
+
- Footage and Visuals: Describe any significant footage or visual elements used to convey the documentary's message.
|
| 146 |
+
- Conclusion: Summarize the main takeaways or conclusions drawn from the documentary.
|
| 147 |
+
- Markdown Table: Create a markdown table to define and explain important terms or concepts mentioned in the documentary.
|
| 148 |
+
```{text}```
|
| 149 |
+
SUMMARY:
|
| 150 |
+
"""
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
if option == 'Default':
|
| 155 |
+
prompt_template = general_prompt_template
|
| 156 |
+
elif option == 'Lecture':
|
| 157 |
+
prompt_template = lecture_prompt_template
|
| 158 |
+
elif option == 'Speech':
|
| 159 |
+
prompt_template = speech_prompt_template
|
| 160 |
+
elif option == 'Tutorial':
|
| 161 |
+
prompt_template = tutorial_prompt_template
|
| 162 |
+
elif option == 'Documentary':
|
| 163 |
+
prompt_template = documentary_prompt_template
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
PROMPT = PromptTemplate(template=prompt_template, input_variables=["text"])
|
| 169 |
+
refine_template = (
|
| 170 |
+
"Your job is to produce a final summary\n"
|
| 171 |
+
"We have provided an existing summary up to a certain point: {existing_answer}\n"
|
| 172 |
+
"We have the opportunity to refine the existing summary"
|
| 173 |
+
"with some more context below.\n"
|
| 174 |
+
"------------\n"
|
| 175 |
+
"{text}\n"
|
| 176 |
+
"------------\n"
|
| 177 |
+
f"Given the new context, refine the original summary in English within {target_len} words and do not mention the summary is refined."
|
| 178 |
+
)
|
| 179 |
+
refine_prompt = PromptTemplate(
|
| 180 |
+
input_variables=["existing_answer", "text"],
|
| 181 |
+
template=refine_template,
|
| 182 |
+
)
|
| 183 |
+
chain = load_summarize_chain(
|
| 184 |
+
llm,
|
| 185 |
+
chain_type="refine",
|
| 186 |
+
return_intermediate_steps=True,
|
| 187 |
+
question_prompt=PROMPT,
|
| 188 |
+
refine_prompt=refine_prompt,
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
resp = chain(docs)
|
| 193 |
+
output_text = resp["output_text"]
|
| 194 |
+
|
| 195 |
+
return output_text
|
| 196 |
+
|
| 197 |
+
def initialize_session_state():
|
| 198 |
+
if 'history' not in st.session_state:
|
| 199 |
+
st.session_state['history'] = []
|
| 200 |
+
|
| 201 |
+
if 'generated' not in st.session_state:
|
| 202 |
+
st.session_state['generated'] = ["Hello! Ask me anything about your video!"]
|
| 203 |
+
|
| 204 |
+
if 'past' not in st.session_state:
|
| 205 |
+
st.session_state['past'] = ["Hey!"]
|
| 206 |
+
|
| 207 |
+
def conversation_chat(query, chain, history):
|
| 208 |
+
result = chain({"question": query, "chat_history": history})
|
| 209 |
+
history.append((query, result["answer"]))
|
| 210 |
+
return result["answer"]
|
| 211 |
+
|
| 212 |
+
def display_chat_history(chain):
|
| 213 |
+
reply_container = st.container()
|
| 214 |
+
container = st.container()
|
| 215 |
+
|
| 216 |
+
with container:
|
| 217 |
+
with st.form(key='my_form', clear_on_submit=True):
|
| 218 |
+
user_input = st.text_input("Question:", placeholder="Ask about your video transcript", key='input')
|
| 219 |
+
submit_button = st.form_submit_button(label='Send')
|
| 220 |
+
|
| 221 |
+
if submit_button and user_input:
|
| 222 |
+
with st.spinner('Generating response...'):
|
| 223 |
+
output = conversation_chat(user_input, chain, st.session_state['history'])
|
| 224 |
+
|
| 225 |
+
st.session_state['past'].append(user_input)
|
| 226 |
+
st.session_state['generated'].append(output)
|
| 227 |
+
|
| 228 |
+
if st.session_state['generated']:
|
| 229 |
+
with reply_container:
|
| 230 |
+
for i in range(len(st.session_state['generated'])):
|
| 231 |
+
message(st.session_state["past"][i], is_user=True, key=str(i) + '_user', avatar_style="thumbs")
|
| 232 |
+
message(st.session_state["generated"][i], key=str(i), avatar_style="fun-emoji")
|
| 233 |
+
|
| 234 |
+
def create_conversational_chain(vector_store, option):
|
| 235 |
+
chatbot_general_prompt = PromptTemplate(input_variables=["history", "context", "question"], template="""
|
| 236 |
+
You are a knowledgeable chatbot, here to help with questions of the user. Your tone should be professional and informative.
|
| 237 |
+
|
| 238 |
+
Context: {context}
|
| 239 |
+
History: {history}
|
| 240 |
+
|
| 241 |
+
User: {question}
|
| 242 |
+
Chatbot:""
|
| 243 |
+
""")
|
| 244 |
+
chatbot_lecture_prompt = PromptTemplate(input_variables=["history", "context", "question"], template="""
|
| 245 |
+
You are a knowledgeable chatbot, you already have the knowledge of a lecture video transcript. Help with questions of the user
|
| 246 |
+
with use of this lecture video transcript. Your tone should be professional and informative.
|
| 247 |
+
|
| 248 |
+
Context: {context}
|
| 249 |
+
History: {history}
|
| 250 |
+
|
| 251 |
+
User: {question}
|
| 252 |
+
Chatbot:""
|
| 253 |
+
""")
|
| 254 |
+
chatbot_speech_prompt = PromptTemplate(input_variables=["history", "context", "question"], template="""
|
| 255 |
+
You are a knowledgeable chatbot, you already have the knowledge of a speech video transcript. Help with questions of the user
|
| 256 |
+
with use of this speech video transcript. Your tone should be professional and informative.
|
| 257 |
+
|
| 258 |
+
Context: {context}
|
| 259 |
+
History: {history}
|
| 260 |
+
|
| 261 |
+
User: {question}
|
| 262 |
+
Chatbot:""
|
| 263 |
+
""")
|
| 264 |
+
chatbot_tutorial_prompt = PromptTemplate(input_variables=["history", "context", "question"], template="""
|
| 265 |
+
You are a knowledgeable chatbot, you already have the knowledge of a tutorial video transcript. Help with questions of the user
|
| 266 |
+
with use of this tutorial video transcript. Your tone should be professional and informative.
|
| 267 |
+
|
| 268 |
+
Context: {context}
|
| 269 |
+
History: {history}
|
| 270 |
+
|
| 271 |
+
User: {question}
|
| 272 |
+
Chatbot:""
|
| 273 |
+
""")
|
| 274 |
+
|
| 275 |
+
chatbot_documentary_prompt = PromptTemplate(input_variables=["history", "context", "question"], template="""
|
| 276 |
+
You are a knowledgeable chatbot, you already have the knowledge of a documentary video transcript. Help with questions of the user
|
| 277 |
+
with use of this documentary video transcript. Your tone should be professional and informative.
|
| 278 |
+
|
| 279 |
+
Context: {context}
|
| 280 |
+
History: {history}
|
| 281 |
+
|
| 282 |
+
User: {question}
|
| 283 |
+
Chatbot:""
|
| 284 |
+
""")
|
| 285 |
+
|
| 286 |
+
if option == 'Default':
|
| 287 |
+
chatbot_prompt = chatbot_general_prompt
|
| 288 |
+
elif option == 'Lecture':
|
| 289 |
+
chatbot_prompt = chatbot_lecture_prompt
|
| 290 |
+
elif option == 'Speech':
|
| 291 |
+
chatbot_prompt = chatbot_speech_prompt
|
| 292 |
+
elif option == 'Tutorial':
|
| 293 |
+
chatbot_prompt = chatbot_tutorial_prompt
|
| 294 |
+
elif option == 'Documentary':
|
| 295 |
+
chatbot_prompt = chatbot_documentary_prompt
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
# Create llm
|
| 299 |
+
llm = LlamaCpp(
|
| 300 |
+
streaming = True,
|
| 301 |
+
#model_path="mistral-7b-instruct-v0.1.Q4_K_M.gguf",
|
| 302 |
+
#model_path="mistral-7b-openorca.Q4_K_M.gguf",
|
| 303 |
+
#model_path="starling-lm-7b-alpha.Q4_K_M.gguf",
|
| 304 |
+
model_path="zephyr-7b-beta.Q4_K_M.gguf",
|
| 305 |
+
#model_path="dolphin-2.1-mistral-7b.Q4_K_M.gguf",
|
| 306 |
+
#model_path="llama-2-7b.Q4_K_M.gguf",
|
| 307 |
+
temperature=0,
|
| 308 |
+
top_p=1,
|
| 309 |
+
n_ctx=4096,
|
| 310 |
+
max_tokens=-1,#output word count limitation
|
| 311 |
+
verbose=True,
|
| 312 |
+
#n_gpu_layers=35,
|
| 313 |
+
#n_gpu_layers=-1,#all move to gpu
|
| 314 |
+
#n_batch=256,
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 318 |
+
|
| 319 |
+
chain = ConversationalRetrievalChain.from_llm(llm=llm, chain_type='stuff',
|
| 320 |
+
retriever=vector_store.as_retriever(search_kwargs={"k": 2}),
|
| 321 |
+
memory=memory, condense_question_prompt=chatbot_prompt)
|
| 322 |
+
return chain
|
| 323 |
+
|
| 324 |
+
def main():
|
| 325 |
+
# Initialize session state
|
| 326 |
+
initialize_session_state()
|
| 327 |
+
im = Image.open('sricon.png')
|
| 328 |
+
st.set_page_config(page_title=' 🤖Automatic Video Assistant🔗', layout='wide', page_icon = im)
|
| 329 |
+
|
| 330 |
+
# Set up the Streamlit app layout
|
| 331 |
+
st.title("🤖 Automatic Video Assistant 🔗")
|
| 332 |
+
st.subheader(" Powered by LangChain + Streamlit")
|
| 333 |
+
|
| 334 |
+
hide_default_format = """
|
| 335 |
+
<style>
|
| 336 |
+
#MainMenu {visibility: hidden; }
|
| 337 |
+
footer {visibility: hidden;}
|
| 338 |
+
</style>
|
| 339 |
+
"""
|
| 340 |
+
st.markdown(hide_default_format, unsafe_allow_html=True)
|
| 341 |
+
|
| 342 |
+
#sidebar
|
| 343 |
+
with st.sidebar:
|
| 344 |
+
st.markdown("# Introduction")
|
| 345 |
+
st.markdown(
|
| 346 |
+
"Automatic Video Assistant is able to summarize videos and answer related questions.")
|
| 347 |
+
st.markdown("You can select specific video types to enhance the assistant's performance.")
|
| 348 |
+
st.markdown("You can input local video or YouTube video link.")
|
| 349 |
+
st.markdown("# Input your video to start!")
|
| 350 |
+
st.markdown("---")
|
| 351 |
+
st.markdown("# Feedback")
|
| 352 |
+
txt = st.text_area(
|
| 353 |
+
"We will continue to improve💪?",
|
| 354 |
+
"Please share your feedback... ",
|
| 355 |
+
)
|
| 356 |
+
if st.button('Submit'):
|
| 357 |
+
save_feedback(txt)
|
| 358 |
+
st.write('Your feedback is submitted!')
|
| 359 |
+
|
| 360 |
+
|
| 361 |
+
option = st.selectbox(
|
| 362 |
+
'Please indicate you video type for better interaction😀',
|
| 363 |
+
('Default', 'Lecture', 'Speech', 'Tutorial', 'Documentary'))
|
| 364 |
+
|
| 365 |
+
st.write('Selected video type:', option)
|
| 366 |
+
|
| 367 |
+
|
| 368 |
+
#User Input File
|
| 369 |
+
audio_file = st.file_uploader("Upload Video", type=["mp4", "wav","mp3","mov","avi","wmv"])
|
| 370 |
+
|
| 371 |
+
with st.form('myform', clear_on_submit=True):
|
| 372 |
+
youtube_url = st.text_input("Or enter a YouTube URL")
|
| 373 |
+
submitted = st.form_submit_button('Submit')
|
| 374 |
+
|
| 375 |
+
# Check if either YouTube URL or file uploaded
|
| 376 |
+
if (submitted and youtube_url) or audio_file:
|
| 377 |
+
if youtube_url:
|
| 378 |
+
loader = YoutubeLoader.from_youtube_url(youtube_url, add_video_info=False)
|
| 379 |
+
else:
|
| 380 |
+
#transcript = transcribe(audio_file)
|
| 381 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_audio_file:
|
| 382 |
+
tmp_audio_file.write(audio_file.read())
|
| 383 |
+
tmp_audio_path = tmp_audio_file.name
|
| 384 |
+
transcript = transcribe(tmp_audio_path)
|
| 385 |
+
|
| 386 |
+
with st.expander("See Transcript"):
|
| 387 |
+
if youtube_url:
|
| 388 |
+
transcript = loader.load()
|
| 389 |
+
# Save the transcript to a text file
|
| 390 |
+
with open("transcript.txt", "w", encoding="utf-8") as file:
|
| 391 |
+
transcript_text = '\n'.join([document.page_content for document in transcript])
|
| 392 |
+
file.write(transcript_text)
|
| 393 |
+
with open("transcript.txt", "r", encoding="utf-8") as file:
|
| 394 |
+
transcript = file.read()
|
| 395 |
+
else:
|
| 396 |
+
with open("transcript.txt", "w") as f:
|
| 397 |
+
f.write(transcript)
|
| 398 |
+
# Display the transcript
|
| 399 |
+
st.write(transcript)
|
| 400 |
+
# Provide a download button for the transcript
|
| 401 |
+
st.download_button("Download Transcript", transcript, key='transcript_download_button')
|
| 402 |
+
|
| 403 |
+
st.subheader("Do you want a summary for this video?")
|
| 404 |
+
if 'clicked' not in st.session_state:
|
| 405 |
+
st.session_state.clicked = False
|
| 406 |
+
def click_button():
|
| 407 |
+
st.session_state.clicked = True
|
| 408 |
+
st.button('Generate summary', on_click=click_button)
|
| 409 |
+
#Summarize
|
| 410 |
+
if st.session_state.clicked:
|
| 411 |
+
with st.expander("See Summary", expanded=True):
|
| 412 |
+
st.header("Summary")
|
| 413 |
+
summary = summarize(transcript, option)
|
| 414 |
+
with open("summary.txt", "w") as f:
|
| 415 |
+
f.write(summary)
|
| 416 |
+
with open("summary.txt", "r") as f:
|
| 417 |
+
for line in f:
|
| 418 |
+
st.write(line)
|
| 419 |
+
#st.write_stream(summary)
|
| 420 |
+
#st.download_button("Download Summary", summary, key='summary_download_button')
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
loader = TextLoader("transcript.txt")
|
| 425 |
+
documents = loader.load()
|
| 426 |
+
|
| 427 |
+
st.header("Chatbot🤖")
|
| 428 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=20)
|
| 429 |
+
text_chunks = text_splitter.split_documents(documents)
|
| 430 |
+
|
| 431 |
+
# Create embeddings
|
| 432 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",
|
| 433 |
+
model_kwargs={'device': 'cuda:1'})
|
| 434 |
+
#model_kwargs={'device': 'cpu'}) #Almost the same speed
|
| 435 |
+
|
| 436 |
+
# Create vector store
|
| 437 |
+
vector_store = FAISS.from_documents(text_chunks, embedding=embeddings)
|
| 438 |
+
|
| 439 |
+
# Create the chain object
|
| 440 |
+
chain = create_conversational_chain(vector_store, option)
|
| 441 |
+
|
| 442 |
+
display_chat_history(chain)
|
| 443 |
+
|
| 444 |
+
if st.button("Click to start with a new video"):
|
| 445 |
+
streamlit_js_eval(js_expressions="parent.window.location.reload()")
|
| 446 |
+
|
| 447 |
+
if __name__ == "__main__":
|
| 448 |
+
main()
|
sricon.png
ADDED
|
|
transcript.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
Today on The Stay At Home Chef I'm showing you how to cook steak perfectly every time. This method works for any kind of steak and will help you achieve the perfect steak every single time. There's no need to be intimidated by cooking steak because it's actually super easy and I'm going to show you how. The first thing you need to do is pull your steaks out and let them sit at room temperature for at least 30 minutes. If you're using frozen steaks you'll want to thaw them in the fridge first but you do want them to come to room temperature. And if you'll notice we're using nice thick steaks. Thick steaks are actually easier to cook perfectly. Thin steaks will cook too fast and may even dry out so choose a good thick steak. Today we're using a couple of New York strip steaks but this same method works with ribeye or t-bone or even filet so choose a cut of steak that you like. You can see this steak has some nice marbling in it and some fat around the outside. There are actually three different grades of beef, select, choice, and prime and it's all based on the marbling and flavor in the beef. Now the difference between select and choice is actually pretty big so I always recommend choosing at least choice grade or prime grade beef when you're making steak. This is actually a choice grade New York steak. Once your steaks have had a chance to sit out at room temperature for 30 minutes, preheat your oven to 375 degrees Fahrenheit and heat a large heavy skillet on the stove until it's smoking hot. Then we'll prep our steaks. Drizzle just a little bit of olive oil onto each steak and rub it into the top and the sides as well as the bottom. Then we're going to put together our rub. Today I'm going to keep it super simple and just use a mixture of salt and pepper. Use a teaspoon of salt, half a teaspoon of black pepper, and half a teaspoon of white pepper. Mix these together. You can add other spices and herbs but I like to keep it simple and let the meat shine. We're going to take this mixture, sprinkle it over our steaks, and rub it right in. You want to make sure you do both sides. Now since these steaks are really thick, you want to go a little more heavy-handed than you might think. Before we go over and cook our steaks, there's one last thing we want to do. I like to finish off my steaks with butter, so I'm going to make a little garlic compound butter here by mixing 2 tablespoons of softened butter with 1 to 2 cloves of crushed garlic and you just mash that together with a fork. You can also add in fresh herbs to this butter to make it an herb compound butter but I'm keeping it simple with just a little bit of garlic. Next we're going to head on over to the stove and I'm pretty sure our skillet is smoking hot which is exactly what we need. Place the steaks down directly into the pan. You don't need oil or anything. And we're going to sear each side of the steak for 2-3 minutes per side. And then since these have a little fat strip that runs along the side, you want to render that fat down. So just turn it onto its side and hold it there with tongs. You always want to render the fat so you don't end up with a chewy piece of fat around the edges. Once it's seared on both sides and you've rendered the fat then just slide them in the oven to finish cooking. The amount of time that you need to leave your steaks in the oven varies depending on how long you seared your steaks and how thick they are. A good rule of thumb is about five minutes per inch, but if you want 100% accurate results, use a meat thermometer. I pull my steak out when it reaches 130 degrees Fahrenheit for a good medium rare. The steaks will need to rest for at least 10 minutes before serving, so you want to dollop on a tablespoon of butter onto each one so it'll melt over the steak while it rests. As you let your steaks rest the internal temperature will continue to rise by about 5 degrees. It's important to let your steaks rest because it gives them a chance for the juices to reabsorb into the meat, giving you the juiciest, most perfect steak possible. After 10 minutes of resting, you are ready to eat! Thanks for watching! You can find the full written recipe in the video description. Be sure to subscribe, like, and follow, and check out the rest of my videos, where you can find hundreds of restaurant quality recipes you can easily make at home. See you later!
|