File size: 4,094 Bytes
fe4da73
 
519bb2b
 
fe4da73
519bb2b
 
 
fe4da73
 
 
 
 
 
 
 
 
 
 
 
 
519bb2b
fe4da73
 
519bb2b
fe4da73
519bb2b
fe4da73
 
519bb2b
fe4da73
 
519bb2b
 
 
 
fe4da73
519bb2b
fe4da73
 
519bb2b
fe4da73
519bb2b
fe4da73
519bb2b
fe4da73
 
519bb2b
fe4da73
 
519bb2b
fe4da73
 
519bb2b
fe4da73
 
519bb2b
fe4da73
519bb2b
fe4da73
519bb2b
fe4da73
 
519bb2b
fe4da73
 
519bb2b
 
 
fe4da73
519bb2b
fe4da73
519bb2b
 
 
 
fe4da73
519bb2b
fe4da73
 
519bb2b
fe4da73
 
 
 
519bb2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe4da73
519bb2b
fe4da73
519bb2b
fe4da73
519bb2b
 
 
fe4da73
519bb2b
fe4da73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import time
import yt_dlp
import requests

# You can replace these URLs with Hugging Face API URLs
BASE_URL = "https://api-inference.huggingface.co/models/"
HEADERS = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_KEY"}  # Replace with your actual Hugging Face API key

# βœ… Download YouTube Audio
def download_audio(youtube_url, output_file="audio.mp3"):
    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': output_file,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192',
        }],
        'quiet': False,  # Set to False for more verbose output to help debug
    }

    try:
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            print("Downloading audio...")
            ydl.download([youtube_url])
        print(f"Audio downloaded successfully: {output_file}")
        return output_file
    except Exception as e:
        print("Error downloading audio:", str(e))
        return None

# βœ… Upload Audio to Hugging Face STT Model for Transcription
def get_transcription(file_path):
    with open(file_path, "rb") as audio_file:
        audio = audio_file.read()

    data = {"inputs": audio}

    try:
        response = requests.post(f"{BASE_URL}/whisper-large", headers=HEADERS, files={"file": audio})
        if response.status_code == 200:
            return response.json()["text"]
        else:
            print(f"Error during transcription request: {response.status_code}")
            return None
    except Exception as e:
        print(f"Error during transcription request: {str(e)}")
        return None

# βœ… Summarize Transcript using Hugging Face GPT-based Model
def summarize_text(transcript_text):
    data = {
        "inputs": f"Summarize the following text:\n\n{transcript_text}",
    }
    try:
        response = requests.post(f"{BASE_URL}/gpt2", headers=HEADERS, json=data)
        if response.status_code == 200:
            return response.json()[0]['generated_text']
        else:
            print(f"Error summarizing transcript: {response.status_code}")
            return None
    except Exception as e:
        print(f"Error summarizing transcript: {str(e)}")
        return None

# βœ… Text-to-Speech (TTS) using Hugging Face
def generate_tts_audio(summary_text):
    data = {"inputs": summary_text}
    try:
        response = requests.post(f"{BASE_URL}/tacotron2", headers=HEADERS, json=data)
        if response.status_code == 200:
            with open("summary_audio.wav", "wb") as audio_file:
                audio_file.write(response.content)
            print(f"Audio summary saved as: summary_audio.wav")
            return "summary_audio.wav"
        else:
            print(f"Error generating TTS audio: {response.status_code}")
            return None
    except Exception as e:
        print(f"Error generating TTS audio: {str(e)}")
        return None

# βœ… Full Pipeline Execution
def main():
    video_url = input("Please enter the YouTube video URL: ")  # User input for YouTube URL

    # Step 1: Download Audio from YouTube
    audio_file = download_audio(video_url)
    if audio_file:
        # Step 2: Get Transcription from Hugging Face
        transcript = get_transcription(audio_file)
        if transcript:
            print("Transcript:\n", transcript)

            # Step 3: Summarize the Transcript using Hugging Face Model
            summary = summarize_text(transcript)
            if summary:
                print("\nSummary:\n", summary)

                # Step 4: Generate TTS Audio for the Summary
                tts_audio = generate_tts_audio(summary)
                if tts_audio:
                    print(f"Text-to-Speech audio saved at: {tts_audio}")
                else:
                    print("Failed to generate TTS audio.")
            else:
                print("Failed to summarize transcript.")
        else:
            print("Failed to transcribe the audio.")
    else:
        print("Failed to download audio from the video.")

if __name__ == "__main__":
    main()