Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| import tempfile | |
| import subprocess | |
| from whisper import load_model | |
| import requests | |
| # --- Groq API Key --- | |
| GROQ_API_KEY = os.getenv('GROQ_API_KEY') | |
| st.title("π₯ Multilingual Video Translator App") | |
| st.write("Upload a video in any language, and translate it to your selected language!") | |
| # --- Video Upload --- | |
| video_file = st.file_uploader("π€ Upload a video", type=['mp4', 'mov', 'avi']) | |
| # --- Target Language Selection --- | |
| target_lang = st.selectbox("π Select Target Language", | |
| ["English", "Spanish", "French", "German", "Hindi", "Chinese", "Arabic", "Japanese"]) | |
| if video_file is not None: | |
| st.video(video_file) | |
| # Save uploaded video temporarily | |
| with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmpfile: | |
| tmpfile.write(video_file.read()) | |
| temp_video_path = tmpfile.name | |
| # --- Extract Audio using ffmpeg --- | |
| st.info("π Extracting audio using ffmpeg...") | |
| audio_output = "temp_audio.mp3" | |
| ffmpeg_cmd = f"ffmpeg -i {temp_video_path} -vn -acodec libmp3lame {audio_output} -y" | |
| subprocess.run(ffmpeg_cmd, shell=True) | |
| # --- Transcribe Audio using Whisper --- | |
| st.info("π Transcribing audio...") | |
| model = load_model("small") # Or "tiny" | |
| result = model.transcribe(audio_output) | |
| transcription = result['text'] | |
| st.success("β Transcription Complete") | |
| st.write("**Original Transcription:**") | |
| st.write(transcription) | |
| # --- Translate using Groq API --- | |
| st.info("π Translating text...") | |
| url = "https://api.groq.com/openai/v1/chat/completions" | |
| headers = { | |
| "Authorization": f"Bearer {GROQ_API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| messages = [ | |
| {"role": "system", "content": "You are a translation assistant."}, | |
| {"role": "user", "content": f"Translate this text to {target_lang}: {transcription}"} | |
| ] | |
| payload = { | |
| "model": "mixtral-8x7b-32768", | |
| "messages": messages, | |
| "temperature": 0.2 | |
| } | |
| response = requests.post(url, headers=headers, json=payload) | |
| if response.status_code == 200: | |
| translated_text = response.json()['choices'][0]['message']['content'] | |
| st.success(f"β Translated to {target_lang}:") | |
| st.write(translated_text) | |
| else: | |
| st.error(f"β Translation failed! {response.status_code}: {response.text}") | |
| # Cleanup | |
| os.remove(temp_video_path) | |
| os.remove(audio_output) | |
| st.write("---") | |
| st.write("π Made with β€οΈ using Whisper + Groq + Streamlit") | |