darthPanda commited on
Commit
0283b01
β€’
1 Parent(s): 5aa0262

added order taking NER

Browse files
Files changed (4) hide show
  1. README.md +1 -1
  2. app.py +63 -15
  3. order_parser.py +87 -0
  4. requirements.txt +2 -1
README.md CHANGED
@@ -1,6 +1,6 @@
1
  ---
2
  title: Falcon Barista
3
- emoji: πŸ‘€
4
  colorFrom: red
5
  colorTo: gray
6
  sdk: gradio
 
1
  ---
2
  title: Falcon Barista
3
+ emoji: πŸ¦…
4
  colorFrom: red
5
  colorTo: gray
6
  sdk: gradio
app.py CHANGED
@@ -1,7 +1,9 @@
1
  import gradio as gr
 
2
  from asr_openai import AutomaticSpeechRecognition
3
  from tts_elevenlabs import ElevenLabsTTS
4
  from falcon_7b_llm import Falcon_7b_llm
 
5
  import logging
6
  import os
7
 
@@ -21,7 +23,15 @@ def generate_response(input_audio):
21
  sentence = asr.run_transcription(input_audio)
22
  # sentence = 'how are you?'
23
  print(sentence)
 
 
 
 
 
 
 
24
  llm_response = llm.get_llm_response(sentence['text'])
 
25
  output_audio = tts.tts_generate_audio(llm_response)
26
  # output_audio = tts.tts_generate_audio(sentence)
27
  chatbot_history.append(((input_audio,), (output_audio,)))
@@ -29,33 +39,71 @@ def generate_response(input_audio):
29
 
30
  delete_files_in_folder('data//tts_responses')
31
 
32
- title = "<h1 style='text-align: center; color: #ffffff; font-size: 40px;'> πŸ¦… Falcon Barista"
33
 
34
  asr = AutomaticSpeechRecognition()
35
  tts = ElevenLabsTTS()
36
  llm = Falcon_7b_llm()
 
37
  chatbot_history = []
 
 
38
 
39
- def restart_chat():
40
- delete_files_in_folder('data//tts_responses')
41
- global chatbot_history
42
- chatbot_history = []
43
- tts.restart_state()
44
- llm.restart_state()
45
- return chatbot_history
46
 
47
  with gr.Blocks() as demo:
48
  gr.Markdown(title)
 
49
  with gr.Row():
50
  gr.Image('https://i.imgur.com/fHCFI2T.png', label="Look how cute is Falcon Barista")
51
- # gr.Image('data//falcon.png', label="Look how cute is Falcon Barista")
52
  with gr.Column():
53
- chatbot = gr.Chatbot(label='Chat with Falcon Barista', avatar_images=('data//user_avatar_logo.png','data//falcon_logo_transparent.png'))
54
- with gr.Row():
55
- mic = gr.Audio(source="microphone", type='filepath', scale=3)
56
- mic.stop_recording(generate_response, mic, chatbot)
57
- restart_btn = gr.Button(value="Restart Chat", scale=1)
58
- restart_btn.click(restart_chat, outputs=[chatbot])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  if __name__ == "__main__":
61
  demo.launch()
 
1
  import gradio as gr
2
+ import pandas as pd
3
  from asr_openai import AutomaticSpeechRecognition
4
  from tts_elevenlabs import ElevenLabsTTS
5
  from falcon_7b_llm import Falcon_7b_llm
6
+ from order_parser import Order_Parser
7
  import logging
8
  import os
9
 
 
23
  sentence = asr.run_transcription(input_audio)
24
  # sentence = 'how are you?'
25
  print(sentence)
26
+ global order_dict
27
+ try:
28
+ order_dict = order_taking.order_parser(sentence['text'])
29
+ print(order_dict)
30
+ except Exception as e:
31
+ print('order parsing failed')
32
+ print(e)
33
  llm_response = llm.get_llm_response(sentence['text'])
34
+ print(llm_response)
35
  output_audio = tts.tts_generate_audio(llm_response)
36
  # output_audio = tts.tts_generate_audio(sentence)
37
  chatbot_history.append(((input_audio,), (output_audio,)))
 
39
 
40
  delete_files_in_folder('data//tts_responses')
41
 
42
+ title = "<h1 style='text-align: center; color: #ffffff; font-size: 40px;'> Falcon Barista (Pre-Alpha Release)"
43
 
44
  asr = AutomaticSpeechRecognition()
45
  tts = ElevenLabsTTS()
46
  llm = Falcon_7b_llm()
47
+ order_taking = Order_Parser()
48
  chatbot_history = []
49
+ order_display=False
50
+ order_dict={}
51
 
52
+ df = pd.DataFrame({
53
+ "item" : [],
54
+ "quantity" : [],
55
+ })
56
+
57
+ s = df#.style.format("{:.2f}")
 
58
 
59
  with gr.Blocks() as demo:
60
  gr.Markdown(title)
61
+ order_title = gr.Markdown('### Your Order', visible=False)
62
  with gr.Row():
63
  gr.Image('https://i.imgur.com/fHCFI2T.png', label="Look how cute is Falcon Barista")
 
64
  with gr.Column():
65
+ chatbot = gr.Chatbot(label='Chat with Falcon Barista', avatar_images=('data//user_avatar_logo.png','data//falcon_logo_transparent.png'), scale=2)
66
+ mic = gr.Audio(source="microphone", type='filepath', scale=1)
67
+ mic.stop_recording(generate_response, mic, chatbot)
68
+ with gr.Row():
69
+ restart_btn = gr.Button(value="Restart Chat", scale=1, variant='stop')
70
+ # restart_btn.click(restart_chat, outputs=[chatbot])
71
+ end_btn = gr.Button(value="End Chat and Confirm Order", scale=2, variant='primary')
72
+
73
+ with gr.Column(visible=False) as output_col:
74
+ order_title = gr.Markdown('### Your Order')
75
+ order_summary = gr.DataFrame(s)
76
+
77
+ def restart_chat():
78
+ delete_files_in_folder('data//tts_responses')
79
+ global chatbot_history
80
+ chatbot_history = []
81
+ global order_dict
82
+ order_dict = {}
83
+ global df
84
+ df = pd.DataFrame({
85
+ "item" : [],
86
+ "quantity" : [],
87
+ })
88
+ order_taking.restart_state()
89
+ tts.restart_state()
90
+ llm.restart_state()
91
+ return {
92
+ chatbot: [],
93
+ output_col: gr.Column(visible=False)
94
+ }
95
+
96
+ def end_chat():
97
+ df = pd.DataFrame(list(order_dict.items()), columns=['item', 'quantity'])
98
+
99
+ s = df#.style.format("{:.2f}")
100
+ return {
101
+ output_col: gr.Column(visible=True),
102
+ order_summary: gr.DataFrame(s, visible=True)
103
+ }
104
+
105
+ restart_btn.click(restart_chat, outputs=[chatbot, output_col])
106
+ end_btn.click(end_chat, outputs=[output_col, order_summary])
107
 
108
  if __name__ == "__main__":
109
  demo.launch()
order_parser.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
2
+ from transformers import pipeline
3
+ from word2number import w2n
4
+ import pandas as pd
5
+
6
+ class Order_Parser():
7
+ def __init__(self):
8
+ tokenizer = AutoTokenizer.from_pretrained("davanstrien/deberta-v3-base_fine_tuned_food_ner")
9
+ model = AutoModelForTokenClassification.from_pretrained("davanstrien/deberta-v3-base_fine_tuned_food_ner")
10
+ self.pipe = pipeline("ner", model=model, tokenizer=tokenizer)
11
+ self.complete_order_dict={}
12
+
13
+ def restart_state(self):
14
+ self.complete_order_dict={}
15
+
16
+ def join_adjacent_items(self, data):
17
+ result = []
18
+ current_group = []
19
+ current_entity = None
20
+
21
+ for item in data:
22
+ # Check if the item's entity is related to FOOD or QUANTITY
23
+ if any(e in item['entity'] for e in ['FOOD', 'QUANTITY']):
24
+ # Start a new group if the entity type changes
25
+ if not current_entity:
26
+ current_entity = item['entity'].split('-')[-1]
27
+ elif current_entity != item['entity'].split('-')[-1]:
28
+ result.append({'entity': current_entity, 'word': ''.join(current_group)})
29
+ current_group = []
30
+ current_entity = item['entity'].split('-')[-1]
31
+
32
+ current_group.append(item['word'])
33
+ else:
34
+ if current_group:
35
+ result.append({'entity': current_entity, 'word': ''.join(current_group)})
36
+ current_group = []
37
+ current_entity = None
38
+ result.append(item)
39
+
40
+ # Handle the last group if it exists
41
+ if current_group:
42
+ result.append({'entity': current_entity, 'word': ''.join(current_group)})
43
+
44
+ return result
45
+
46
+ def order_parser(self, sentence):
47
+ sentence = sentence.replace(',', ' ')
48
+ sentence = sentence.replace('?', ' ')
49
+ sentence = sentence.replace('.', ' ')
50
+ # updated_sentence = updated_sentence.replace('and', 'one')
51
+ sentence = sentence.replace(' a ', ' one ')
52
+ sentence = sentence.replace(' an ', ' one ')
53
+ # sentence = sentence.replace(' and ', ' one ')
54
+ # print(updated_sentence)
55
+ # raw_order = self.pipe(updated_sentence)
56
+ print(sentence)
57
+ raw_order = self.pipe(sentence)
58
+ raw_order_piped = self.join_adjacent_items(raw_order)
59
+
60
+ order_dict={}
61
+ quantity_exist = False
62
+ for ent in raw_order_piped:
63
+ if 'QUANTITY' in ent['entity']:
64
+ quantity = ent['word'].replace('▁', ' ')
65
+ try:
66
+ quantity = w2n.word_to_num(quantity)
67
+ except:
68
+ quantity = 1
69
+
70
+ # print(quantity)
71
+ quantity_exist = True
72
+
73
+ elif 'FOOD' in ent['entity']:
74
+ food = ent['word'].replace('▁', '')
75
+ # print(food)
76
+
77
+ if quantity_exist:
78
+ order_dict[food] = quantity
79
+ else:
80
+ order_dict[food] = 1
81
+
82
+ quantity_exist=False
83
+
84
+ # print(order_dict)
85
+ self.complete_order_dict.update(order_dict)
86
+ return self.complete_order_dict
87
+
requirements.txt CHANGED
@@ -8,4 +8,5 @@ scipy
8
  elevenlabs
9
  openai
10
  torch
11
- wandb
 
 
8
  elevenlabs
9
  openai
10
  torch
11
+ wandb
12
+ word2number