Spaces:
Runtime error
Runtime error
Update LLMwithvoice.py
Browse files- LLMwithvoice.py +28 -41
LLMwithvoice.py
CHANGED
@@ -1,21 +1,15 @@
|
|
1 |
import requests
|
2 |
import torch
|
3 |
-
|
4 |
-
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
5 |
-
from parler_tts import ParlerTTSForConditionalGeneration
|
6 |
from pydub import AudioSegment
|
7 |
from IPython.display import Audio, display
|
|
|
8 |
|
9 |
-
# Hugging Face API URL for
|
10 |
API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
torch_dtype = torch.float16 if device.type != "cpu" else torch.float32
|
15 |
-
|
16 |
-
# Load the ParlerTTS model and tokenizer
|
17 |
-
model = ParlerTTSForConditionalGeneration.from_pretrained("parler-tts/parler_tts_mini_v0.1").to(device, dtype=torch_dtype)
|
18 |
-
tokenizer = AutoTokenizer.from_pretrained("parler-tts/parler_tts_mini_v0.1")
|
19 |
|
20 |
# Function to query the RoBERTa model
|
21 |
def query_roberta(api_token, prompt, context):
|
@@ -28,43 +22,36 @@ def query_roberta(api_token, prompt, context):
|
|
28 |
headers = {"Authorization": f"Bearer {api_token}"}
|
29 |
response = requests.post(API_URL_ROBERTA, headers=headers, json=payload)
|
30 |
try:
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
return
|
35 |
except ValueError as e:
|
36 |
-
|
37 |
-
return {"error": str(e)}
|
38 |
except Exception as e:
|
39 |
-
|
40 |
-
return {"error": "An unexpected error occurred"}
|
41 |
-
|
42 |
-
# Function to generate speech from text
|
43 |
-
def generate_speech(answer):
|
44 |
-
input_ids = tokenizer(answer, return_tensors="pt").input_ids.to(device)
|
45 |
-
|
46 |
-
generation = model.generate(input_ids=input_ids)
|
47 |
-
audio_arr = generation.cpu().numpy().squeeze()
|
48 |
-
|
49 |
-
# Convert numpy array to audio segment
|
50 |
-
audio_segment = AudioSegment(
|
51 |
-
audio_arr.tobytes(),
|
52 |
-
frame_rate=model.config.sampling_rate,
|
53 |
-
sample_width=audio_arr.dtype.itemsize,
|
54 |
-
channels=1
|
55 |
-
)
|
56 |
-
|
57 |
-
# Save the audio to a file
|
58 |
-
audio_file = "output.wav"
|
59 |
-
audio_segment.export(audio_file, format="wav")
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
# Function to interface with Gradio
|
65 |
def gradio_interface(api_token, prompt, context):
|
66 |
answer = query_roberta(api_token, prompt, context)
|
67 |
if 'error' in answer:
|
68 |
return answer['error'], None
|
69 |
-
generate_speech(answer.get('answer', ''))
|
70 |
return answer.get('answer', 'No answer found'), None
|
|
|
1 |
import requests
|
2 |
import torch
|
3 |
+
from transformers import AutoTokenizer
|
|
|
|
|
4 |
from pydub import AudioSegment
|
5 |
from IPython.display import Audio, display
|
6 |
+
import io
|
7 |
|
8 |
+
# Hugging Face API URL for RoBERTa model
|
9 |
API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
|
10 |
|
11 |
+
# ESPnet TTS API URL
|
12 |
+
API_URL_TTS = "https://api-inference.huggingface.co/models/espnet/english_male_ryanspeech_tacotron"
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# Function to query the RoBERTa model
|
15 |
def query_roberta(api_token, prompt, context):
|
|
|
22 |
headers = {"Authorization": f"Bearer {api_token}"}
|
23 |
response = requests.post(API_URL_ROBERTA, headers=headers, json=payload)
|
24 |
try:
|
25 |
+
response.raise_for_status() # Raise an error for bad responses
|
26 |
+
return response.json()
|
27 |
+
except requests.exceptions.HTTPError as e:
|
28 |
+
return {"error": f"HTTP error occurred: {e}"}
|
29 |
except ValueError as e:
|
30 |
+
return {"error": f"Value error occurred: {e}"}
|
|
|
31 |
except Exception as e:
|
32 |
+
return {"error": f"An unexpected error occurred: {e}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
# Function to generate speech from text using ESPnet TTS
|
35 |
+
def generate_speech(api_token, answer):
|
36 |
+
payload = {
|
37 |
+
"inputs": answer,
|
38 |
+
}
|
39 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
40 |
+
response = requests.post(API_URL_TTS, headers=headers, json=payload)
|
41 |
+
try:
|
42 |
+
response.raise_for_status() # Raise an error for bad responses
|
43 |
+
audio = response.content
|
44 |
+
audio_segment = AudioSegment.from_wav(io.BytesIO(audio))
|
45 |
+
display(Audio(audio))
|
46 |
+
except requests.exceptions.HTTPError as e:
|
47 |
+
print(f"HTTP error occurred: {e}")
|
48 |
+
except Exception as e:
|
49 |
+
print(f"An unexpected error occurred: {e}")
|
50 |
|
51 |
# Function to interface with Gradio
|
52 |
def gradio_interface(api_token, prompt, context):
|
53 |
answer = query_roberta(api_token, prompt, context)
|
54 |
if 'error' in answer:
|
55 |
return answer['error'], None
|
56 |
+
generate_speech(api_token, answer.get('answer', ''))
|
57 |
return answer.get('answer', 'No answer found'), None
|