deadshot2003 commited on
Commit
588a2b1
·
verified ·
1 Parent(s): 37fa51c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -24
app.py CHANGED
@@ -1,19 +1,52 @@
 
 
1
  import streamlit as st
2
  from youtube_transcript_api import YouTubeTranscriptApi
3
  import re
 
 
 
 
4
  from dotenv import load_dotenv
5
- import os
6
- from groq import Groq
7
 
 
 
8
 
 
 
9
 
10
- # Load the API key from .env file
11
- load_dotenv()
12
- api_key = os.getenv('GROQ_API_KEY')
13
- client = Groq(api_key=api_key)
14
 
15
- if not api_key:
16
- raise ValueError("API key is not set. Please check your .env file and ensure GROQ_API_KEY is set.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  def get_transcript(url):
19
  try:
@@ -31,32 +64,39 @@ def get_transcript(url):
31
  def summarize_text(text):
32
  try:
33
  response = client.chat.completions.create(
34
- model="mixtral-8x7b-32768",
35
  messages=[
36
  {"role": "system", "content": "You are a helpful assistant."},
37
  {"role": "user", "content": f"Please provide a concise summary of the following text:\n\n{text}"}
38
  ],
39
  max_tokens=150
40
  )
41
-
42
- if response.choices and response.choices[0].message:
43
- summary = response.choices[0].message.content.strip()
44
- return summary
45
- else:
46
- return "No summary available."
47
  except Exception as e:
48
  return f"Error in summarizing text: {str(e)}"
49
 
50
- def handle_summary(youtube_url):
51
- transcript = get_transcript(youtube_url)
52
- if "Error" in transcript:
53
- return transcript
54
- summary = summarize_text(transcript)
55
  return summary
56
 
57
  st.title("YouTube Summary Generator")
58
- youtube_url = st.text_input("YouTube URL", placeholder="Enter YouTube URL here...")
59
 
60
- if st.button("Generate Summary"):
61
- summary = handle_summary(youtube_url)
62
- st.text_area("Summary", summary, height=200)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import io
3
  import streamlit as st
4
  from youtube_transcript_api import YouTubeTranscriptApi
5
  import re
6
+ import logging
7
+ import warnings
8
+ from pydub import AudioSegment
9
+ import tempfile
10
  from dotenv import load_dotenv
11
+ from openai import OpenAI
 
12
 
13
+ # Load environment variables
14
+ load_dotenv()
15
 
16
+ # Initialize the OpenAI client
17
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
18
 
19
+ # Set up logging
20
+ logging.basicConfig(level=logging.INFO)
 
 
21
 
22
+ def convert_to_supported_format(file):
23
+ audio = AudioSegment.from_file(file)
24
+ buffer = io.BytesIO()
25
+ audio.export(buffer, format="wav")
26
+ buffer.seek(0)
27
+ return buffer
28
+
29
+ def transcribe_audio(file):
30
+ logging.info("Transcribing audio file")
31
+ file = convert_to_supported_format(file)
32
+ logging.info("Converted file to WAV format")
33
+
34
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
35
+ temp_file.write(file.getvalue())
36
+ temp_file_path = temp_file.name
37
+
38
+ try:
39
+ with open(temp_file_path, "rb") as audio_file:
40
+ transcript = client.audio.transcriptions.create(
41
+ model="whisper-1",
42
+ file=audio_file
43
+ )
44
+ return transcript.text
45
+ except Exception as e:
46
+ logging.error(f"Error in transcription: {str(e)}")
47
+ return f"Error in transcription: {str(e)}"
48
+ finally:
49
+ os.remove(temp_file_path)
50
 
51
  def get_transcript(url):
52
  try:
 
64
  def summarize_text(text):
65
  try:
66
  response = client.chat.completions.create(
67
+ model="gpt-3.5-turbo",
68
  messages=[
69
  {"role": "system", "content": "You are a helpful assistant."},
70
  {"role": "user", "content": f"Please provide a concise summary of the following text:\n\n{text}"}
71
  ],
72
  max_tokens=150
73
  )
74
+ return response.choices[0].message.content.strip()
 
 
 
 
 
75
  except Exception as e:
76
  return f"Error in summarizing text: {str(e)}"
77
 
78
+ def handle_summary(transcript_text):
79
+ if "Error" in transcript_text:
80
+ return transcript_text
81
+ summary = summarize_text(transcript_text)
 
82
  return summary
83
 
84
  st.title("YouTube Summary Generator")
 
85
 
86
+ option = st.selectbox("Choose input type", ("YouTube URL", "Upload audio/video file"))
87
+
88
+ if option == "YouTube URL":
89
+ youtube_url = st.text_input("YouTube URL", placeholder="Enter YouTube URL here...")
90
+ if st.button("Generate Summary"):
91
+ transcript = get_transcript(youtube_url)
92
+ summary = handle_summary(transcript)
93
+ st.text_area("Summary", summary, height=200)
94
+
95
+ elif option == "Upload audio/video file":
96
+ uploaded_file = st.file_uploader("Choose an audio or video file", type=["mp3", "wav", "mp4", "mov"])
97
+ if uploaded_file is not None:
98
+ if st.button("Generate Summary"):
99
+ with st.spinner('Transcribing audio...'):
100
+ transcript_text = transcribe_audio(uploaded_file)
101
+ summary = handle_summary(transcript_text)
102
+ st.text_area("Summary", summary, height=200)