RobertaSpeak / LLMwithvoice.py
ariankhalfani's picture
Update LLMwithvoice.py
e8892bb verified
raw
history blame
2.19 kB
import requests
from pydub import AudioSegment
from io import BytesIO
import gradio as gr
# Hugging Face API URLs
API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
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_file(BytesIO(audio), format="wav")
audio_file = BytesIO()
audio_segment.export(audio_file, format="wav")
audio_file.seek(0)
return audio_file
except requests.exceptions.HTTPError as e:
print(f"HTTP error occurred: {e}")
return None
except Exception as e:
print(f"An unexpected error occurred: {e}")
return None
# Function to interface with Gradio
def gradio_interface(api_token, context, prompt):
answer = query_roberta(api_token, prompt, context)
if 'error' in answer:
return answer['error'], None
answer_text = answer.get('answer', 'No answer found')
audio_file = generate_speech(api_token, answer_text)
return answer_text, audio_file