File size: 1,574 Bytes
9f5785e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
709897b
 
 
 
 
 
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
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