desiree commited on
Commit
7abb7ba
·
verified ·
1 Parent(s): e55ad0a

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +56 -29
  2. requirements.txt +0 -1
app.py CHANGED
@@ -1,27 +1,25 @@
1
  import gradio as gr
2
  import spaces
3
- from transformers import AutoTokenizer
4
  import torch
5
  import os
6
  from threading import Thread
7
  import uuid
8
  import soundfile as sf
9
  import numpy as np
10
- import ctranslate2
11
- from huggingface_hub import hf_hub_download
12
 
13
  # Model and Tokenizer Loading
14
- MODEL_ID = "NexaAIDev/Qwen2-Audio-7B-GGUF"
15
- TOKENIZER_ID = "NexaAIDev/Qwen2-Audio-7B" # Use the base model's tokenizer
 
 
 
 
 
 
16
 
17
- # Download the GGUF model file
18
- model_path = hf_hub_download(MODEL_ID, "model.gguf")
19
-
20
- # Initialize the model and tokenizer
21
- generator = ctranslate2.Generator(model_path, device="cuda")
22
- tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_ID, trust_remote_code=True)
23
-
24
- DESCRIPTION = "[Qwen2-Audio-7B Demo](https://huggingface.co/NexaAIDev/Qwen2-Audio-7B-GGUF)"
25
 
26
  audio_extensions = (".wav", ".mp3", ".ogg", ".flac")
27
 
@@ -40,26 +38,55 @@ def qwen_inference(audio_input, text_input=None):
40
  # Process audio input
41
  audio_data, sample_rate = process_audio(audio_input)
42
 
43
- # Prepare the prompt
44
  if text_input:
45
- prompt = f"Below is an audio clip. {text_input}"
46
  else:
47
- prompt = "Please describe what you hear in this audio clip."
48
 
49
- # Tokenize input
50
- tokens = tokenizer.encode(prompt)
51
-
52
- # Generate response
53
- results = generator.generate_batch(
54
- [tokens],
55
- max_length=512,
56
- sampling_temperature=0.7,
57
- sampling_topk=50,
58
- include_prompt_in_result=False
 
 
 
 
 
 
 
 
 
 
 
59
  )
60
-
61
- response = tokenizer.decode(results[0].sequences_ids[0])
62
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  css = """
65
  #output {
 
1
  import gradio as gr
2
  import spaces
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
  import torch
5
  import os
6
  from threading import Thread
7
  import uuid
8
  import soundfile as sf
9
  import numpy as np
10
+ from transformers.generation import TextIteratorStreamer
 
11
 
12
  # Model and Tokenizer Loading
13
+ MODEL_ID = "Qwen/Qwen-Audio-Chat"
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ MODEL_ID,
16
+ torch_dtype=torch.float16,
17
+ device_map="auto",
18
+ trust_remote_code=True
19
+ )
20
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
21
 
22
+ DESCRIPTION = "[Qwen-Audio-Chat Demo](https://huggingface.co/Qwen/Qwen-Audio-Chat)"
 
 
 
 
 
 
 
23
 
24
  audio_extensions = (".wav", ".mp3", ".ogg", ".flac")
25
 
 
38
  # Process audio input
39
  audio_data, sample_rate = process_audio(audio_input)
40
 
41
+ # Prepare the messages
42
  if text_input:
43
+ query = text_input
44
  else:
45
+ query = "Please describe what you hear in this audio clip."
46
 
47
+ messages = [
48
+ {
49
+ "role": "user",
50
+ "content": [
51
+ {
52
+ "type": "audio",
53
+ "audio": audio_input,
54
+ },
55
+ {
56
+ "type": "text",
57
+ "text": query,
58
+ },
59
+ ],
60
+ }
61
+ ]
62
+
63
+ # Convert messages to model input format
64
+ text = tokenizer.apply_chat_template(
65
+ messages,
66
+ tokenize=False,
67
+ add_generation_prompt=True
68
  )
69
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
70
+
71
+ # Set up streamer for real-time output
72
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
73
+ generation_kwargs = dict(
74
+ model_inputs,
75
+ streamer=streamer,
76
+ max_new_tokens=512,
77
+ temperature=0.7,
78
+ do_sample=True
79
+ )
80
+
81
+ # Start generation in a separate thread
82
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
83
+ thread.start()
84
+
85
+ # Stream the output
86
+ buffer = ""
87
+ for new_text in streamer:
88
+ buffer += new_text
89
+ yield buffer
90
 
91
  css = """
92
  #output {
requirements.txt CHANGED
@@ -4,4 +4,3 @@ transformers>=4.36.0
4
  soundfile>=0.12.1
5
  numpy>=1.24.0
6
  huggingface-hub>=0.19.0
7
- ctranslate2>=3.23.0
 
4
  soundfile>=0.12.1
5
  numpy>=1.24.0
6
  huggingface-hub>=0.19.0