Spaces:
Running
on
Zero
Running
on
Zero
VanguardAI
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,144 +1,82 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
5 |
import gradio as gr
|
|
|
|
|
6 |
import os
|
7 |
-
import logging
|
8 |
-
from unsloth import FastLanguageModel
|
9 |
-
import subprocess
|
10 |
-
loler=gr.SimpleCSVLogger()
|
11 |
-
# Set up logging to file
|
12 |
-
log_file_path = "app.log" # Name of your log file
|
13 |
-
logging.basicConfig(
|
14 |
-
filename=log_file_path,
|
15 |
-
level=logging.DEBUG,
|
16 |
-
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
17 |
-
)
|
18 |
-
logger = logging.getLogger(__name__)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
if 'not found' in cuda_version.lower():
|
95 |
-
raise RuntimeError("CUDA not found.")
|
96 |
-
|
97 |
-
# Load model and tokenizer
|
98 |
-
model, tokenizer = FastLanguageModel.from_pretrained(
|
99 |
-
model_name = "VanguardAI/CoT_multi_llama_LoRA_4bit",
|
100 |
-
max_seq_length = 2048,
|
101 |
-
dtype = torch.bfloat16,
|
102 |
-
load_in_4bit = True,
|
103 |
-
token = READ_HF
|
104 |
-
)
|
105 |
-
logger.info("Model and tokenizer loaded.")
|
106 |
-
|
107 |
-
# Format the prompt
|
108 |
-
formatted_prompt = alpaca_prompt.format(
|
109 |
-
string + inventory_list,
|
110 |
-
user_input_text,
|
111 |
-
"",
|
112 |
-
)
|
113 |
-
logger.debug(f"Formatted prompt: {formatted_prompt}")
|
114 |
-
|
115 |
-
# Tokenize the input
|
116 |
-
inputs = tokenizer([formatted_prompt], return_tensors="pt").to("cuda")
|
117 |
-
logger.debug(f"Tokenized inputs: {inputs}")
|
118 |
-
|
119 |
-
# Generate output
|
120 |
-
outputs = model.generate(**inputs, max_new_tokens=216, use_cache=True)
|
121 |
-
logger.info("Output generated.")
|
122 |
-
|
123 |
-
# Decode output
|
124 |
-
reply = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
125 |
-
logger.debug(f"Decoded output: {reply}")
|
126 |
-
|
127 |
-
logger.debug(f"Final reply: {reply}")
|
128 |
-
return reply
|
129 |
-
|
130 |
-
except Exception as e:
|
131 |
-
logger.error(f"Error loading model or CUDA issues: {e}")
|
132 |
-
return "There seems to be an issue with CUDA or the model. Please check the Hugging Face Spaces environment."
|
133 |
-
|
134 |
-
# Interface for inputs
|
135 |
-
iface = gr.Interface(
|
136 |
-
fn=chunk_it,
|
137 |
-
inputs=[
|
138 |
-
gr.Textbox(label="user_input_text", lines=3),
|
139 |
-
gr.Textbox(label="inventory_list", lines=5)
|
140 |
-
],
|
141 |
-
outputs=gr.Textbox(label="output", lines=23),
|
142 |
-
title="Testing",
|
143 |
-
flagging_callback=SimpleCSVLogger()
|
144 |
-
)
|
|
|
1 |
+
import sounddevice as sd
|
2 |
+
import scipy.io.wavfile as wavfile
|
3 |
+
import numpy as np
|
|
|
4 |
import gradio as gr
|
5 |
+
from groq import Groq
|
6 |
+
import tempfile
|
7 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
class Recorder:
|
10 |
+
def __init__(self, sample_rate=44100):
|
11 |
+
self.recording = False
|
12 |
+
self.frames = []
|
13 |
+
self.sample_rate = sample_rate
|
14 |
+
self.stream = None
|
15 |
+
|
16 |
+
def toggle_recording(self):
|
17 |
+
if not self.recording:
|
18 |
+
self.frames = []
|
19 |
+
self.stream = sd.InputStream(callback=self.callback, channels=2, samplerate=self.sample_rate)
|
20 |
+
self.stream.start()
|
21 |
+
self.recording = True
|
22 |
+
return "Recording... Press to Stop"
|
23 |
+
else:
|
24 |
+
self.stream.stop()
|
25 |
+
self.stream.close()
|
26 |
+
self.recording = False
|
27 |
+
return "Recording stopped. Press to Record"
|
28 |
+
|
29 |
+
def callback(self, indata, frames, time, status):
|
30 |
+
if self.recording:
|
31 |
+
self.frames.append(indata.copy())
|
32 |
+
|
33 |
+
def save_audio(self):
|
34 |
+
if self.frames:
|
35 |
+
audio_data = np.concatenate(self.frames, axis=0)
|
36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav_file:
|
37 |
+
wavfile.write(temp_wav_file.name, self.sample_rate, audio_data)
|
38 |
+
return temp_wav_file.name
|
39 |
+
else:
|
40 |
+
return None
|
41 |
+
|
42 |
+
recorder = Recorder()
|
43 |
+
|
44 |
+
def record():
|
45 |
+
return recorder.toggle_recording()
|
46 |
+
|
47 |
+
def transcribe():
|
48 |
+
audio_file = recorder.save_audio()
|
49 |
+
if audio_file:
|
50 |
+
client = Groq(api_key="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")
|
51 |
+
with open(audio_file, "rb") as file:
|
52 |
+
transcription = client.audio.transcriptions.create(
|
53 |
+
file=(audio_file, file.read()),
|
54 |
+
model="whisper-large-v3",
|
55 |
+
prompt="Specify context or spelling", # Optional
|
56 |
+
response_format="json", # Optional
|
57 |
+
language="en", # Optional
|
58 |
+
temperature=0.0 # Optional
|
59 |
+
)
|
60 |
+
os.remove(audio_file) # Clean up the temporary file
|
61 |
+
|
62 |
+
# Inspect the transcription object to find the text
|
63 |
+
print(transcription)
|
64 |
+
|
65 |
+
# Access the text attribute directly if available
|
66 |
+
if hasattr(transcription, 'text'):
|
67 |
+
return transcription.text
|
68 |
+
else:
|
69 |
+
return "Transcription text not found."
|
70 |
+
|
71 |
+
else:
|
72 |
+
return "No audio recorded."
|
73 |
+
|
74 |
+
with gr.Blocks() as gradio_interface:
|
75 |
+
with gr.Column():
|
76 |
+
record_button = gr.Button("Press to Record")
|
77 |
+
record_button.click(fn=record, outputs=record_button)
|
78 |
+
transcription_output = gr.Textbox(label="Transcription")
|
79 |
+
record_button.click(fn=transcribe, outputs=transcription_output)
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
gradio_interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|