abdullahzunorain commited on
Commit
8886568
·
verified ·
1 Parent(s): 73e2d22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -42
app.py CHANGED
@@ -8,9 +8,13 @@ from groq import Groq
8
  st.title("Audio/Video Transcription and Summarization")
9
  st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
10
 
11
- # Get the API key from user input (You may want to use Streamlit secrets management)
12
- GROQ_API_KEY = st.text_input("Enter your Groq API Key:")
13
- os.environ["GROQ_API_KEY"] = GROQ_API_KEY
 
 
 
 
14
 
15
  # Create a temporary directory if it does not exist
16
  temp_dir = "temp"
@@ -41,44 +45,96 @@ def summarize_text(text):
41
  """Summarizes text using Groq API."""
42
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
43
  response = client.chat.completions.create(
44
- messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
45
- model="llama3-8b-8192"
46
- )
47
- summary = response.choices[0].message.content
48
- return summary
49
-
50
- # Complete function to process audio or video
51
- def process_media(media_file):
52
- """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
53
- # Save the uploaded file to a temporary path
54
- temp_file_path = os.path.join(temp_dir, media_file.name)
55
- with open(temp_file_path, "wb") as f:
56
- f.write(media_file.getbuffer())
57
-
58
- # Determine if the file is a video or audio based on the file extension
59
- if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
60
- # Step 1: Extract audio from video
61
- audio_path = extract_audio(temp_file_path)
62
- else:
63
- audio_path = temp_file_path # If it's already audio, use it as is
64
-
65
- # Step 2: Transcribe audio to text
66
- transcription = transcribe_audio(audio_path)
67
- st.write("### Transcription:")
68
- st.write(transcription)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
- # Step 3: Summarize transcription
71
- summary = summarize_text(transcription)
72
- st.write("### Summary:")
73
- st.write(summary)
74
 
75
- # Clean up temporary files if needed
76
- os.remove(temp_file_path)
77
- if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
78
- os.remove(audio_path)
79
-
80
- # Run the app
81
- if uploaded_file is not None and GROQ_API_KEY:
82
- process_media(uploaded_file)
83
- else:
84
- st.warning("Please upload a file and enter your Groq API key.")
 
8
  st.title("Audio/Video Transcription and Summarization")
9
  st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
10
 
11
+ # # Retrieve the API key from environment variables or Streamlit secrets
12
+ # GROQ_API_KEY = os.getenv("GROQ_API_KEY") or st.secrets["GROQ_API_KEY"]
13
+ # os.environ["GROQ_API_KEY"] = GROQ_API_KEY
14
+
15
+ # Set your Groq API key here or use environment variable
16
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
17
+ # client = Groq(api_key=GROQ_API_KEY)
18
 
19
  # Create a temporary directory if it does not exist
20
  temp_dir = "temp"
 
45
  """Summarizes text using Groq API."""
46
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
47
  response = client.chat.completions.create(
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+ # import os
58
+ # import ffmpeg
59
+ # import whisper
60
+ # import streamlit as st
61
+ # from groq import Groq
62
+
63
+ # # Set the title and description of the app
64
+ # st.title("Audio/Video Transcription and Summarization")
65
+ # st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
66
+
67
+ # # Get the API key from user input (You may want to use Streamlit secrets management)
68
+ # GROQ_API_KEY = st.text_input("Enter your Groq API Key:")
69
+ # os.environ["GROQ_API_KEY"] = GROQ_API_KEY
70
+
71
+ # # Create a temporary directory if it does not exist
72
+ # temp_dir = "temp"
73
+ # os.makedirs(temp_dir, exist_ok=True)
74
+
75
+ # # Upload the audio or video file
76
+ # uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
77
+
78
+ # # Function to extract audio from video
79
+ # def extract_audio(video_path, audio_path="temp/temp_audio.wav"):
80
+ # """Extracts audio from video."""
81
+ # try:
82
+ # # Run ffmpeg command with stderr capture for better error handling
83
+ # ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
84
+ # except ffmpeg.Error as e:
85
+ # st.error("FFmpeg error encountered: " + e.stderr.decode())
86
+ # return audio_path
87
+
88
+ # # Function to transcribe audio to text using Whisper model
89
+ # def transcribe_audio(audio_path):
90
+ # """Transcribes audio to text using Whisper model."""
91
+ # model = whisper.load_model("base") # Load the Whisper model
92
+ # result = model.transcribe(audio_path)
93
+ # return result["text"]
94
+
95
+ # # Function to summarize text using Groq API
96
+ # def summarize_text(text):
97
+ # """Summarizes text using Groq API."""
98
+ # client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
99
+ # response = client.chat.completions.create(
100
+ # messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
101
+ # model="llama3-8b-8192"
102
+ # )
103
+ # summary = response.choices[0].message.content
104
+ # return summary
105
+
106
+ # # Complete function to process audio or video
107
+ # def process_media(media_file):
108
+ # """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
109
+ # # Save the uploaded file to a temporary path
110
+ # temp_file_path = os.path.join(temp_dir, media_file.name)
111
+ # with open(temp_file_path, "wb") as f:
112
+ # f.write(media_file.getbuffer())
113
+
114
+ # # Determine if the file is a video or audio based on the file extension
115
+ # if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
116
+ # # Step 1: Extract audio from video
117
+ # audio_path = extract_audio(temp_file_path)
118
+ # else:
119
+ # audio_path = temp_file_path # If it's already audio, use it as is
120
+
121
+ # # Step 2: Transcribe audio to text
122
+ # transcription = transcribe_audio(audio_path)
123
+ # st.write("### Transcription:")
124
+ # st.write(transcription)
125
 
126
+ # # Step 3: Summarize transcription
127
+ # summary = summarize_text(transcription)
128
+ # st.write("### Summary:")
129
+ # st.write(summary)
130
 
131
+ # # Clean up temporary files if needed
132
+ # os.remove(temp_file_path)
133
+ # if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
134
+ # os.remove(audio_path)
135
+
136
+ # # Run the app
137
+ # if uploaded_file is not None and GROQ_API_KEY:
138
+ # process_media(uploaded_file)
139
+ # else:
140
+ # st.warning("Please upload a file and enter your Groq API key.")