Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,4 @@
|
|
1 |
-
import requests
|
2 |
import gradio as gr
|
3 |
-
import os
|
4 |
-
import time
|
5 |
-
from pytubefix import YouTube
|
6 |
-
from moviepy.editor import VideoFileClip
|
7 |
from transformers import pipeline
|
8 |
|
9 |
# Whisper model
|
@@ -12,45 +7,8 @@ asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-smal
|
|
12 |
# Summarization model
|
13 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
14 |
|
15 |
-
|
16 |
-
def get_po_token():
|
17 |
try:
|
18 |
-
response = requests.get("http://localhost:3001/get-token") # Call Node.js API
|
19 |
-
response.raise_for_status()
|
20 |
-
return response.json().get("poToken") # Extract token
|
21 |
-
except Exception as e:
|
22 |
-
print(f"Error fetching PO token: {e}")
|
23 |
-
return None # Return None if the request fails
|
24 |
-
|
25 |
-
def process_youtube_link(youtube_url):
|
26 |
-
try:
|
27 |
-
print(f"Received YouTube URL: {youtube_url}") # Debugging
|
28 |
-
|
29 |
-
po_token = get_po_token()
|
30 |
-
if not po_token:
|
31 |
-
return "Error: Unable to fetch PO token.", ""
|
32 |
-
|
33 |
-
# Use the token with pytubefix
|
34 |
-
yt = YouTube(youtube_url, po_token=po_token)
|
35 |
-
title = yt.title
|
36 |
-
print(f"Downloading: {title}")
|
37 |
-
|
38 |
-
ys = yt.streams.get_highest_resolution()
|
39 |
-
video_path = f"{title}.mp4"
|
40 |
-
ys.download(filename=video_path)
|
41 |
-
|
42 |
-
# Log download success
|
43 |
-
if os.path.exists(video_path):
|
44 |
-
print(f"Download successful: {video_path}")
|
45 |
-
print("Files in current directory:", os.listdir("."))
|
46 |
-
else:
|
47 |
-
print("Download failed!")
|
48 |
-
|
49 |
-
# Extract Audio
|
50 |
-
audio_path = f"{title}.wav"
|
51 |
-
video = VideoFileClip(video_path)
|
52 |
-
video.audio.write_audiofile(audio_path, codec="pcm_s16le")
|
53 |
-
|
54 |
# Transcribe Audio
|
55 |
transcription = asr(audio_path, return_timestamps=True)
|
56 |
transcribed_text = transcription["text"]
|
@@ -59,18 +17,17 @@ def process_youtube_link(youtube_url):
|
|
59 |
summary = summarizer(transcribed_text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
60 |
|
61 |
return transcribed_text, summary
|
62 |
-
|
63 |
except Exception as e:
|
64 |
print(f"Error: {str(e)}") # Log errors
|
65 |
return f"Error: {str(e)}", ""
|
66 |
|
67 |
# Gradio Interface
|
68 |
iface = gr.Interface(
|
69 |
-
fn=
|
70 |
-
inputs=gr.
|
71 |
outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Summary")],
|
72 |
-
title="
|
73 |
-
description="
|
74 |
)
|
75 |
|
76 |
-
iface.launch()
|
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
2 |
from transformers import pipeline
|
3 |
|
4 |
# Whisper model
|
|
|
7 |
# Summarization model
|
8 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
9 |
|
10 |
+
def process_audio(audio_path):
|
|
|
11 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
# Transcribe Audio
|
13 |
transcription = asr(audio_path, return_timestamps=True)
|
14 |
transcribed_text = transcription["text"]
|
|
|
17 |
summary = summarizer(transcribed_text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
|
18 |
|
19 |
return transcribed_text, summary
|
|
|
20 |
except Exception as e:
|
21 |
print(f"Error: {str(e)}") # Log errors
|
22 |
return f"Error: {str(e)}", ""
|
23 |
|
24 |
# Gradio Interface
|
25 |
iface = gr.Interface(
|
26 |
+
fn=process_audio,
|
27 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
28 |
outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Summary")],
|
29 |
+
title="Audio Summarizer",
|
30 |
+
description="Upload an audio file, and this app will transcribe and summarize its content.",
|
31 |
)
|
32 |
|
33 |
+
iface.launch()
|