Skip to content
site logo mobile

Code Snippets Library >

Extracting Data from YouTube Comments

🔑 ID:







This code reads a list of comments from a CSV file and extracts data about them according to a prompt.

This code checks if the comments are testimonial-worthy or not.

To learn more about how the code works, read this.


import csv
import time
from SimplerLLM.language.llm import LLM, LLMProvider
from import read_csv_file

def is_testimonial_worthy(comment):
    llm_instance = LLM.create(provider=LLMProvider.OPENAI, model_name="gpt-3.5-turbo")
    u_prompt = f"""You are an expert in comment analysis. Evaluate if this YouTube comment is worthy 
    of being a testimonial on my website. Only consider positive testimonials. Return True or False.

    Comment: {comment}
    response = llm_instance.generate_response(prompt=u_prompt)
    return response

start = time.time()
file = read_csv_file("youtube_comments.csv")
counter = 0

with open('my_data.csv', 'a', newline='', encoding='utf-8') as csvfile:
    writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
    if csvfile.tell() == 0:
        writer.writerow(['Comment', 'User', 'Profile Picture', 'Video ID', 'Video Privacy', 'Time', 'Testimonial Worthy'])
    for row in file.content[1:]:
        testimonial = is_testimonial_worthy(row[0])
        row[6] = testimonial
        writer.writerow([row[0], row[1], row[2], row[3], row[4], row[5], row[6]])
        counter = counter + 1

end = time.time()


Untitled design (82)

GitHub Link

✖️ Not Available

Untitled design (83)

Download File

✖️ Not Available

If you’re encountering any problems or need further assistance with this code, we’re here to help! Join our community on the forum or Discord for support, tips, and discussion.