Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,32 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 6 |
|
| 7 |
-
|
| 8 |
def create_prompt_with_chat_format(messages, bos="<s>", eos="</s>", add_bos=True):
|
| 9 |
formatted_text = ""
|
| 10 |
for message in messages:
|
| 11 |
if message["role"] == "system":
|
| 12 |
-
formatted_text += "
|
| 13 |
elif message["role"] == "user":
|
| 14 |
-
formatted_text += "
|
| 15 |
elif message["role"] == "assistant":
|
| 16 |
-
formatted_text += "
|
| 17 |
else:
|
| 18 |
raise ValueError(
|
| 19 |
-
"Tulu chat template only supports 'system', 'user' and 'assistant' roles. Invalid role: {}.".format(
|
| 20 |
message["role"]
|
| 21 |
)
|
| 22 |
)
|
| 23 |
-
formatted_text += "
|
| 24 |
formatted_text = bos + formatted_text if add_bos else formatted_text
|
| 25 |
return formatted_text
|
| 26 |
|
| 27 |
-
|
| 28 |
def inference(input_prompts, model, tokenizer):
|
| 29 |
input_prompts = [
|
| 30 |
create_prompt_with_chat_format([{"role": "user", "content": input_prompt}], add_bos=False)
|
|
@@ -34,8 +36,8 @@ def inference(input_prompts, model, tokenizer):
|
|
| 34 |
encodings = tokenizer(input_prompts, padding=True, return_tensors="pt")
|
| 35 |
encodings = encodings.to(device)
|
| 36 |
|
| 37 |
-
with torch.
|
| 38 |
-
outputs = model.generate(encodings.input_ids, do_sample=False,
|
| 39 |
|
| 40 |
output_texts = tokenizer.batch_decode(outputs.detach(), skip_special_tokens=True)
|
| 41 |
|
|
@@ -45,26 +47,124 @@ def inference(input_prompts, model, tokenizer):
|
|
| 45 |
output_texts = [output_text[len(input_prompt) :] for input_prompt, output_text in zip(input_prompts, output_texts)]
|
| 46 |
return output_texts
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="left")
|
| 52 |
-
tokenizer.pad_token = tokenizer.eos_token
|
| 53 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16).to(device)
|
| 54 |
|
| 55 |
-
def respond_to_text(input_text):
|
| 56 |
-
outputs = inference([input_text], model, tokenizer)
|
| 57 |
-
return outputs[0]
|
| 58 |
|
| 59 |
|
| 60 |
-
input_prompts = [
|
| 61 |
-
"मैं अपने समय प्रबंधन कौशल को कैसे सुधार सकता हूँ? मुझे पांच बिंदु बताएं।",
|
| 62 |
-
"मैं अपने समय प्रबंधन कौशल को कैसे सुधार सकता हूँ? मुझे पांच बिंदु बताएं और उनका वर्णन करें।",
|
| 63 |
-
]
|
| 64 |
-
iface = gr.Interface(fn=respond_to_text, inputs="text", outputs="text")
|
| 65 |
-
iface.launch()
|
| 66 |
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# import gradio as gr
|
| 69 |
# from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 70 |
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
from pydub import AudioSegment
|
| 7 |
+
import io
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
|
|
|
| 11 |
def create_prompt_with_chat_format(messages, bos="<s>", eos="</s>", add_bos=True):
|
| 12 |
formatted_text = ""
|
| 13 |
for message in messages:
|
| 14 |
if message["role"] == "system":
|
| 15 |
+
formatted_text += "\n" + message["content"] + "\n"
|
| 16 |
elif message["role"] == "user":
|
| 17 |
+
formatted_text += "\n" + message["content"] + "\n"
|
| 18 |
elif message["role"] == "assistant":
|
| 19 |
+
formatted_text += "\n" + message["content"].strip() + eos + "\n"
|
| 20 |
else:
|
| 21 |
raise ValueError(
|
| 22 |
+
"Tulu chat template only supports 'system', 'user', and 'assistant' roles. Invalid role: {}.".format(
|
| 23 |
message["role"]
|
| 24 |
)
|
| 25 |
)
|
| 26 |
+
formatted_text += "\n"
|
| 27 |
formatted_text = bos + formatted_text if add_bos else formatted_text
|
| 28 |
return formatted_text
|
| 29 |
|
|
|
|
| 30 |
def inference(input_prompts, model, tokenizer):
|
| 31 |
input_prompts = [
|
| 32 |
create_prompt_with_chat_format([{"role": "user", "content": input_prompt}], add_bos=False)
|
|
|
|
| 36 |
encodings = tokenizer(input_prompts, padding=True, return_tensors="pt")
|
| 37 |
encodings = encodings.to(device)
|
| 38 |
|
| 39 |
+
with torch.no_grad():
|
| 40 |
+
outputs = model.generate(encodings.input_ids, do_sample=False, max_length=250)
|
| 41 |
|
| 42 |
output_texts = tokenizer.batch_decode(outputs.detach(), skip_special_tokens=True)
|
| 43 |
|
|
|
|
| 47 |
output_texts = [output_text[len(input_prompt) :] for input_prompt, output_text in zip(input_prompts, output_texts)]
|
| 48 |
return output_texts
|
| 49 |
|
| 50 |
+
def recognize_speech():
|
| 51 |
+
recognizer = sr.Recognizer()
|
| 52 |
+
microphone = sr.Microphone()
|
| 53 |
+
|
| 54 |
+
with microphone as source:
|
| 55 |
+
print("Listening...")
|
| 56 |
+
recognizer.adjust_for_ambient_noise(source)
|
| 57 |
+
audio_data = recognizer.listen(source, timeout=5)
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
print("Recognizing...")
|
| 61 |
+
text = recognizer.recognize_google(audio_data, language="hi-IN")
|
| 62 |
+
return text
|
| 63 |
+
except sr.UnknownValueError:
|
| 64 |
+
print("Speech Recognition could not understand audio.")
|
| 65 |
+
return ""
|
| 66 |
+
except sr.RequestError as e:
|
| 67 |
+
print(f"Could not request results from Google Speech Recognition service; {e}")
|
| 68 |
+
return ""
|
| 69 |
+
|
| 70 |
+
def text_to_speech(text):
|
| 71 |
+
tts = gTTS(text=text, lang="hi")
|
| 72 |
+
audio_stream = io.BytesIO()
|
| 73 |
+
tts.save(audio_stream)
|
| 74 |
+
audio = AudioSegment.from_file(io.BytesIO(audio_stream.read()), format="mp3")
|
| 75 |
+
return audio
|
| 76 |
+
|
| 77 |
+
def respond_to_input(input_text):
|
| 78 |
+
output_texts = inference([input_text], model, tokenizer)
|
| 79 |
+
output_text = output_texts[0]
|
| 80 |
+
output_audio = text_to_speech(output_text)
|
| 81 |
+
return output_text, output_audio.export(format="wav")
|
| 82 |
+
|
| 83 |
+
iface = gr.Interface(
|
| 84 |
+
fn=respond_to_input,
|
| 85 |
+
inputs=["text", "microphone"],
|
| 86 |
+
outputs=["text", "audio"],
|
| 87 |
+
live=True,
|
| 88 |
+
title="Airavata Speech Chatbot",
|
| 89 |
+
description="Type or speak to me, and I'll generate a response!",
|
| 90 |
+
theme="light",
|
| 91 |
+
)
|
| 92 |
|
| 93 |
+
iface.launch()
|
| 94 |
|
|
|
|
|
|
|
|
|
|
| 95 |
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
|
| 100 |
+
###############################################################################################################################
|
| 101 |
+
# import torch
|
| 102 |
+
# from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 103 |
+
# import gradio as gr
|
| 104 |
+
|
| 105 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# def create_prompt_with_chat_format(messages, bos="<s>", eos="</s>", add_bos=True):
|
| 109 |
+
# formatted_text = ""
|
| 110 |
+
# for message in messages:
|
| 111 |
+
# if message["role"] == "system":
|
| 112 |
+
# formatted_text += "<|system|>\n" + message["content"] + "\n"
|
| 113 |
+
# elif message["role"] == "user":
|
| 114 |
+
# formatted_text += "<|user|>\n" + message["content"] + "\n"
|
| 115 |
+
# elif message["role"] == "assistant":
|
| 116 |
+
# formatted_text += "<|assistant|>\n" + message["content"].strip() + eos + "\n"
|
| 117 |
+
# else:
|
| 118 |
+
# raise ValueError(
|
| 119 |
+
# "Tulu chat template only supports 'system', 'user' and 'assistant' roles. Invalid role: {}.".format(
|
| 120 |
+
# message["role"]
|
| 121 |
+
# )
|
| 122 |
+
# )
|
| 123 |
+
# formatted_text += "<|assistant|>\n"
|
| 124 |
+
# formatted_text = bos + formatted_text if add_bos else formatted_text
|
| 125 |
+
# return formatted_text
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# def inference(input_prompts, model, tokenizer):
|
| 129 |
+
# input_prompts = [
|
| 130 |
+
# create_prompt_with_chat_format([{"role": "user", "content": input_prompt}], add_bos=False)
|
| 131 |
+
# for input_prompt in input_prompts
|
| 132 |
+
# ]
|
| 133 |
+
|
| 134 |
+
# encodings = tokenizer(input_prompts, padding=True, return_tensors="pt")
|
| 135 |
+
# encodings = encodings.to(device)
|
| 136 |
+
|
| 137 |
+
# with torch.inference_mode():
|
| 138 |
+
# outputs = model.generate(encodings.input_ids, do_sample=False, max_new_tokens=250)
|
| 139 |
+
|
| 140 |
+
# output_texts = tokenizer.batch_decode(outputs.detach(), skip_special_tokens=True)
|
| 141 |
+
|
| 142 |
+
# input_prompts = [
|
| 143 |
+
# tokenizer.decode(tokenizer.encode(input_prompt), skip_special_tokens=True) for input_prompt in input_prompts
|
| 144 |
+
# ]
|
| 145 |
+
# output_texts = [output_text[len(input_prompt) :] for input_prompt, output_text in zip(input_prompts, output_texts)]
|
| 146 |
+
# return output_texts
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# model_name = "ai4bharat/Airavata"
|
| 150 |
+
|
| 151 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_name, padding_side="left")
|
| 152 |
+
# tokenizer.pad_token = tokenizer.eos_token
|
| 153 |
+
# model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16).to(device)
|
| 154 |
+
|
| 155 |
+
# def respond_to_text(input_text):
|
| 156 |
+
# outputs = inference([input_text], model, tokenizer)
|
| 157 |
+
# return outputs[0]
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
# input_prompts = [
|
| 161 |
+
# "मैं अपने समय प्रबंधन कौशल को कैसे सुधार सकता हूँ? मुझे पांच बिंदु बताएं।",
|
| 162 |
+
# "मैं अपने समय प्रबंधन कौशल को कैसे सुधार सकता हूँ? मुझे पांच बिंदु बताएं और उनका वर्णन करें।",
|
| 163 |
+
# ]
|
| 164 |
+
# iface = gr.Interface(fn=respond_to_text, inputs="text", outputs="text")
|
| 165 |
+
# iface.launch()
|
| 166 |
+
########################################################################################
|
| 167 |
+
|
| 168 |
# import gradio as gr
|
| 169 |
# from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 170 |
|