Spaces:
Sleeping
Sleeping
Download Program_Flask.py from ayanDataleads/YTAnalyzer: direct link, hf CLI and curl.
- Browser
- Download file 7.58 kB
-
https://huggingface.co/spaces/ayanDataleads/YTAnalyzer/resolve/main/Program_Flask.py
- Command line
-
hf download hf://spaces/ayanDataleads/YTAnalyzer/Program_Flask.py
-
curl -L -o Program_Flask.py https://huggingface.co/spaces/ayanDataleads/YTAnalyzer/resolve/main/Program_Flask.py
7.58 kB
| import csv | |
| import pytube | |
| from pytube import YouTube | |
| import moviepy.editor as mp | |
| from pydub import AudioSegment | |
| from urllib.parse import urlparse | |
| import os | |
| import whisper | |
| import openai | |
| import json | |
| import ffmpeg | |
| import yt_dlp | |
| model = whisper.load_model("large") | |
| def Analyze(video_link): | |
| video_url = f"{video_link}" | |
| # Your existing code for processing the video and analyzing with ChatGPT | |
| # Extracting details of the Video | |
| print("Extracting Video Details"'\n') | |
| try: | |
| video = YouTube(video_url) | |
| video_channel = video.author | |
| video_title = video.title | |
| video_description = video.description | |
| video_url = video.watch_url | |
| print(f"Channel Name: {video_channel}") | |
| print(f"Video Tile: {video_title}") | |
| print(f"Video URL: {video_url}") | |
| print(f"Video Description: {video_description}") | |
| except Exception as e: | |
| print(f"An error occured: {str(e)}") | |
| #--- | |
| try: | |
| # Determine if it's for YouTube or YouTube Shorts and save the audio \ | |
| # file according to its video ID | |
| parsed_url = urlparse(video_url) | |
| if 'youtube.com' in parsed_url.netloc: | |
| if '/shorts/' in parsed_url.path: | |
| # It's a YouTube Shorts URL | |
| video_id = parsed_url.path.split('/shorts/')[-1] | |
| else: | |
| # It's a regular YouTube video URL | |
| query_params = parsed_url.query.split('&') | |
| video_id = None | |
| for param in query_params: | |
| if param.startswith('v='): | |
| video_id = param[2:] | |
| break | |
| if video_id is None: | |
| print("Invalid YouTube video URL. No video ID found.") | |
| else: | |
| print("Invalid URL. Please provide a valid YouTube URL.") | |
| #--- | |
| # Downloading audio and converting to .wav format | |
| print("Downloading Audio") | |
| if video_id: | |
| # Replace 'VIDEO_URL' with the actual URL of the video you want to download | |
| # Options to download only audio in the best quality available | |
| options = { | |
| 'format': 'bestaudio/best', | |
| 'postprocessors': [{ | |
| 'key': 'FFmpegExtractAudio', | |
| 'preferredcodec': 'mp3', | |
| 'preferredquality': 'best', | |
| }], | |
| 'outtmpl': '%(id)s.%(ext)s', | |
| 'quiet': True, # Set quiet to True to suppress verbose output | |
| } | |
| # Create yt-dlp object with specified options | |
| ydl = yt_dlp.YoutubeDL(options) | |
| # Download the video | |
| ydl.download([video_url]) | |
| print("AUDIO DOWNLOADED") | |
| savepath_audio = f'{video_id}.mp3' | |
| print(savepath_audio) | |
| print('\n'"Audio path: ", savepath_audio) | |
| #Whisper Transcription | |
| print('\n'"Transcribing Audio, Please wait!"'\n') | |
| result = model.transcribe(savepath_audio, verbose=True, task='translate') | |
| os.remove(savepath_audio) | |
| combined_data = "" | |
| for segment in result['segments']: | |
| start_time = "[{:02d}:{:06.3f}".format(int(segment['start']) // 60, segment['start'] % 60) | |
| end_time = "{:02d}:{:06.3f}]".format(int(segment['end']) // 60, segment['end'] % 60) | |
| timestamp = start_time + ' --> ' + end_time | |
| text = segment['text'] | |
| # Combining transcript into a single string with newlines | |
| combined_data += timestamp + ' ' + text + '\n' | |
| #---- | |
| #Using ChatGPT for Analysis | |
| openai.api_key = os.environ.get('API_KEY') | |
| def callChatGPT(input_text): | |
| #input_prompt = "I have receive the message following message: " + '"' + str(input_text) + '"' + "." | |
| input_prompt = """you will be provided a transcript of a video with timestamp. perform the following actions keeping \ | |
| youtube commmunity guidelines, giving importance to freedom of speech and differentiating between fact and opinion,\ | |
| and the idea that people are free to believe what they want to believe and say what they want about other people with\ | |
| different viewpoints \ | |
| (as long as the latter do not belong to 'protected group' category i.e., video should not hateful towards age, caste, \ | |
| disability, ethnicity, gender identity and expression, nationality, race, immigration status, religion,\ | |
| sex/gender, sexual orientation, victims of a major violent event and their kin, and veteran status; if it is not \ | |
| against any protected group, then \ | |
| flag ONLY if the language is extremely hateful - ignore minor everyday foul deragatory language) in mind: | |
| 1 - summarize the text delimited by triple backticks with 1-2 sentence \ | |
| 2 - provide the overall sentiment of the text \ | |
| 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 \ | |
| 4 - check for any hate in the text. if it does then just say yes else say no \ | |
| 5 - if the text contains hate then mention all the timestamps and it's texts where it contains hate \ | |
| 6 - classify the risk/degree of hate into high, medium, or low \ | |
| 7 - if flagged as hate, explain why | |
| 8 - output a json object which contains the following keys: \ | |
| summary, sentiment, presence_of_hate, hate_degree, part_which_contains_hate, fact_or_opinion, explanation""" + '```' + str(input_text) + '```' + "." | |
| response = openai.ChatCompletion.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "user", "content": input_prompt} | |
| ], | |
| temperature=0, | |
| ) | |
| return response | |
| input_text = (f"""{combined_data}""") | |
| print('\n'"ANALYZING..."'\n') | |
| response_AI = callChatGPT(input_text) | |
| # Extracting values from the response | |
| response_data = response_AI['choices'][0]['message']['content'] | |
| response_json = json.loads(response_data) | |
| summary = response_json.get('summary', '') | |
| sentiment = response_json.get('sentiment', '') | |
| presence_of_hate = response_json.get('presence_of_hate', '') | |
| hate_degree = response_json.get('hate_degree', '') | |
| part_which_contains_hate = response_json.get('part_which_contains_hate', '') | |
| foo = response_json.get('fact_or_opinion', '') | |
| exp = response_json.get('explanation', '') | |
| print(text) | |
| print('\n') | |
| print("Summary:", summary) | |
| print("Sentiment:", sentiment) | |
| print("Fact or opinion: ", foo) | |
| print("Presence of Hate:", presence_of_hate) | |
| print("Explanation:", exp) | |
| print("Hate Degree:", hate_degree) | |
| print("Part Which Contains Hate:", part_which_contains_hate) | |
| 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} | |
| except Exception as e: | |
| print(f"An error occured: Unable to process") | |