File size: 2,097 Bytes
9f5785e
5bcaefa
39422e4
4b144cc
3366c2e
39422e4
9f5785e
39422e4
9f5785e
5bcaefa
39422e4
 
9f5785e
9d4db1c
0b90662
 
 
 
 
 
 
9f5785e
 
 
39422e4
 
 
 
a5f7b84
39422e4
a5f7b84
39422e4
3366c2e
39422e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ddeb31
9d4db1c
0b90662
 
a5f7b84
 
39422e4
3366c2e
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
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