The app is running before second text_input is given

val1 = st.text_input(โ€œEnter a sentence1โ€)

val2 = st.text_input("Enter a sentence2")

text1 = word_tokenize(val1)
text2 = word_tokenize(val2)

sw = stopwords.words('english') 
l1 =[];l2 =[]
text_1 = {w for w in text1 if not w in sw} 
text_2 = {w for w in text2 if not w in sw}
rvector = text_1.union(text_2) 
for w in rvector:

    if w in text_1: l1.append(1) # create a vector
    else: l1.append(0)
    if w in text_2: l2.append(1)
    else: l2.append(0)
c = 0
for i in range(len(rvector)):
    c+= l1[i]*l2[i]
    out = c / float((sum(l1)*sum(l2))**0.5)  


if st.button("Cosine Similarity"):
    st.success(out)

The program starts running and give error since it is running before the second input is even given. Then when I give second input, the error is goes away.

tl;dr Code more defensively. This fixes your problem:

val1 = st.text_input("Enter a sentence1")
val2 = st.text_input("Enter a sentence2")

if (not val1) or (not val2):
    st.info('The calculations will run, once you entered two sentences.')
    st.stop()

text1 = word_tokenize(val1)
text2 = word_tokenize(val2)

sw = stopwords.words('english')
l1 = []
l2 = []
text_1 = {w for w in text1 if not w in sw}
text_2 = {w for w in text2 if not w in sw}

if len(text_1) == 0 or len(text_2) == 0:
    st.warning('One of your inputs contains only stopwords. Please enter a more meaningful text.')
    st.stop()

rvector = text_1.union(text_2)
for w in rvector:

    if w in text_1:
        l1.append(1)  # create a vector
    else:
        l1.append(0)
    if w in text_2:
        l2.append(1)
    else:
        l2.append(0)

st.write(l1)
st.write(l2)
c = 0
for i in range(len(rvector)):
    c += l1[i] * l2[i]
try:
    out = c / float((sum(l1) * sum(l2)) ** 0.5)
except ZeroDivisionError:
    st.error('It appears, something went horribly wrong!')
    out = 0

st.success(out)

longer explanation
The main problem here is not with Streamlit but with the lack of safeguards in your code. Try to code more defensively. I assume the main problem is this line of code:

It is executed every run and assumes that sum(l1) and sum(l2) are not 0. If they are, a ZeroDivisionError is thrown. This is the case if a) either val1 or val2 are empty strings or b) if any of the two consists of stopwords only.

Stop execution if inputs are incomplete
To address a), you can stop the code from executing if both values are not set. Giving the user some feedback as to what is happening is always a good idea.

val1 = st.text_input("Enter a sentence1")
val2 = st.text_input("Enter a sentence2")

if (not val1) or (not val2):
    st.info('The calculations will run, once you entered two sentences.')
    st.stop()

# rest of your code
...

You could also have the user initiate the calculation manually.

val1 = st.text_input("Enter a sentence1")
val2 = st.text_input("Enter a sentence2")

if st.button("Cosine Similarity"):
    if (not val1) or (not val2):
        st.info('The calculations will run, once you entered two sentences.')
        st.stop()

    # rest of your code
    ...

Make sure inputs are not only stopwords
To address issue b), make sure to implement a sanity check for your sets.

...
text_1 = {w for w in text1 if not w in sw}
text_2 = {w for w in text2 if not w in sw}

if len(text_1) == 0 or len(text_2) == 0:
    st.warning('One of your inputs contains only stopwords. Please enter a more meaningful text.')
    st.stop()
...

Catch ZeroDivisionErrors
Finally, it is probably a good idea to prevent zero divisions, when dividing by some variable.

for i in range(len(rvector)):
    c += l1[i] * l2[i]
try:
    out = c / float((sum(l1) * sum(l2)) ** 0.5)
except ZeroDivisionError:
    st.error('It appears, something went horribly wrong!')
    out = 0

On a somewhat unrelated note, I am not entirely sure, using cosine similarity is a good fit for what you are trying to do. You may want to look into word embeddings and bag-of-word models for your sentence representation or simply use intersection over union as a distance metric.