garima-mahato commited on
Commit
7aa4aab
·
verified ·
1 Parent(s): fe95d1e

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -39
app.py CHANGED
@@ -8,6 +8,10 @@ from peft import PeftModel
8
  import whisperx
9
  import gc
10
  import pandas as pd
 
 
 
 
11
 
12
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
13
  q_ctxt, query, ctxt_type = None, None, None
@@ -71,44 +75,48 @@ def add_file(history, file):
71
 
72
 
73
  def bot(history):
74
- if len(history) > 0 and isinstance(history[-1][0], tuple):
75
- ctxt_type = "audio"
76
- q_ctxt = get_audio_context(history[-1][0][0])
77
- response = "**Based on the given context: \n" + q_ctxt + "\n. **What do you want to know ?"
78
- # input_text = context + query
79
- # input_tokens = tokenizer.encode(input_text)
80
- # input_ids = torch.tensor(input_tokens, dtype=torch.int32).unsqueeze(0).to(device)
81
- # inputs_embeds = phi_model.get_input_embeddings()(input_ids)
82
- # out = phi_model.generate(inputs_embeds=inputs_embeds, min_new_tokens=10, max_new_tokens=50, bos_token_id=tokenizer.bos_token_id)
83
- # response = tokenizer.decode(out[0], skip_special_tokens=True)
84
- elif len(history) > 0 and isinstance(history[-1][0],str) and len(history[-1][0].strip()) > 0:
85
- print(history[-1][0])
86
- if ctxt_type == "audio" and "[INST]" not in history[-1][0]:
87
- query = history[-1][0]
88
- input_text = q_ctxt + query
89
- elif "[INST]" in history[-1][0]:
90
- q_ctxt = history[-1][0].split("[INST]")[0]
91
- print(q_ctxt)
92
- query = history[-1][0].split("[INST]")[-1]
93
- print(query)
94
- input_text = history[-1][0].replace("[INST]"," ")
95
- print(input_text)
96
- input_tokens = tokenizer.encode(input_text)
97
- input_ids = torch.tensor(input_tokens, dtype=torch.int32).unsqueeze(0).to(device)
98
- inputs_embeds = phi_model.get_input_embeddings()(input_ids)
99
- out = phi_model.generate(inputs_embeds=inputs_embeds, min_new_tokens=10, max_new_tokens=50, bos_token_id=tokenizer.bos_token_id)
100
- response = tokenizer.decode(out[0], skip_special_tokens=True)
101
- ctxt_type = "text"
102
- else:
103
- ctxt_type = None
104
- q_ctxt = None
105
- response = "Please ask your query or upload an audio/video to ask query"
106
-
107
- history[-1][1] = ""
108
- for character in response:
109
- history[-1][1] += character
110
- time.sleep(0.05)
111
- yield history
 
 
 
 
112
 
113
 
114
  with gr.Blocks() as demo:
@@ -144,4 +152,4 @@ with gr.Blocks() as demo:
144
 
145
 
146
  demo.queue()
147
- demo.launch(share=True)
 
8
  import whisperx
9
  import gc
10
  import pandas as pd
11
+ from transformers.utils import logging
12
+
13
+ logging.set_verbosity_info()
14
+ logger = logging.get_logger("transformers")
15
 
16
  device = 'cuda' if torch.cuda.is_available() else 'cpu'
17
  q_ctxt, query, ctxt_type = None, None, None
 
75
 
76
 
77
  def bot(history):
78
+ try:
79
+ if len(history) > 0 and isinstance(history[-1][0], tuple):
80
+ ctxt_type = "audio"
81
+ q_ctxt = get_audio_context(history[-1][0][0])
82
+ response = "**Based on the given context: \n" + q_ctxt + "\n. **What do you want to know ?"
83
+ # input_text = context + query
84
+ # input_tokens = tokenizer.encode(input_text)
85
+ # input_ids = torch.tensor(input_tokens, dtype=torch.int32).unsqueeze(0).to(device)
86
+ # inputs_embeds = phi_model.get_input_embeddings()(input_ids)
87
+ # out = phi_model.generate(inputs_embeds=inputs_embeds, min_new_tokens=10, max_new_tokens=50, bos_token_id=tokenizer.bos_token_id)
88
+ # response = tokenizer.decode(out[0], skip_special_tokens=True)
89
+ elif len(history) > 0 and isinstance(history[-1][0],str) and len(history[-1][0].strip()) > 0:
90
+ logger.info(history[-1][0])
91
+ if ctxt_type == "audio" and "[INST]" not in history[-1][0]:
92
+ query = history[-1][0]
93
+ input_text = q_ctxt + query
94
+ elif "[INST]" in history[-1][0]:
95
+ q_ctxt = history[-1][0].split("[INST]")[0]
96
+ logger.info(q_ctxt)
97
+ query = history[-1][0].split("[INST]")[-1]
98
+ logger.info(query)
99
+ input_text = history[-1][0].replace("[INST]"," ")
100
+ logger.info(input_text)
101
+ input_tokens = tokenizer.encode(input_text)
102
+ input_ids = torch.tensor(input_tokens, dtype=torch.int32).unsqueeze(0).to(device)
103
+ inputs_embeds = phi_model.get_input_embeddings()(input_ids)
104
+ out = phi_model.generate(inputs_embeds=inputs_embeds, min_new_tokens=10, max_new_tokens=50, bos_token_id=tokenizer.bos_token_id)
105
+ response = tokenizer.decode(out[0], skip_special_tokens=True)
106
+ ctxt_type = "text"
107
+ else:
108
+ ctxt_type = None
109
+ q_ctxt = None
110
+ response = "Please ask your query or upload an audio/video to ask query"
111
+
112
+ history[-1][1] = ""
113
+ for character in response:
114
+ history[-1][1] += character
115
+ time.sleep(0.05)
116
+ yield history
117
+ except Exception as e:
118
+ logger.error(e)
119
+ return e
120
 
121
 
122
  with gr.Blocks() as demo:
 
152
 
153
 
154
  demo.queue()
155
+ demo.launch(debug=True)