RobertaSpeak / LLMwithvoice.py
ariankhalfani's picture
Update LLMwithvoice.py
39422e4 verified
raw
history blame
2.1 kB
import requests
import torch
from transformers import AutoTokenizer
from pydub import AudioSegment
from IPython.display import Audio, display
import io
# Hugging Face API URL for RoBERTa model
API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
# ESPnet TTS API URL
API_URL_TTS = "https://api-inference.huggingface.co/models/espnet/english_male_ryanspeech_tacotron"
# Function to query the RoBERTa model
def query_roberta(api_token, prompt, context):
payload = {
"inputs": {
"question": prompt,
"context": context
}
}
headers = {"Authorization": f"Bearer {api_token}"}
response = requests.post(API_URL_ROBERTA, headers=headers, json=payload)
try:
response.raise_for_status() # Raise an error for bad responses
return response.json()
except requests.exceptions.HTTPError as e:
return {"error": f"HTTP error occurred: {e}"}
except ValueError as e:
return {"error": f"Value error occurred: {e}"}
except Exception as e:
return {"error": f"An unexpected error occurred: {e}"}
# Function to generate speech from text using ESPnet TTS
def generate_speech(api_token, answer):
payload = {
"inputs": answer,
}
headers = {"Authorization": f"Bearer {api_token}"}
response = requests.post(API_URL_TTS, headers=headers, json=payload)
try:
response.raise_for_status() # Raise an error for bad responses
audio = response.content
audio_segment = AudioSegment.from_wav(io.BytesIO(audio))
display(Audio(audio))
except requests.exceptions.HTTPError as e:
print(f"HTTP error occurred: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Function to interface with Gradio
def gradio_interface(api_token, prompt, context):
answer = query_roberta(api_token, prompt, context)
if 'error' in answer:
return answer['error'], None
generate_speech(api_token, answer.get('answer', ''))
return answer.get('answer', 'No answer found'), None