Gijs Wijngaard commited on
Commit Β·
3d1304c
1
Parent(s): 9a41897
init
Browse files- app.py +72 -0
- requirements.txt +7 -0
app.py
CHANGED
|
@@ -1,4 +1,76 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def greet(name):
|
| 4 |
return "Hello " + name + "!!"
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import io
|
| 3 |
+
from urllib.request import urlopen
|
| 4 |
+
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
MODEL_ID = "microsoft/Phi-4-multimodal-instruct"
|
| 11 |
+
|
| 12 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
device_map="cuda" if torch.cuda.is_available() else "cpu",
|
| 16 |
+
torch_dtype="auto",
|
| 17 |
+
trust_remote_code=True,
|
| 18 |
+
_attn_implementation="flash_attention_2",
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
generation_config = GenerationConfig.from_pretrained(MODEL_ID)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def run_phi4(audio_path: str, instruction: str, max_tokens: int = 512) -> str:
|
| 25 |
+
if not audio_path:
|
| 26 |
+
return "Please upload an audio file."
|
| 27 |
+
|
| 28 |
+
audio, samplerate = sf.read(audio_path)
|
| 29 |
+
|
| 30 |
+
user_prompt = "<|user|>"
|
| 31 |
+
assistant_prompt = "<|assistant|>"
|
| 32 |
+
prompt_suffix = "<|end|>"
|
| 33 |
+
|
| 34 |
+
prompt = f"{user_prompt}<|audio_1|>{instruction}{prompt_suffix}{assistant_prompt}"
|
| 35 |
+
|
| 36 |
+
inputs = processor(text=prompt, audios=[(audio, samplerate)], return_tensors="pt").to(model.device)
|
| 37 |
+
|
| 38 |
+
output_ids = model.generate(
|
| 39 |
+
**inputs,
|
| 40 |
+
max_new_tokens=int(max_tokens),
|
| 41 |
+
generation_config=generation_config,
|
| 42 |
+
)
|
| 43 |
+
output_ids = output_ids[:, inputs["input_ids"].shape[1]:]
|
| 44 |
+
response = processor.batch_decode(output_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 45 |
+
return response
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
with gr.Blocks(title="Phi-4 Multimodal Audio Demo") as demo:
|
| 49 |
+
gr.Markdown("# Phi-4 Multimodal (Audio) Demo")
|
| 50 |
+
gr.Markdown("Upload an audio file and run instructions with Phi-4.")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
with gr.Column():
|
| 54 |
+
audio_input = gr.Audio(type="filepath", label="Upload Audio")
|
| 55 |
+
instruction = gr.Textbox(
|
| 56 |
+
label="Instruction",
|
| 57 |
+
value=(
|
| 58 |
+
"Transcribe the audio to text, and then translate the audio to French. "
|
| 59 |
+
"Use <sep> as a separator between the original transcript and the translation."
|
| 60 |
+
),
|
| 61 |
+
)
|
| 62 |
+
max_tokens = gr.Slider(128, 2000, value=1000, step=64, label="Max Output Tokens")
|
| 63 |
+
submit_btn = gr.Button("Run", variant="primary")
|
| 64 |
+
with gr.Column():
|
| 65 |
+
output_text = gr.Textbox(label="Model Response", lines=14)
|
| 66 |
+
|
| 67 |
+
submit_btn.click(run_phi4, [audio_input, instruction, max_tokens], output_text)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
demo.queue().launch(share=False, ssr_mode=False)
|
| 72 |
+
|
| 73 |
+
import gradio as gr
|
| 74 |
|
| 75 |
def greet(name):
|
| 76 |
return "Hello " + name + "!!"
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
torch>=2.1.0
|
| 3 |
+
transformers>=4.43.0
|
| 4 |
+
accelerate>=0.30.0
|
| 5 |
+
soundfile>=0.12.1
|
| 6 |
+
sentencepiece>=0.1.99
|
| 7 |
+
|