Spaces:
Runtime error
Runtime error
Update LLMwithvoice.py
Browse files- LLMwithvoice.py +11 -12
LLMwithvoice.py
CHANGED
@@ -1,14 +1,9 @@
|
|
1 |
import requests
|
2 |
-
import torch
|
3 |
-
from transformers import AutoTokenizer
|
4 |
from pydub import AudioSegment
|
5 |
-
from
|
6 |
-
import io
|
7 |
|
8 |
-
# Hugging Face API
|
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
|
@@ -41,17 +36,21 @@ def generate_speech(api_token, answer):
|
|
41 |
try:
|
42 |
response.raise_for_status() # Raise an error for bad responses
|
43 |
audio = response.content
|
44 |
-
audio_segment = AudioSegment.
|
45 |
-
|
|
|
|
|
|
|
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,
|
53 |
answer = query_roberta(api_token, prompt, context)
|
54 |
if 'error' in answer:
|
55 |
return answer['error'], None
|
56 |
-
|
57 |
-
|
|
|
|
1 |
import requests
|
|
|
|
|
2 |
from pydub import AudioSegment
|
3 |
+
from io import BytesIO
|
|
|
4 |
|
5 |
+
# Hugging Face API URLs
|
6 |
API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
|
|
|
|
|
7 |
API_URL_TTS = "https://api-inference.huggingface.co/models/espnet/english_male_ryanspeech_tacotron"
|
8 |
|
9 |
# Function to query the RoBERTa model
|
|
|
36 |
try:
|
37 |
response.raise_for_status() # Raise an error for bad responses
|
38 |
audio = response.content
|
39 |
+
audio_segment = AudioSegment.from_file(BytesIO(audio), format="wav")
|
40 |
+
audio_file = BytesIO()
|
41 |
+
audio_segment.export(audio_file, format="wav")
|
42 |
+
audio_file.seek(0)
|
43 |
+
return audio_file
|
44 |
except requests.exceptions.HTTPError as e:
|
45 |
print(f"HTTP error occurred: {e}")
|
46 |
except Exception as e:
|
47 |
print(f"An unexpected error occurred: {e}")
|
48 |
|
49 |
# Function to interface with Gradio
|
50 |
+
def gradio_interface(api_token, context, prompt):
|
51 |
answer = query_roberta(api_token, prompt, context)
|
52 |
if 'error' in answer:
|
53 |
return answer['error'], None
|
54 |
+
answer_text = answer.get('answer', 'No answer found')
|
55 |
+
audio_file = generate_speech(api_token, answer_text)
|
56 |
+
return answer_text, audio_file
|