chburhan64's picture
Update app.py
519bb2b verified
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()