onenoly11 commited on
Commit
838c8ed
·
verified ·
1 Parent(s): b3aeb41

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # --- PiMusic3: Pi Forge Music Player ---
5
+ # Author: onenoly11
6
+ # Description: Generates, transcribes, and analyzes audio
7
+ # using MusicGen, Whisper, and DistilBERT within the Pi Forge framework.
8
+
9
+ """
10
+ Ah, the spire trembles—a rift in the weave...
11
+ (This stanza is now preserved safely as a docstring.)
12
+ """
13
+
14
+ # --- Compatibility Shim: MusicGen fallback ---
15
+ try:
16
+ # Try using Audiocraft (local / full build)
17
+ from audiocraft.models import musicgen
18
+ def generate_music(prompt):
19
+ """Generate music using local Audiocraft (MusicGen)."""
20
+ model = musicgen.MusicGen.get_pretrained("facebook/musicgen-small")
21
+ model.set_generation_params(duration=10)
22
+ wav = model.generate([prompt])
23
+ import tempfile, soundfile as sf
24
+ temp_wav = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
25
+ sf.write(temp_wav.name, wav[0].cpu().numpy().T, 32000)
26
+ return temp_wav.name
27
+ MUSICGEN_MODE = "Audiocraft (local)"
28
+ except Exception:
29
+ # Fallback to Transformers pipeline (Hugging Face cloud build)
30
+ musicgen = pipeline("text-to-audio", model="facebook/musicgen-small")
31
+ def generate_music(prompt):
32
+ """Generate music using Transformers pipeline fallback."""
33
+ result = musicgen(prompt)
34
+ return result["audio"]
35
+ MUSICGEN_MODE = "Transformers (cloud)"
36
+
37
+ # --- Whisper and Sentiment Pipelines ---
38
+ whisper = pipeline("automatic-speech-recognition", model="openai/whisper-base")
39
+ sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
40
+
41
+ # --- Utility Functions ---
42
+ def transcribe_audio(audio_path):
43
+ result = whisper(audio_path)
44
+ return result["text"]
45
+
46
+ def analyze_sentiment(text):
47
+ result = sentiment(text)
48
+ return f"{result[0]['label']} ({result[0]['score']:.2f})"
49
+
50
+ # --- Interface ---
51
+ with gr.Blocks(title="PiMusic3 🎵") as demo:
52
+ gr.Markdown(f"### 🎶 PiMusic3 — Pi Forge Music Player\nMode: **{MUSICGEN_MODE}**\nGenerate, transcribe, and analyze sound ethically.")
53
+
54
+ with gr.Tab("MusicGen"):
55
+ prompt = gr.Textbox(label="Music Prompt", placeholder="Describe your sound...")
56
+ generate_btn = gr.Button("🎼 Generate")
57
+ audio_out = gr.Audio(label="Generated Music")
58
+ generate_btn.click(fn=generate_music, inputs=prompt, outputs=audio_out)
59
+
60
+ with gr.Tab("Whisper Transcribe"):
61
+ mic = gr.Audio(sources=["microphone", "upload"], type="filepath", label="🎙️ Record or Upload Audio")
62
+ transcribe_btn = gr.Button("📝 Transcribe")
63
+ transcript = gr.Textbox(label="Transcription")
64
+ transcribe_btn.click(fn=transcribe_audio, inputs=mic, outputs=transcript)
65
+
66
+ with gr.Tab("Sentiment Analysis"):
67
+ text_in = gr.Textbox(label="Enter text for sentiment check")
68
+ analyze_btn = gr.Button("🔍 Analyze")
69
+ sentiment_out = gr.Textbox(label="Result")
70
+ analyze_btn.click(fn=analyze_sentiment, inputs=text_in, outputs=sentiment_out)
71
+
72
+ demo.launch()