Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,157 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
predictions = pipeline(input_img)
|
| 8 |
-
return input_img, {p["label"]: p["score"] for p in predictions}
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
if __name__ == "__main__":
|
| 18 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import librosa
|
| 4 |
|
|
|
|
| 5 |
|
| 6 |
+
########################ASR model###############################
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
| 9 |
+
|
| 10 |
+
# load model and processor
|
| 11 |
+
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
| 12 |
+
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
|
| 13 |
+
model.config.forced_decoder_ids = None
|
| 14 |
+
|
| 15 |
+
sample_rate = 16000
|
| 16 |
+
|
| 17 |
+
def ASR_model(audio, sr=16000):
|
| 18 |
+
DB_audio = audio
|
| 19 |
+
input_features = processor(audio, sampling_rate=sr, return_tensors="pt").input_features
|
| 20 |
+
# generate token ids
|
| 21 |
+
predicted_ids = model.generate(input_features)
|
| 22 |
+
# decode token ids to text
|
| 23 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=False)
|
| 24 |
+
|
| 25 |
+
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
return transcription
|
| 28 |
+
|
| 29 |
+
########################LLama model###############################
|
| 30 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 31 |
+
|
| 32 |
+
model_name_or_path = "TheBloke/llama2_7b_chat_uncensored-GPTQ"
|
| 33 |
+
# To use a different branch, change revision
|
| 34 |
+
# For example: revision="main"
|
| 35 |
+
model = AutoModelForCausalLM.from_pretrained(model_name_or_path,
|
| 36 |
+
device_map="auto",
|
| 37 |
+
trust_remote_code=True,
|
| 38 |
+
revision="main",
|
| 39 |
+
#quantization_config=QuantizationConfig(disable_exllama=True)
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=True)
|
| 43 |
+
Llama_pipe = pipeline(
|
| 44 |
+
"text-generation",
|
| 45 |
+
model=model,
|
| 46 |
+
tokenizer=tokenizer,
|
| 47 |
+
max_new_tokens=20,
|
| 48 |
+
do_sample=True,
|
| 49 |
+
temperature=0.7,
|
| 50 |
+
top_p=0.95,
|
| 51 |
+
top_k=40,
|
| 52 |
+
repetition_penalty=1.1
|
| 53 |
)
|
| 54 |
+
history="""User: Hello, Rally?
|
| 55 |
+
Rally: I'm happy to see you again. What you want to talk to day?
|
| 56 |
+
User: Let's talk about food
|
| 57 |
+
Rally: Sure.
|
| 58 |
+
User: I'm hungry right now. Do you know any Vietnamese food?"""
|
| 59 |
+
|
| 60 |
+
prompt_template = f"""<|im_start|>system
|
| 61 |
+
Talk one sentence to continue the conversation<|im_end|>
|
| 62 |
+
{history}
|
| 63 |
+
Rally:"""
|
| 64 |
+
print(Llama_pipe(prompt_template)[0]['generated_text'])
|
| 65 |
+
|
| 66 |
+
def RallyRespone(chat_history, message):
|
| 67 |
+
chat_history += "User: " + message + "\n"
|
| 68 |
+
t_chat = Llama_pipe(prompt_template)[0]['generated_text']
|
| 69 |
+
res = t_chat[t_chat.rfind("Rally: "):]
|
| 70 |
+
return res
|
| 71 |
+
|
| 72 |
+
########################Gradio UI###############################
|
| 73 |
+
|
| 74 |
+
# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.
|
| 75 |
+
def add_file(files):
|
| 76 |
+
return files.name
|
| 77 |
+
|
| 78 |
+
def print_like_dislike(x: gr.LikeData):
|
| 79 |
+
print(x.index, x.value, x.liked)
|
| 80 |
+
|
| 81 |
+
def upfile(files):
|
| 82 |
+
x = librosa.load(files, sr=16000)
|
| 83 |
+
print(x[0])
|
| 84 |
+
text = ASR_model(x[0])
|
| 85 |
+
return [text[0], text[0]]
|
| 86 |
+
|
| 87 |
+
def transcribe(audio):
|
| 88 |
+
sr, y = audio
|
| 89 |
+
y = y.astype(np.float32)
|
| 90 |
+
y /= np.max(np.abs(y))
|
| 91 |
+
|
| 92 |
+
return transcriber({"sampling_rate": sr, "raw": y})["text"], transcriber({"sampling_rate": sr, "raw": y})["text"]
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# def recommand(text):
|
| 96 |
+
# ret = "answer for"
|
| 97 |
+
|
| 98 |
+
# return ret + text
|
| 99 |
+
|
| 100 |
+
def add_text(history, text):
|
| 101 |
+
history = history + [(text, None)]
|
| 102 |
+
return history, gr.Textbox(value="", interactive=False)
|
| 103 |
+
|
| 104 |
+
# def bot(history):
|
| 105 |
+
# response = "**That's cool!**"
|
| 106 |
+
# history[-1][1] = ""
|
| 107 |
+
# for character in response:
|
| 108 |
+
# history[-1][1] += character
|
| 109 |
+
# time.sleep(0.05)
|
| 110 |
+
# yield history
|
| 111 |
+
|
| 112 |
+
with gr.Blocks() as demo:
|
| 113 |
+
chatbot = gr.Chatbot(
|
| 114 |
+
[],
|
| 115 |
+
elem_id="chatbot",
|
| 116 |
+
bubble_full_width=False,
|
| 117 |
+
)
|
| 118 |
+
file_output = gr.File()
|
| 119 |
+
|
| 120 |
+
def respond(message, chat_history):
|
| 121 |
+
bot_message = RallyRespone(chat_history, message)
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
chat_history.append((message, bot_message))
|
| 125 |
+
time.sleep(2)
|
| 126 |
+
print (chat_history[-1])
|
| 127 |
+
return chat_history[-1][-1], chat_history
|
| 128 |
+
|
| 129 |
+
with gr.Row():
|
| 130 |
+
with gr.Column():
|
| 131 |
+
audio_speech = gr.Audio(sources=["microphone"])
|
| 132 |
+
submit = gr.Button("Submit")
|
| 133 |
+
send = gr.Button("Send")
|
| 134 |
+
btn = gr.UploadButton("📁", file_types=["audio"])
|
| 135 |
+
|
| 136 |
+
with gr.Column():
|
| 137 |
+
opt1 = gr.Button("1: ")
|
| 138 |
+
opt2 = gr.Button("2: ")
|
| 139 |
+
|
| 140 |
+
#submit.click(translate, inputs=audio_speech, outputs=[opt1, opt2])
|
| 141 |
+
# output is opt1 value, opt2 value [ , ]
|
| 142 |
+
|
| 143 |
+
file_msg = btn.upload(add_file, btn, file_output)
|
| 144 |
+
submit.click(upfile, inputs=file_output, outputs=[opt1, opt2])
|
| 145 |
+
send.click(transcribe, inputs=audio_speech, outputs=[opt1, opt2])
|
| 146 |
+
opt1.click(respond, [opt1, chatbot], [opt1, chatbot])
|
| 147 |
+
|
| 148 |
+
opt2.click(respond, [opt2, chatbot], [opt2, chatbot])
|
| 149 |
+
|
| 150 |
+
#opt2.click(recommand, inputs=opt2)
|
| 151 |
+
#click event maybe BOT . generate history = optx.value,
|
| 152 |
+
|
| 153 |
+
chatbot.like(print_like_dislike, None, None)
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
+
demo.queue()
|
| 157 |
+
demo.launch(debug=True)
|