Spaces:
Runtime error
Runtime error
import requests | |
from IPython.display import Audio | |
# Hugging Face API URL for Roberta model | |
API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2" | |
# Hugging Face API URL for text-to-speech model | |
API_URL_TTS = "https://api-inference.huggingface.co/models/suno/bark" | |
def query_roberta(api_token, payload): | |
headers = {"Authorization": f"Bearer {api_token}"} | |
response = requests.post(API_URL_ROBERTA, headers=headers, json=payload) | |
try: | |
return response.json() | |
except ValueError: | |
return {"error": "Invalid JSON response"} | |
def query_tts(api_token, payload): | |
headers = {"Authorization": f"Bearer {api_token}"} | |
response = requests.post(API_URL_TTS, headers=headers, json=payload) | |
return response.content | |
def chat_with_roberta(api_token, question, context): | |
payload = { | |
"inputs": { | |
"question": question, | |
"context": context | |
} | |
} | |
response = query_roberta(api_token, payload) | |
if "error" in response: | |
return f"Error: {response['error']}" | |
else: | |
try: | |
return response['answer'] | |
except (IndexError, KeyError): | |
return f"Unexpected response structure: {response}" | |
def generate_speech(api_token, text): | |
payload = {"inputs": text} | |
audio_bytes = query_tts(api_token, payload) | |
return audio_bytes | |
def gradio_interface(api_token, context, question): | |
answer = chat_with_roberta(api_token, question, context) | |
audio_bytes = generate_speech(api_token, answer) | |
return answer, audio_bytes |