zinoubm commited on
Commit
361479c
·
1 Parent(s): 3dacc3e

final touches

Browse files
__pycache__/app.cpython-310.pyc ADDED
Binary file (4.4 kB). View file
 
__pycache__/commands.cpython-310.pyc CHANGED
Binary files a/__pycache__/commands.cpython-310.pyc and b/__pycache__/commands.cpython-310.pyc differ
 
__pycache__/constants.cpython-310.pyc CHANGED
Binary files a/__pycache__/constants.cpython-310.pyc and b/__pycache__/constants.cpython-310.pyc differ
 
__pycache__/demo.cpython-310.pyc ADDED
Binary file (4.13 kB). View file
 
__pycache__/main.cpython-310.pyc ADDED
Binary file (275 Bytes). View file
 
app.py CHANGED
@@ -1,9 +1,12 @@
 
1
  import gradio as gr
2
  import openai
3
  from constants import *
 
4
 
5
 
6
  openai.api_key = OPENAI_API_KEY
 
7
 
8
  title = "Car Seats Voice Commands"
9
 
@@ -30,25 +33,27 @@ Command Classification phase. For Transcription I used The OpenAi whisper model,
30
  I Fine-Tuned the OpenAi **ada** model on Car Seats Command.
31
  """
32
 
 
 
 
33
 
34
- # def get_command(command, model, id2label):
35
- # """
36
- # This function get the classification outputs from openai API
37
- # """
38
- # completion = openai.Completion.create(
39
- # model=model, prompt=f"{command}->", max_tokens=1, temperature=0
40
- # )
41
- # id = int(completion["choices"][0]["text"].strip())
42
- # result = id2label[id] if id in id2label else "unknown"
43
- # return result
44
 
 
 
 
 
 
 
 
 
45
 
46
- def get_command(command, model, id2label):
47
  """
48
  This function get the classification outputs from openai API
49
  """
50
  prompt = f"""
51
- We want to control the seats of a car which has features to cool, heat, or massage a seat. The user said "{command}", Which feature we should use to ensure user comfort? Give just the number of the feature.
52
  Mapping:
53
  1: "massage_seats_on"
54
  2: "massage_seats_off"
@@ -61,51 +66,72 @@ def get_command(command, model, id2label):
61
  """
62
 
63
  completion = openai.Completion.create(
64
- model="text-davinci-003", prompt=prompt, max_tokens=2, temperature=0
65
  )
66
- id = int(completion["choices"][0]["text"].strip())
 
 
 
 
67
 
68
  result = id2label[id] if id in id2label else "unknown"
69
  return result
70
 
71
 
72
- def transcribe(audio, text):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  """
74
  if text provided the function will classify the input directly.
75
  if not the audio will be transcribed then the transcription will be classified.
76
- """
77
 
78
- if text:
79
- result = get_command(text, MODEL, id2label)
80
- return "Text provided by the user", text_respnses[result], None
81
 
82
  # getting text transcription
83
  audio_file = open(audio, "rb")
84
  transcription = openai.Audio.transcribe("whisper-1", audio_file, language="en")
85
  transcription = transcription["text"]
86
 
87
- result = get_command(transcription, MODEL, id2label)
88
- audio_res = resoponses.get(result)()
 
 
 
89
 
90
- return transcription, text_respnses[result], audio_res
91
 
92
 
93
- if __name__ == "__main__":
94
  gr.Interface(
95
- fn=transcribe,
96
- inputs=[
97
- gr.Audio(label="", source="microphone", type="filepath"),
98
- gr.Textbox(label="If you prefer type your command (more accurate)"),
99
- ],
100
- outputs=[
101
- gr.Textbox(
102
- label="Input Transcription (Please check that this matches what you've said)"
103
- ),
104
- gr.Textbox(label="Machine Response (Text Version)"),
105
- gr.Audio(label="Machine Response (Audio Version)"),
106
- ],
107
- allow_flagging="auto",
108
- title=title,
109
- description=description,
110
- article=article,
111
  ).launch()
 
1
+ import logging
2
  import gradio as gr
3
  import openai
4
  from constants import *
5
+ import string
6
 
7
 
8
  openai.api_key = OPENAI_API_KEY
9
+ openai.organization = OPENAI_ORGANIZATION
10
 
11
  title = "Car Seats Voice Commands"
12
 
 
33
  I Fine-Tuned the OpenAi **ada** model on Car Seats Command.
34
  """
35
 
36
+ def remove_punctuation(input_string):
37
+ translator = str.maketrans('', '', string.punctuation)
38
+ clean_string = input_string.translate(translator)
39
 
40
+ return clean_string
 
 
 
 
 
 
 
 
 
41
 
42
+ id2label = {
43
+ 1:"massage_seats_on",
44
+ 2:"massage_seats_off",
45
+ 3:"heated_seats_on",
46
+ 4:"heated_seats_off",
47
+ 5:"cooled_seats_on",
48
+ 6:"cooled_seats_off"
49
+ }
50
 
51
+ def get_command(command, id2label, model = "text-davinci-003"):
52
  """
53
  This function get the classification outputs from openai API
54
  """
55
  prompt = f"""
56
+ We want to control the seats of a car which has features to cool, heat, or massage a seat. The user said "{command}", Which feature we should use to ensure user comfort? Give just the number of the feature without any punctuation.
57
  Mapping:
58
  1: "massage_seats_on"
59
  2: "massage_seats_off"
 
66
  """
67
 
68
  completion = openai.Completion.create(
69
+ model=model, prompt=prompt, max_tokens=2, temperature=0
70
  )
71
+
72
+ print("result")
73
+ print(completion["choices"][0]["text"].strip())
74
+
75
+ id = int(remove_punctuation(completion["choices"][0]["text"]).strip())
76
 
77
  result = id2label[id] if id in id2label else "unknown"
78
  return result
79
 
80
 
81
+ def command_tokens(command, model = "text-davinci-003"):
82
+ """
83
+ This function get the classification outputs from openai API
84
+ """
85
+ prompt = f"""
86
+ Give an array of the same length of the input, for every element of the returned array use one of the labels in the label-list
87
+
88
+ label-list :
89
+ - unit if belongs to the International System of Units
90
+ - value
91
+ - none if none of the above
92
+
93
+ input : [{",".join(command.split(" "))}]
94
+ output :
95
+ """
96
+
97
+ completion = openai.Completion.create(
98
+ model=model, prompt=prompt, max_tokens=128, temperature=0
99
+ )
100
+
101
+ result = completion["choices"][0]["text"].strip()
102
+
103
+ result_list = result.replace("[", "").replace("]", "").replace("'", "").split(',')
104
+
105
+ return list(zip(command.split(" "), result_list))
106
+
107
+
108
+ def transcribe(audio):
109
  """
110
  if text provided the function will classify the input directly.
111
  if not the audio will be transcribed then the transcription will be classified.
 
112
 
113
+ return a tuple of (transcription, command, audio to be played)
114
+ """
 
115
 
116
  # getting text transcription
117
  audio_file = open(audio, "rb")
118
  transcription = openai.Audio.transcribe("whisper-1", audio_file, language="en")
119
  transcription = transcription["text"]
120
 
121
+ result = get_command(transcription, id2label)
122
+ tokens = command_tokens(transcription)
123
+
124
+ print("result", result)
125
+ print("tokens", tokens)
126
 
127
+ return result, tokens
128
 
129
 
130
+ if __name__=="__main__":
131
  gr.Interface(
132
+ fn=transcribe,
133
+ inputs=gr.Audio(source="microphone", type="filepath"),
134
+ outputs=["text", "highlight"],
135
+ title=title,
136
+ description=description
 
 
 
 
 
 
 
 
 
 
 
137
  ).launch()
constants.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from commands import ResponseManager
2
  from dotenv import load_dotenv
3
  import os
@@ -5,6 +6,7 @@ import os
5
  load_dotenv()
6
 
7
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
 
8
  MODEL = os.getenv("MODEL")
9
 
10
  rs = ResponseManager()
 
1
+ from token import OP
2
  from commands import ResponseManager
3
  from dotenv import load_dotenv
4
  import os
 
6
  load_dotenv()
7
 
8
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
9
+ OPENAI_ORGANIZATION = os.getenv("OPENAI_ORGANIZATION")
10
  MODEL = os.getenv("MODEL")
11
 
12
  rs = ResponseManager()
flagged/audio/tmp7zujjp6r.wav DELETED
Binary file (16.7 kB)
 
flagged/audio/tmpxmty_reu.wav DELETED
Binary file (16.7 kB)
 
flagged/component 0/tmp4mx6cxo6.wav DELETED
Binary file (10.1 kB)
 
flagged/component 0/tmp7zfrxwn1.wav DELETED
Binary file (21.1 kB)
 
flagged/component 0/tmpj5vph7ui.wav DELETED
Binary file (9.56 kB)
 
flagged/component 0/tmpj9jcosbt.wav DELETED
Binary file (21.1 kB)
 
flagged/log.csv DELETED
@@ -1,38 +0,0 @@
1
- audio,output,flag,username,timestamp
2
- C:\Users\admin\Desktop\DS JRP\Programming\Python\Portfolio\Voice_Commands\flagged\audio\tmp7zujjp6r.wav,,,,2023-02-18 19:27:17.163146
3
- C:\Users\admin\Desktop\DS JRP\Programming\Python\Portfolio\Voice_Commands\flagged\audio\tmpxmty_reu.wav,,,,2023-02-18 19:27:19.327085
4
- C:\Users\admin\Desktop\DS JRP\Programming\Python\Portfolio\Voice_Commands\flagged\component 0\tmp4mx6cxo6.wav,,,,2023-02-19 19:21:00.276808
5
- C:\Users\admin\Desktop\DS JRP\Programming\Python\Portfolio\Voice_Commands\flagged\component 0\tmpj5vph7ui.wav,,,,2023-02-19 19:22:46.434333
6
- ,my neck is cold,,,2023-02-19 19:28:56.486805
7
- ,my neck need no more heating,,,2023-02-19 19:29:16.234959
8
- C:\Users\admin\Desktop\DS JRP\Programming\Python\Portfolio\Voice_Commands\flagged\component 0\tmp7zfrxwn1.wav,,,,2023-02-19 20:28:51.150010
9
- C:\Users\admin\Desktop\DS JRP\Programming\Python\Portfolio\Voice_Commands\flagged\component 0\tmpj9jcosbt.wav,,,,2023-02-19 20:29:16.601422
10
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpgp91h4bs.wav,,,,2023-03-03 10:48:05.204488
11
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpzxnv34e9.wav,,,,2023-03-03 11:00:04.470118
12
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp293krtcc.wav,,,,2023-03-03 11:05:18.409853
13
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp8w_dg0my.wav,,,,2023-03-03 11:06:13.151883
14
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp8uwv1dlh.wav,,,,2023-03-03 11:08:34.437979
15
- ,,,,2023-03-03 11:09:59.046011
16
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpwerjyrhm.wav,,,,2023-03-03 11:10:14.290251
17
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp1msp3gk7.wav,,,,2023-03-03 11:11:04.122287
18
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpic0klrj5.wav,,,,2023-03-03 11:11:17.671385
19
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpvb2y2kk5.wav,,,,2023-03-03 11:11:31.502951
20
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp_sv03ta9.wav,,,,2023-03-03 11:11:52.200257
21
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpl6bjw0sw.wav,,,,2023-03-03 11:17:37.575339
22
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp7272239u.wav,,,,2023-03-03 11:18:02.333990
23
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp4fwibr34.wav,,,,2023-03-03 11:18:18.551143
24
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp3qcgcacy.wav,,,,2023-03-03 11:18:35.923342
25
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmplih8n03h.wav,,,,2023-03-03 11:51:57.253346
26
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpx7nz_jih.wav,,,,2023-03-03 11:52:11.455135
27
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpflbx1ja4.wav,,,,2023-03-03 11:52:28.437100
28
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpcrult4_q.wav,,,,2023-03-03 11:52:39.450544
29
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmps1x6dtf2.wav,,,,2023-03-03 11:53:52.432486
30
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp_sseh3hk.wav,,,,2023-03-03 11:54:09.292486
31
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp68h39ev5.wav,,,,2023-03-03 11:54:26.488504
32
- ,I'm cold,,,2023-03-04 10:42:40.559129
33
- ,I'm cold,,,2023-03-04 10:44:49.418487
34
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpz0hq94rg.wav,,,,2023-03-04 11:32:53.276143
35
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmpr_cd3uqa.wav,,,,2023-03-04 11:33:22.605201
36
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp8pxcjozd.wav,,,,2023-03-04 11:34:02.384882
37
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp7vog58pt.wav,,,,2023-03-04 11:34:32.302966
38
- /home/boumaza/Desktop/Programming/Python/Voice_Commands/flagged/component 0/tmp5v5ipo64.wav,,,,2023-03-04 11:34:50.524003