Spaces:
Running
on
Zero
Running
on
Zero
Gijs Wijngaard
commited on
Commit
·
27a9c0b
1
Parent(s):
c560ddc
init
Browse files- app.py +80 -4
- requirements.txt +6 -0
app.py
CHANGED
|
@@ -1,7 +1,83 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def greet(name):
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import soundfile as sf
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import Qwen2_5OmniForConditionalGeneration, Qwen2_5OmniProcessor
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
MODEL_ID = "Qwen/Qwen2.5-Omni-7B" if False else "Qwen/Qwen2.5-Omni-7B" # keep explicit string
|
| 8 |
+
|
| 9 |
+
model = Qwen2_5OmniForConditionalGeneration.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
torch_dtype="auto",
|
| 12 |
+
device_map="auto",
|
| 13 |
+
)
|
| 14 |
+
model.disable_talker()
|
| 15 |
+
|
| 16 |
+
processor = Qwen2_5OmniProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def run_omni(audio_path: str, instruction: str, max_tokens: int = 512) -> str:
|
| 20 |
+
if not audio_path:
|
| 21 |
+
return "Please upload an audio file."
|
| 22 |
+
|
| 23 |
+
system_text = (
|
| 24 |
+
"You are Qwen, a virtual human developed by the Qwen Team, Alibaba Group, "
|
| 25 |
+
"capable of perceiving auditory and visual inputs, as well as generating text and speech."
|
| 26 |
+
)
|
| 27 |
+
conversation = [
|
| 28 |
+
{
|
| 29 |
+
"role": "system",
|
| 30 |
+
"content": [{"type": "text", "text": system_text}],
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"role": "user",
|
| 34 |
+
"content": [
|
| 35 |
+
{"type": "audio", "audio_url": audio_path},
|
| 36 |
+
{"type": "text", "text": instruction},
|
| 37 |
+
],
|
| 38 |
+
},
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
text = processor.apply_chat_template(conversation, add_generation_prompt=True, tokenize=False)
|
| 42 |
+
|
| 43 |
+
audio, sr = sf.read(audio_path)
|
| 44 |
+
audios = [(audio, sr)]
|
| 45 |
+
|
| 46 |
+
inputs = processor(
|
| 47 |
+
text=text,
|
| 48 |
+
audio=audios,
|
| 49 |
+
images=[],
|
| 50 |
+
videos=[],
|
| 51 |
+
return_tensors="pt",
|
| 52 |
+
padding=True,
|
| 53 |
+
)
|
| 54 |
+
inputs = inputs.to(model.device)
|
| 55 |
+
|
| 56 |
+
output_ids = model.generate(**inputs, max_new_tokens=int(max_tokens))
|
| 57 |
+
response = processor.batch_decode(
|
| 58 |
+
output_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 59 |
+
)[0]
|
| 60 |
+
return response
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
with gr.Blocks(title="Qwen2.5 Omni (Audio) Demo") as demo:
|
| 64 |
+
gr.Markdown("# Qwen2.5-Omni (Audio) Demo")
|
| 65 |
+
gr.Markdown("Upload an audio file and provide an instruction for the model.")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column():
|
| 69 |
+
audio_input = gr.Audio(type="filepath", label="Upload Audio")
|
| 70 |
+
instruction = gr.Textbox(
|
| 71 |
+
label="Instruction",
|
| 72 |
+
value="Transcribe the audio, then summarize it in one sentence.",
|
| 73 |
+
)
|
| 74 |
+
max_tokens = gr.Slider(128, 2048, value=512, step=64, label="Max Output Tokens")
|
| 75 |
+
submit_btn = gr.Button("Run", variant="primary")
|
| 76 |
+
with gr.Column():
|
| 77 |
+
output_text = gr.Textbox(label="Response", lines=14)
|
| 78 |
+
|
| 79 |
+
submit_btn.click(run_omni, [audio_input, instruction, max_tokens], output_text)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
demo.queue().launch(share=False, ssr_mode=False)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|