Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
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 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|
|
|
|
| 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)
|