Spaces:
Runtime error
Runtime error
File size: 3,361 Bytes
6fa5743 1025165 6fa5743 1025165 6fa5743 1025165 6fa5743 1025165 6fa5743 0939b3c 6fa5743 1025165 |
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 117 |
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()
# Load OpenAI API key from .env
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']
def answer_question(question, context):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Q: {question}\nContext: {context}\nAnswer:",
temperature=0.7,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
answer = response.choices[0].text
return answer
st.markdown('# Generate and Chat with Transcripts')
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)
if not transcript:
st.error("Transcript generation failed. Please try another video or check the input.")
else:
st.header("Transcript")
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"
)
st.header("Ask Questions")
user_question = st.text_input("Ask a question:")
if st.button("Get Answer"):
if transcript:
answer = answer_question(user_question, transcript)
st.success("Answer: " + answer)
else:
st.error("No transcript available. Please analyze a video first.")
|