Spaces:
Runtime error
Runtime error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159import gradio as grfrom groq import Groqfrom langchain_google_genai import ChatGoogleGenerativeAIimport osimport tempfile
# -------------------- API Configuration --------------------# Set your API keys as environment variables in Hugging Face Spaces settingsGROQ_API_KEY = os.environ.get("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", "")GOOGLE_API_KEY = os.environ.get("AIzaSyD2DMFgcL0kWTQYhii8wseSHY3BRGWSebk", "")
# Initialize clientsclient = Groq(api_key=GROQ_API_KEY)llm = ChatGoogleGenerativeAI( model="gemini-2.0-flash", google_api_key=GOOGLE_API_KEY, max_output_tokens=500)
# -------------------- Core Functions --------------------def transcribe_audio(audio_path, language="ar"): """Transcribe audio file using Groq Whisper""" try: with open(audio_path, "rb") as audio_file: transcription = client.audio.transcriptions.create( file=(os.path.basename(audio_path), audio_file.read()), model="whisper-large-v3-turbo", response_format="verbose_json", language=language ) return transcription.text, transcription.language except Exception as e: return f"Error in transcription: {str(e)}", None
def get_ai_response(text): """Get AI response from Gemini""" try: response = llm.invoke(text) return response.content except Exception as e: return f"Error getting AI response: {str(e)}"
def text_to_speech(text, language="ar"): """Convert text to speech using Groq TTS""" try: # Select voice based on language if language == "ar": voice = "Amira-PlayAI" model = "playai-tts-arabic" else: voice = "alloy" # Default English voice model = "playai-tts" response = client.audio.speech.create( model=model, voice=voice, response_format="mp3", input=text, ) # Save to temporary file temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") response.write_to_file(temp_file.name) return temp_file.name except Exception as e: return None
# -------------------- Gradio Interface Function --------------------def process_voice_chat(audio, language_choice): """Main function to process voice input and generate response""" if audio is None: return "Please provide an audio input", "", None # Map language choice to code lang_map = {"Arabic": "ar", "English": "en"} lang_code = lang_map.get(language_choice, "ar") # Step 1: Transcribe audio transcription, detected_lang = transcribe_audio(audio, lang_code) if transcription.startswith("Error"): return transcription, "", None # Step 2: Get AI response ai_response = get_ai_response(transcription) if ai_response.startswith("Error"): return transcription, ai_response, None # Step 3: Convert response to speech # Use detected language if available, otherwise use selected language output_lang = detected_lang if detected_lang else lang_code audio_output = text_to_speech(ai_response, output_lang) return transcription, ai_response, audio_output
# -------------------- Gradio Interface --------------------with gr.Blocks(title="Voice Chat Assistant", theme=gr.themes.Soft()) as demo: gr.Markdown( """ # 🎤 Voice Chat Assistant Upload an audio file or record your voice to chat with AI. The AI will respond in the same language! """ ) with gr.Row(): with gr.Column(): language = gr.Radio( choices=["Arabic", "English"], value="Arabic", label="Select Language" ) audio_input = gr.Audio( sources=["microphone", "upload"], type="filepath", label="Record or Upload Audio" ) submit_btn = gr.Button("Process", variant="primary", size="lg") with gr.Column(): transcription_output = gr.Textbox( label="Your Message (Transcription)", lines=3 ) ai_response_output = gr.Textbox( label="AI Response", lines=5 ) audio_output = gr.Audio( label="AI Voice Response", type="filepath" ) # Button action submit_btn.click( fn=process_voice_chat, inputs=[audio_input, language], outputs=[transcription_output, ai_response_output, audio_output] ) gr.Markdown( """ ### 📝 Instructions: 1. Select your language (Arabic or English) 2. Record your voice using the microphone or upload an audio file 3. Click "Process" to get AI response with voice output ### 🔑 Setup for Hugging Face Spaces: Add these secrets in your Space settings: - `GROQ_API_KEY`: Your Groq API key - `GOOGLE_API_KEY`: Your Google API key """ )
# Launch the appif __name__ == "__main__": demo.launch()app.pyAdded+159