Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,38 @@
|
|
1 |
-
from transformers import TTSForConditionalGeneration, AutoModelForSeq2SeqLM
|
2 |
from datasets import load_dataset
|
3 |
-
from gradio import Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import TTSForConditionalGeneration, AutoModelForSeq2SeqLM, AutoModelForCausalLM
|
2 |
from datasets import load_dataset
|
3 |
+
from gradio import Interface
|
4 |
+
|
5 |
+
|
6 |
+
tts_model = TTSForConditionalGeneration.from_pretrained("facebook/s2s-wav2vec2-ft")
|
7 |
+
whisper_model = AutoModelForSeq2SeqLM.from_pretrained("openai/whisper-medium")
|
8 |
+
gpt2_xl_model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2-xl")
|
9 |
+
|
10 |
+
|
11 |
+
def voice_chat(user_voice):
|
12 |
+
messages = [{"role": "system", "content": "You are a kind helpful assistant."}]
|
13 |
+
user_message = whisper_model.generate(user_voice)["generated_text"]
|
14 |
+
messages.append({"role": "user", "content": user_message})
|
15 |
+
print(messages)
|
16 |
+
|
17 |
+
# Utilize gpt2-xl locally for chat completion
|
18 |
+
with gpt2_xl_model.no_grad():
|
19 |
+
input_ids = gpt2_xl_model.tokenizer(messages, return_tensors="pt").input_ids
|
20 |
+
outputs = gpt2_xl_model.generate(input_ids, max_length=100)
|
21 |
+
chat_response = gpt2_xl_model.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
|
23 |
+
reply = chat_response
|
24 |
+
messages.append({"role": "assistant", "content": reply})
|
25 |
+
audio = tts_model.generate(text=reply, return_tensors="pt").tolist()[0][0]
|
26 |
+
return reply, audio
|
27 |
+
|
28 |
+
|
29 |
+
text_reply = Interface.Textbox(label="ChatGPT Text")
|
30 |
+
voice_reply = Interface.Audio(type="audio/wav")
|
31 |
+
|
32 |
+
Interface(
|
33 |
+
title="AI Voice Assistant with ChatGPT AI",
|
34 |
+
fn=voice_chat,
|
35 |
+
inputs=[Interface.Audio(source="microphone", type="filepath")],
|
36 |
+
outputs=[text_reply, voice_reply],
|
37 |
+
live=True,
|
38 |
+
).launch(debug=True)
|