Error: 'NoneType' object has no attribute 'span'

I am running the streamlit app for YouTube link and it chats with the video. It was working fine till todoy. From today I am getting this error:
Error: ‘NoneType’ object has no attribute ‘span’
Can you please guide me to resolve this error?

Can you please share a small reproducible code snippet that reproduces this issue?

import streamlit as st
from htmlTemplates import css, bot_template, user_template

from dotenv import load_dotenv
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings, HuggingFaceInstructEmbeddings
from langchain.vectorstores import FAISS
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain.llms import HuggingFaceHub
import whisper,os
from pytube import YouTube
import datetime

#st.set_page_config(page_title=“Question and Answering on Web Page”, page_icon=“:video”)

def get_video_text(url):
model = whisper.load_model(‘base’)
youtube_video_url = url #“https://www.youtube.com/watch?v=27S2JzQnZh8” #“https://www.youtube.com/watch?v=SuIGHtDyuP8
youtube_video = YouTube(youtube_video_url)
print(‘youtube_video.title-’,youtube_video.title)
for stream in youtube_video.streams:
print(stream)
streams = youtube_video.streams.filter(only_audio=True)
stream.download(filename=‘feds.mp4’)
# save a timestamp before transcription
t1 = datetime.datetime.now()
# print(f"started at {t1}")
path=os.getcwd()
# print(os.getcwd())
file=‘feds.mp4’
# print(os.path.join(path,file))
full_file_location=os.path.join(path,file)
# print(“full_file_location-”,full_file_location)
# do the transcription
# output = model.transcribe(“fed.mp4”, fp16=False)

output = model.transcribe(full_file_location, fp16=False)

show time elapsed after transcription is complete.

t2 = datetime.datetime.now()
# print(f"ended at {t2}")
# print(f"time elapsed: {t2 - t1}")
text=output['text']
# print(text)
return text

def get_text_chunks(text):
text_splitter = CharacterTextSplitter(
separator=“\n”,
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
chunks = text_splitter.split_text(text)
return chunks

def get_vectorstore(text_chunks):
embeddings = OpenAIEmbeddings()
#embeddings = HuggingFaceInstructEmbeddings(model_name=“hkunlp/instructor-xl”)
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
return vectorstore

def get_conversation_chain(vectorstore):
llm = ChatOpenAI()
:speaking_head: Large Language Models (LLMs) = HuggingFaceHub(repo_id=“google/flan-t5-xxl”, model_kwargs={“temperature”:0.5, “max_length”:512})

memory = ConversationBufferMemory(
    memory_key='chat_history', return_messages=True)
conversation_chain = ConversationalRetrievalChain.from_llm(
    llm=llm,
    retriever=vectorstore.as_retriever(),
    memory=memory
)
return conversation_chain

def generate_summary_url(url, max_words):
if url:
try:
st.session_state.chat_history = None
# Load the website content from the provided URL
web_document = get_video_text(url)

        summary = generate_summary(web_document, max_words)

        # Display the summary
        st.subheader("Summary:")
        st.write(summary)

    except Exception as e:
        st.error(f"Error: {str(e)}")
else:
    st.error("Please enter a Link")

def generate_summary(text, max_words):
# Clear the chat history before generating a new summary
st.session_state.chat_history =
response = st.session_state.conversation(
{‘question’: f"Summarize the document to {max_words} words."})
summary = response[‘chat_history’][-1].content
return summary

def generate_and_display_keypoints(url):
if url:
try:
st.session_state.chat_history = None
# Combine PDF texts
web_document = get_video_text(url)

        # Generate the summary
        summary = generate_keypoints(web_document)

        # Display the summary
        st.subheader("Summary:")
        st.write(summary)

    except Exception as e:
        st.error(f"Error: {str(e)}")
else:
    st.error("Please enter a Link")

def generate_keypoints(text):
# Clear the chat history before generating a new summary
st.session_state.chat_history =
response = st.session_state.conversation(
{‘question’: “Key Points of the document in 10 Bullet Points.”})
summary = response[‘chat_history’][-1].content
return summary

def handle_userinput(user_question):
response = st.session_state.conversation({‘question’: user_question})
st.session_state.chat_history = response[‘chat_history’]

for i, message in enumerate(st.session_state.chat_history):
    if i % 2 == 0:
        st.write(user_template.replace(
            "{{MSG}}", message.content), unsafe_allow_html=True)
    else:
        st.write(bot_template.replace(
            "{{MSG}}", message.content), unsafe_allow_html=True)

def main():
load_dotenv()

st.set_page_config(page_title="Chat with Video", page_icon=":clapper:")
st.write(css, unsafe_allow_html=True)

if "conversation" not in st.session_state:
    st.session_state.conversation = None
if "chat_history" not in st.session_state:
    st.session_state.chat_history = None

st.image("simplifAI logo.jpg", width=200)
st.header("SimplifAI Video :clapper:")
st.subheader("Hi, what would you like me to simplify?")
user_question = st.chat_input(" ")

hide_st_style = """
        <style>
        #MainMenu {visibility: hidden;}
        footer {visibility: hidden;}
        </style>
        """
st.markdown(hide_st_style, unsafe_allow_html=True)

if user_question:
    handle_userinput(user_question)
    
with st.sidebar:

    st.subheader("Your Video Link")
    url = st.text_input("Enter a Video URL:")

    if st.button("Process"):
        if url:
            with st.spinner("Processing"):
                if not url.startswith("http"):
                    st.error("Invalid video URL")
                try:
                    # get pdf text
                    raw_text = get_video_text(url)

                    # get the text chunks
                    text_chunks = get_text_chunks(raw_text)

                    # create vector store
                    vectorstore = get_vectorstore(text_chunks)

                    # Update the processing status in the sidebar
                    st.sidebar.info("Processing completed.")

                    # create conversation chain
                    st.session_state.conversation = get_conversation_chain(vectorstore) 
                except Exception as e:
                            st.error(f"Error: {str(e)}")
        else:
            st.error("Please enter video URL.")

if st.session_state.conversation:
# Buttons to generate summaries
    st.sidebar.subheader("Video Summary Options (select an option or type a request or question)")
    if st.sidebar.button("10 Key Points"):
        generate_and_display_keypoints(url)
    if st.sidebar.button("100 Words Summary"):
        generate_summary_url(url, max_words=100)
    if st.sidebar.button("200 Words Summary"):
        generate_summary_url(url, max_words=200)
    if st.sidebar.button("500 Words Summary"):
        generate_summary_url(url, max_words=500)

if name == ‘main’:
main()

Here You Go…

Add Error Handling

# Example code that might cause the error
text = None
substring = text.span(1)  # This would cause the 'NoneType' error

# Using error handling to avoid the error
try:
    substring = text.span(1)
except AttributeError as e:
    print(f"AttributeError: {e}")
    substring = None  # Handle the error gracefully

Where am I suppose to insert the code which you have given in my code? If you guide me I will really appreciate it…

I have shared the code can you please guide… I will really appreciate it… Thanks!

When you post a code snippet, please use triple quotes ``` so that it gets formatted as code. Otherwise it is not possible to test the code.

Can you please edit your code down to the smallest possible version that still shows the error, and then post that code (formatted with ```) AND copy and paste the entire error message, including line numbers. Otherwise it will be much more difficult to help resolve your issue.

‘’’
import whisper,os
from pytube import YouTube
import datetime

def get_video_text(url):
model = whisper.load_model(‘base’)
youtube_video_url = url
youtube_video = YouTube(youtube_video_url)
print(‘youtube_video.title-’,youtube_video.title)
for stream in youtube_video.streams:
print(stream)
streams = youtube_video.streams.filter(only_audio=True)
stream.download(filename=‘feds.mp4’)
# save a timestamp before transcription
t1 = datetime.datetime.now()
# print(f"started at {t1}")
path=os.getcwd()
# print(os.getcwd())
file=‘feds.mp4’
# print(os.path.join(path,file))
full_file_location=os.path.join(path,file)
# print(“full_file_location-”,full_file_location)
# do the transcription
# output = model.transcribe(“fed.mp4”, fp16=False)

output = model.transcribe(full_file_location, fp16=False)

show time elapsed after transcription is complete.

t2 = datetime.datetime.now()
# print(f"ended at {t2}")
# print(f"time elapsed: {t2 - t1}")
text=output['text']
# print(text)
return text

url=(‘https://www.youtube.com/watch?v=9dSd8WNTlxo’)
get_video_text(url)
‘’’

Only this error displayed in the app:
Error: ‘NoneType’ object has no attribute ‘span’

Blockquote
import whisper,os
from pytube import YouTube
import datetime
def get_video_text(url):
model = whisper.load_model(‘base’)
youtube_video_url = url
youtube_video = YouTube(youtube_video_url)
print(‘youtube_video.title-’,youtube_video.title)
for stream in youtube_video.streams:
print(stream)
streams = youtube_video.streams.filter(only_audio=True)
stream.download(filename=‘feds.mp4’)
# save a timestamp before transcription
t1 = datetime.datetime.now()
# print(f"started at {t1}")
path=os.getcwd()
# print(os.getcwd())
file=‘feds.mp4’
# print(os.path.join(path,file))
full_file_location=os.path.join(path,file)
# print(“full_file_location-”,full_file_location)
# do the transcription
# output = model.transcribe(“fed.mp4”, fp16=False)

output = model.transcribe(full_file_location, fp16=False)
# show time elapsed after transcription is complete.
t2 = datetime.datetime.now()
# print(f"ended at {t2}")
# print(f"time elapsed: {t2 - t1}")
text=output['text']
# print(text)
return text

Can you please paste the entire set code as a single code block that only contains the code which is necessary to show the issue, and has proper indentation?

e.g.

import streamlit as st

def my_func(a):
    return a

for i in range(10):
    my_func("test")

Notice how that is a single code-block, and if you copy it it will run – it includes all of the necessary indentation in particular – your code snippet has no indentation, and so it is not runnable.

This issue has been resolved… it was from the pytube side and its working fine now… but there is another issue I have a chat with website app running fine on streamlit cloud till 2 days ago but when I reboot it, it started giving me this error:
Error: Invalid URL ‘h’: No scheme supplied. Perhaps you meant https://h?
It is running fine locally but on cloud it is giving the above mentioned error.
Can you please guide me how to resolve this issue… The github repository link of this app is as under:

Since this is unrelated to the original question, it would be best to discuss this on the other post you started

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.