Spaces:
Sleeping
Sleeping
Update app.py
Browse files3 unmodified lines45678910111213141516178 unmodified lines26272829303132333435363738394041422 unmodified lines4546474849505152535455565758596061626364656667683 unmodified linesfrom dotenv import load_dotenvimport osfrom groq import Groq# Load the API key from .env fileload_dotenv()api_key = os.getenv('GROQ_API_KEY')client = Groq(api_key=api_key)if not api_key:raise ValueError("API key is not set. Please check your .env file and ensure GROQ_API_KEY is set.")def get_transcript(url):try:8 unmodified linesexcept Exception as e:return f"Error: {str(e)}"def answer_question(transcript, question):try:response = client.chat.completions.create(model="mixtral-8x7b-32768",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": f"Using the following transcript as context and then go through the question and then please solve or answer the question correctly in 300 words:\n\nTranscript:\n{transcript}\n\nQuestion:\n{question}"}],max_tokens=500)if response.choices and response.choices[0].message:answer = response.choices[0].message.content.strip()return answer2 unmodified linesexcept Exception as e:return f"Error in answering question: {str(e)}"def handle_query(youtube_url, question):transcript = get_transcript(youtube_url)if "Error" in transcript:return transcriptanswer = answer_question(transcript, question)return answerst.title("YouTube Video Doubt Bot")youtube_url = st.text_input("YouTube URL", placeholder="Enter YouTube URL here...")if youtube_url:transcript = get_transcript(youtube_url)if "Error" in transcript:st.write(transcript)else:st.write("Transcript successfully loaded.")question = st.text_input("Ask a question about the video")if st.button("Get Answer"):answer = handle_query(youtube_url, question)st.write("### Answer")st.write(answer)3 unmodified lines456789101112131415161718192021222324258 unmodified lines34353637383940414243444546474849505152535455565758596061626364656667686970717273742 unmodified lines77787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161173 unmodified linesfrom dotenv import load_dotenvimport osfrom groq import Groqfrom openai import OpenAIimport tempfilefrom pydub import AudioSegmentimport io# Load the API keys from .env fileload_dotenv()groq_api_key = os.getenv('GROQ_API_KEY')openai_api_key = os.getenv('OPENAI_API_KEY')# Initialize clientsgroq_client = Groq(api_key=groq_api_key)openai_client = OpenAI(api_key=openai_api_key)if not groq_api_key or not openai_api_key:raise ValueError("API keys are not set. Please check your .env file and ensure GROQ_API_KEY and OPENAI_API_KEY are set.")def get_transcript(url):try:8 unmodified linesexcept Exception as e:return f"Error: {str(e)}"def convert_to_supported_format(file):audio = AudioSegment.from_file(file)buffer = io.BytesIO()audio.export(buffer, format="mp3")buffer.seek(0)return bufferdef transcribe_audio(file):file = convert_to_supported_format(file)with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:temp_file.write(file.getvalue())temp_file_path = temp_file.nametry:with open(temp_file_path, "rb") as audio_file:transcript = openai_client.audio.transcriptions.create(model="whisper-1",file=audio_file)return transcript.textexcept Exception as e:return f"Error in transcription: {str(e)}"finally:os.remove(temp_file_path)def answer_question(transcript, question):try:response = groq_client.chat.completions.create(model="mixtral-8x7b-32768",messages=[{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": f"Using the following transcript as context, please answer the question:\n\nTranscript:\n{transcript}\n\nQuestion:\n{question}"}],max_tokens=150)if response.choices and response.choices[0].message:answer = response.choices[0].message.content.strip()return answer2 unmodified linesexcept Exception as e:return f"Error in answering question: {str(e)}"def handle_query(transcript, question):if "Error" in transcript:return transcriptanswer = answer_question(transcript, question)return answerst.title("Video/Audio Doubt Bot")option = st.selectbox("Choose input type", ("YouTube URL", "Upload audio/video file"))if option == "YouTube URL":youtube_url = st.text_input("YouTube URL", placeholder="Enter YouTube URL here...")if youtube_url:transcript = get_transcript(youtube_url)if "Error" in transcript:st.write(transcript)else:st.write("Transcript successfully loaded.")question = st.text_input("Ask a question about the video")if st.button("Get Answer"):answer = handle_query(transcript, question)st.write("### Answer")st.write(answer)elif option == "Upload audio/video file":uploaded_file = st.file_uploader("Choose an audio or video file", type=["mp3", "mp4", "wav"])if uploaded_file is not None:with st.spinner('Transcribing audio...'):transcript = transcribe_audio(uploaded_file)if "Error" in transcript:st.write(transcript)else:st.write("File successfully transcribed.")question = st.text_input("Ask a question about the audio/video")if st.button("Get Answer"):answer = handle_query(transcript, question)st.write("### Answer")st.write(answer)
app.py+73-24