Spaces:
Runtime error
Runtime error
Download app.py from mpara/texttoaudio: direct link, hf CLI and curl.
- Browser
- Download file 1.54 kB
-
https://huggingface.co/spaces/mpara/texttoaudio/resolve/main/app.py
- Command line
-
hf download hf://spaces/mpara/texttoaudio/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mpara/texttoaudio/resolve/main/app.py
1.54 kB
| import os | |
| from transformers import AutoTokenizer, AutoModelForTextToWaveform | |
| import gradio as gr | |
| from groq import Groq | |
| import torch | |
| # Load the Hugging Face model for text-to-waveform | |
| tokenizer = AutoTokenizer.from_pretrained("facebook/musicgen-medium") | |
| model = AutoModelForTextToWaveform.from_pretrained("facebook/musicgen-medium") | |
| # Initialize Groq API with API key directly | |
| client = Groq(api_key="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") # Place your API key here | |
| # Function to generate response and convert to audio | |
| def generate_audio_response(input_text): | |
| # Step 1: Generate text response from Groq API | |
| chat_completion = client.chat.completions.create( | |
| messages=[{"role": "user", "content": input_text}], | |
| model="llama3-8b-8192" | |
| ) | |
| response_text = chat_completion.choices[0].message.content | |
| # Step 2: Generate audio waveform from text using Hugging Face model | |
| inputs = tokenizer(response_text, return_tensors="pt") | |
| with torch.no_grad(): | |
| audio_waveform = model.generate(**inputs) | |
| # Convert audio tensor to numpy array for Gradio | |
| audio_np = audio_waveform.cpu().numpy().flatten() | |
| return (audio_np, 16000) # Returning audio and sample rate of 16 kHz | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=generate_audio_response, | |
| inputs="text", | |
| outputs="audio", | |
| title="Text-to-Audio Generator", | |
| description="Enter a text prompt, and this app will generate an audio response." | |
| ) | |
| # Launch the interface | |
| iface.launch() | |