Spaces:
Runtime error
Runtime error
File size: 2,193 Bytes
9f5785e 4b144cc ab44c82 e8892bb 9f5785e ab44c82 9f5785e 39422e4 9f5785e 9d4db1c 0b90662 9f5785e 39422e4 a5f7b84 39422e4 a5f7b84 39422e4 3366c2e 39422e4 ab44c82 39422e4 e8892bb 39422e4 e8892bb 5ddeb31 9d4db1c ab44c82 0b90662 a5f7b84 ab44c82 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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 |