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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import pytube
|
| 3 |
+
from pytube import YouTube
|
| 4 |
+
import moviepy.editor as mp
|
| 5 |
+
from pydub import AudioSegment
|
| 6 |
+
from urllib.parse import urlparse
|
| 7 |
+
import os
|
| 8 |
+
import whisper
|
| 9 |
+
import openai
|
| 10 |
+
import json
|
| 11 |
+
import ffmpeg
|
| 12 |
+
import yt_dlp
|
| 13 |
+
|
| 14 |
+
model = whisper.load_model("small")
|
| 15 |
+
|
| 16 |
+
def Analyze(video_link):
|
| 17 |
+
|
| 18 |
+
video_url = f"{video_link}"
|
| 19 |
+
|
| 20 |
+
# Your existing code for processing the video and analyzing with ChatGPT
|
| 21 |
+
|
| 22 |
+
# Extracting details of the Video
|
| 23 |
+
print("Extracting Video Details"'\n')
|
| 24 |
+
try:
|
| 25 |
+
video = YouTube(video_url)
|
| 26 |
+
video_channel = video.author
|
| 27 |
+
video_title = video.title
|
| 28 |
+
video_description = video.description
|
| 29 |
+
video_url = video.watch_url
|
| 30 |
+
|
| 31 |
+
print(f"Channel Name: {video_channel}")
|
| 32 |
+
print(f"Video Tile: {video_title}")
|
| 33 |
+
print(f"Video URL: {video_url}")
|
| 34 |
+
print(f"Video Description: {video_description}")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"An error occured: {str(e)}")
|
| 37 |
+
|
| 38 |
+
#---
|
| 39 |
+
try:
|
| 40 |
+
# Determine if it's for YouTube or YouTube Shorts and save the audio \
|
| 41 |
+
# file according to its video ID
|
| 42 |
+
parsed_url = urlparse(video_url)
|
| 43 |
+
if 'youtube.com' in parsed_url.netloc:
|
| 44 |
+
if '/shorts/' in parsed_url.path:
|
| 45 |
+
# It's a YouTube Shorts URL
|
| 46 |
+
video_id = parsed_url.path.split('/shorts/')[-1]
|
| 47 |
+
else:
|
| 48 |
+
# It's a regular YouTube video URL
|
| 49 |
+
query_params = parsed_url.query.split('&')
|
| 50 |
+
video_id = None
|
| 51 |
+
for param in query_params:
|
| 52 |
+
if param.startswith('v='):
|
| 53 |
+
video_id = param[2:]
|
| 54 |
+
break
|
| 55 |
+
if video_id is None:
|
| 56 |
+
print("Invalid YouTube video URL. No video ID found.")
|
| 57 |
+
else:
|
| 58 |
+
print("Invalid URL. Please provide a valid YouTube URL.")
|
| 59 |
+
|
| 60 |
+
#---
|
| 61 |
+
|
| 62 |
+
# Downloading audio and converting to .wav format
|
| 63 |
+
print("Downloading Audio")
|
| 64 |
+
if video_id:
|
| 65 |
+
# Replace 'VIDEO_URL' with the actual URL of the video you want to download
|
| 66 |
+
|
| 67 |
+
# Options to download only audio in the best quality available
|
| 68 |
+
options = {
|
| 69 |
+
'format': 'bestaudio/best',
|
| 70 |
+
'postprocessors': [{
|
| 71 |
+
'key': 'FFmpegExtractAudio',
|
| 72 |
+
'preferredcodec': 'mp3',
|
| 73 |
+
'preferredquality': 'best',
|
| 74 |
+
}],
|
| 75 |
+
'outtmpl': '%(id)s.%(ext)s',
|
| 76 |
+
'quiet': True, # Set quiet to True to suppress verbose output
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# Create yt-dlp object with specified options
|
| 80 |
+
ydl = yt_dlp.YoutubeDL(options)
|
| 81 |
+
|
| 82 |
+
# Download the video
|
| 83 |
+
ydl.download([video_url])
|
| 84 |
+
print("AUDIO DOWNLOADED")
|
| 85 |
+
savepath_audio = f'{video_id}.mp3'
|
| 86 |
+
|
| 87 |
+
print(savepath_audio)
|
| 88 |
+
print('\n'"Audio path: ", savepath_audio)
|
| 89 |
+
#Whisper Transcription
|
| 90 |
+
print('\n'"Transcribing Audio, Please wait!"'\n')
|
| 91 |
+
result = model.transcribe(savepath_audio, verbose=True, task='translate')
|
| 92 |
+
os.remove(savepath_audio)
|
| 93 |
+
|
| 94 |
+
combined_data = ""
|
| 95 |
+
|
| 96 |
+
for segment in result['segments']:
|
| 97 |
+
start_time = "[{:02d}:{:06.3f}".format(int(segment['start']) // 60, segment['start'] % 60)
|
| 98 |
+
end_time = "{:02d}:{:06.3f}]".format(int(segment['end']) // 60, segment['end'] % 60)
|
| 99 |
+
timestamp = start_time + ' --> ' + end_time
|
| 100 |
+
text = segment['text']
|
| 101 |
+
|
| 102 |
+
# Combining transcript into a single string with newlines
|
| 103 |
+
combined_data += timestamp + ' ' + text + '\n'
|
| 104 |
+
|
| 105 |
+
#----
|
| 106 |
+
|
| 107 |
+
#Using ChatGPT for Analysis
|
| 108 |
+
openai.api_key = os.environ.get('API_KEY')
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def callChatGPT(input_text):
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
#input_prompt = "I have receive the message following message: " + '"' + str(input_text) + '"' + "."
|
| 115 |
+
input_prompt = """you will be provided a transcript of a video with timestamp. perform the following actions keeping \
|
| 116 |
+
youtube commmunity guidelines, giving importance to freedom of speech and differentiating between fact and opinion,\
|
| 117 |
+
and the idea that people are free to believe what they want to believe and say what they want about other people with\
|
| 118 |
+
different viewpoints \
|
| 119 |
+
(as long as the latter do not belong to 'protected group' category i.e., video should not hateful towards age, caste, \
|
| 120 |
+
disability, ethnicity, gender identity and expression, nationality, race, immigration status, religion,\
|
| 121 |
+
sex/gender, sexual orientation, victims of a major violent event and their kin, and veteran status; if it is not \
|
| 122 |
+
against any protected group, then \
|
| 123 |
+
flag ONLY if the language is extremely hateful - ignore minor everyday foul deragatory language) in mind:
|
| 124 |
+
1 - summarize the text delimited by triple backticks with 1-2 sentence \
|
| 125 |
+
2 - provide the overall sentiment of the text \
|
| 126 |
+
3 - differentiate between fact or opinion - if it is an opinion, flag the text as not hateful by default unless there are explicit/hateful words \
|
| 127 |
+
4 - check for any hate in the text. if it does then just say yes else say no \
|
| 128 |
+
5 - if the text contains hate then mention all the timestamps and it's texts where it contains hate \
|
| 129 |
+
6 - classify the risk/degree of hate into high, medium, or low \
|
| 130 |
+
7 - if flagged as hate, explain why
|
| 131 |
+
8 - output a json object which contains the following keys: \
|
| 132 |
+
summary, sentiment, presence_of_hate, hate_degree, part_which_contains_hate, fact_or_opinion, explanation""" + '```' + str(input_text) + '```' + "."
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
response = openai.ChatCompletion.create(
|
| 136 |
+
model="gpt-3.5-turbo",
|
| 137 |
+
messages=[
|
| 138 |
+
{"role": "user", "content": input_prompt}
|
| 139 |
+
],
|
| 140 |
+
temperature=0,
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
return response
|
| 144 |
+
|
| 145 |
+
input_text = (f"""{combined_data}""")
|
| 146 |
+
print('\n'"ANALYZING..."'\n')
|
| 147 |
+
response_AI = callChatGPT(input_text)
|
| 148 |
+
|
| 149 |
+
# Extracting values from the response
|
| 150 |
+
response_data = response_AI['choices'][0]['message']['content']
|
| 151 |
+
response_json = json.loads(response_data)
|
| 152 |
+
summary = response_json.get('summary', '')
|
| 153 |
+
sentiment = response_json.get('sentiment', '')
|
| 154 |
+
presence_of_hate = response_json.get('presence_of_hate', '')
|
| 155 |
+
hate_degree = response_json.get('hate_degree', '')
|
| 156 |
+
part_which_contains_hate = response_json.get('part_which_contains_hate', '')
|
| 157 |
+
foo = response_json.get('fact_or_opinion', '')
|
| 158 |
+
exp = response_json.get('explanation', '')
|
| 159 |
+
|
| 160 |
+
print(text)
|
| 161 |
+
print('\n')
|
| 162 |
+
print("Summary:", summary)
|
| 163 |
+
print("Sentiment:", sentiment)
|
| 164 |
+
print("Fact or opinion: ", foo)
|
| 165 |
+
print("Presence of Hate:", presence_of_hate)
|
| 166 |
+
print("Explanation:", exp)
|
| 167 |
+
print("Hate Degree:", hate_degree)
|
| 168 |
+
print("Part Which Contains Hate:", part_which_contains_hate)
|
| 169 |
+
|
| 170 |
+
return {'title': video_title, 'url': video_url, 'len': video.length, 'lang': result['language'], 'summary': summary, 'sentiment': sentiment, 'foo': foo, 'hate': presence_of_hate, 'explanation': exp, 'degree': hate_degree, 'part': part_which_contains_hate, 'transcript': input_text}
|
| 171 |
+
except Exception as e:
|
| 172 |
+
print(f"An error occured: {str(e)}")
|
| 173 |
+
|
| 174 |
+
|