VanguardAI commited on
Commit
d5685b0
·
verified ·
1 Parent(s): 7b05003

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -141
app.py CHANGED
@@ -1,144 +1,82 @@
1
- import torch
2
- import spaces
3
- import re
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
- # Get environment variable for Hugging Face access
21
- READ_HF = os.environ.get("read_hf")
22
- # Alpaca prompt template
23
- alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
24
-
25
- ### Instruction:
26
- {}
27
-
28
- ### Input:
29
- {}
30
-
31
- ### Response:
32
- {}"""
33
-
34
- # Inventory management instructions
35
- string = '''
36
- You are an AI assistant tasked with managing inventory based on user instructions. You must meticulously analyze each user request to determine the appropriate action and execute it with the correct parameters.
37
-
38
- **Here's your step-by-step thought process:**
39
-
40
- 1. **Identify the Function:** Carefully examine the user's input to determine the primary function they want to perform. The available functions are:
41
- - `transaction`: Record a new item transaction.
42
- - `last n days transactions`: Retrieve transaction records within a specific timeframe.
43
- - `view inventory`: View inventory details for a specific category and risk level.
44
- - `generate report`: Generate an inventory report.
45
-
46
- 2. **Extract Parameters:** Once you've identified the function, carefully extract the necessary parameters from the user's input. Each function requires specific parameters:
47
-
48
- **`transaction`:**
49
- - `ItemName`: (string) **Must be an exact match from the provided Item List.**
50
- - `ItemQt`: (integer) The quantity of the item.
51
- - `Type`: (string) "sale", "purchase", or "return".
52
-
53
- **`last n days transactions`:**
54
- - `ItemCategory`: (string) **Must be from the provided Item Category List.**
55
- - `Duration`: (integer) Number of days (convert weeks, months, years to days).
56
-
57
- **`view inventory`:**
58
- - `ItemCategory`: (string) **Must be from the provided Item Category List.**
59
- - `RiskType`: (string) "overstock", "understock", or "Null" (if risk inventory is not asked), or "All" for both overstock and understock.
60
-
61
- **`generate report`:**
62
- - `ItemCategory`: (string) **Must be from the provided Item Category List.**
63
- - `Duration`: (integer) Number of days (convert weeks, months, years to days).
64
- - `ReportType`: (string): "profit", "revenue", "inventory", or "Null" (for all reports).
65
-
66
- 3. **Validate Inputs:** Before proceeding, validate the extracted parameters:
67
-
68
- - **ItemName:** Ensure the `ItemName` is an exact match from the provided Item List.
69
- - **ItemCategory:** Ensure the `ItemCategory` is from the provided Category List.
70
- - **Data Types:** Verify that all parameters are of the correct data type (string or integer).
71
-
72
- 4. **Output in JSON:** Always format your response as a JSON object.
73
-
74
- **Additional Notes:**
75
- - Pay close attention to the case and spelling of function names and parameters.
76
-
77
- Category List : ["Dairy & Eggs", "Beverages & Snacks", "Cleaning & Hygiene", "Grains & Staples", "Personal Care", "Other"]
78
- '''
79
- from unsloth import FastLanguageModel
80
- @spaces.GPU()
81
- def chunk_it(inventory_list, user_input_text):
82
- # Check for CUDA and NVIDIA-related errors
83
- try:
84
- # Check for GPU devices
85
- device_count = torch.cuda.device_count()
86
- logger.info(f"Number of GPU devices: {device_count}")
87
- if device_count == 0:
88
- raise RuntimeError("No GPU devices found.")
89
-
90
- # Check CUDA version using subprocess
91
- process = subprocess.run(['nvcc', '--version'], capture_output=True, text=True)
92
- cuda_version = process.stdout.strip()
93
- logger.info(f"CUDA version: {cuda_version}")
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()