Imageye commited on
Commit
2b56299
·
verified ·
1 Parent(s): be774bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -20
app.py CHANGED
@@ -1,21 +1,19 @@
1
  import streamlit as st
 
2
  from openai import OpenAI
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
  import re
5
  import tempfile
6
  import os
7
- from transformers import pipeline
8
- import soundfile as sf
9
 
10
- # Initialize the pipeline with the model
11
- pipe = pipeline("automatic-speech-recognition", model="openai/whisper-small")
12
-
13
- # Function to transcribe audio using Hugging Face Whisper
14
- def transcribe_audio(file_path):
15
- # Load audio file into NumPy array
16
- audio_input, _ = sf.read(file_path)
17
- transcription = pipe(audio_input)["text"]
18
- return transcription
19
 
20
  # Function to get YouTube transcript
21
  def get_transcript(url):
@@ -122,21 +120,21 @@ def handle_uploaded_file(uploaded_file):
122
  # Streamlit UI
123
  st.title("YouTube Transcript Quiz Generator")
124
 
125
- st.markdown("**Instructions:** Enter your OpenAI API key and paste a YouTube link or upload a media file to generate a quiz.")
126
-
127
- api_key = st.text_input("Enter your OpenAI API Key", type="password")
128
-
129
- if api_key:
130
- client = OpenAI(api_key=api_key)
131
 
 
 
132
  option = st.selectbox("Choose input type", ("YouTube URL", "Upload audio/video file"))
133
 
 
 
 
134
  if "generated_quiz" not in st.session_state:
135
  st.session_state.generated_quiz = False
136
 
137
  if option == "YouTube URL":
138
  url = st.text_input("YouTube URL", value="")
139
- if api_key and url:
140
  if st.button("Generate Quiz"):
141
  transcript_text = get_transcript(url)
142
  if "Error" not in transcript_text:
@@ -151,11 +149,11 @@ if option == "YouTube URL":
151
 
152
  if option == "Upload audio/video file":
153
  uploaded_file = st.file_uploader("Choose an audio or video file", type=["mp3", "wav", "mp4", "mov"])
154
- if uploaded_file and api_key:
155
  if st.button("Generate Quiz"):
156
  tmp_file_path = handle_uploaded_file(uploaded_file)
157
  with st.spinner('Transcribing audio...'):
158
- transcript_text = transcribe_audio(tmp_file_path)
159
  os.remove(tmp_file_path)
160
  if "Error" not in transcript_text:
161
  summary = summarize_text(client, transcript_text)
 
1
  import streamlit as st
2
+ import requests
3
  from openai import OpenAI
4
  from youtube_transcript_api import YouTubeTranscriptApi
5
  import re
6
  import tempfile
7
  import os
 
 
8
 
9
+ # Function to transcribe audio using Hugging Face Inference API
10
+ def transcribe_audio(api_key, file_path):
11
+ API_URL = "https://api-inference.huggingface.co/models/openai/whisper-large"
12
+ headers = {"Authorization": f"Bearer {api_key}"}
13
+ with open(file_path, "rb") as f:
14
+ data = f.read()
15
+ response = requests.post(API_URL, headers=headers, data=data)
16
+ return response.json().get("text", "Error: Could not transcribe audio")
 
17
 
18
  # Function to get YouTube transcript
19
  def get_transcript(url):
 
120
  # Streamlit UI
121
  st.title("YouTube Transcript Quiz Generator")
122
 
123
+ st.markdown("**Instructions:** Enter your OpenAI and Hugging Face API keys, and paste a YouTube link or upload a media file to generate a quiz.")
 
 
 
 
 
124
 
125
+ openai_api_key = st.text_input("Enter your OpenAI API Key", type="password")
126
+ hf_api_key = st.text_input("Enter your Hugging Face API Key", type="password")
127
  option = st.selectbox("Choose input type", ("YouTube URL", "Upload audio/video file"))
128
 
129
+ if openai_api_key:
130
+ client = OpenAI(api_key=openai_api_key)
131
+
132
  if "generated_quiz" not in st.session_state:
133
  st.session_state.generated_quiz = False
134
 
135
  if option == "YouTube URL":
136
  url = st.text_input("YouTube URL", value="")
137
+ if openai_api_key and hf_api_key and url:
138
  if st.button("Generate Quiz"):
139
  transcript_text = get_transcript(url)
140
  if "Error" not in transcript_text:
 
149
 
150
  if option == "Upload audio/video file":
151
  uploaded_file = st.file_uploader("Choose an audio or video file", type=["mp3", "wav", "mp4", "mov"])
152
+ if uploaded_file and openai_api_key and hf_api_key:
153
  if st.button("Generate Quiz"):
154
  tmp_file_path = handle_uploaded_file(uploaded_file)
155
  with st.spinner('Transcribing audio...'):
156
+ transcript_text = transcribe_audio(hf_api_key, tmp_file_path)
157
  os.remove(tmp_file_path)
158
  if "Error" not in transcript_text:
159
  summary = summarize_text(client, transcript_text)