File size: 2,453 Bytes
6fa5743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import openai
from pytube import YouTube
import os
from pathlib import Path
import shutil
import whisper
from dotenv import load_dotenv
from zipfile import ZipFile 

load_dotenv()

openai.api_key = os.getenv("OPENAI_API_KEY")

@st.cache_data
def load_model():
    model = whisper.load_model("base")
    return model

def save_audio(url):
    yt = YouTube(url)
    video = yt.streams.filter(only_audio=True).first()
    out_file = video.download()
    base, ext = os.path.splitext(out_file)
    file_name = base + '.mp3'
    try:
        os.rename(out_file, file_name)
    except WindowsError:
        os.remove(file_name)
        os.rename(out_file, file_name)
    audio_filename = Path(file_name).stem+'.mp3'
    print(yt.title + " Has been successfully downloaded")
    print(file_name)
    return yt.title, audio_filename

def audio_to_transcript(audio_file):
    model = load_model()
    result = model.transcribe(audio_file)
    transcript = result["text"]
    return transcript

def text_to_news_article(text):
    response = openai.Completion.create(
    model="text-davinci-003",
    prompt="Write a news article in 500 words from the below text:\n"+text,
    temperature=0.7,
    max_tokens=600,
    top_p=1,
    frequency_penalty=0,
    presence_penalty=0
    )
    return response['choices'][0]['text']


st.markdown('# 📝 **News Article Generator App**')

st.header('Input the Video URL')

url_link = st.text_input('Enter URL of YouTube video:')

if st.checkbox('Start Analysis'):
    video_title, audio_filename = save_audio(url_link)
    st.audio(audio_filename)
    transcript = audio_to_transcript(audio_filename)
    st.header("Transcript are getting generated...")
    st.success(transcript)
    st.header("News Article")
    result = text_to_news_article(transcript)
    st.success(result)
    
    #save the files
    transcript_txt = open('transcript.txt', 'w')
    transcript_txt.write(transcript)
    transcript_txt.close()  
    
    article_txt = open('article.txt', 'w')
    article_txt.write(result) 
    article_txt.close() 
    
    zip_file = ZipFile('output.zip', 'w')
    zip_file.write('transcript.txt')
    zip_file.write('article.txt')
    zip_file.close()
    
    with open("output.zip", "rb") as zip_download:
        btn = st.download_button(
            label="Download ZIP",
            data=zip_download,
            file_name="output.zip",
            mime="application/zip"
        )