Spaces:
Runtime error
Runtime error
Create LLMwithvoice.py
Browse files- LLMwithvoice.py +46 -0
LLMwithvoice.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from IPython.display import Audio
|
3 |
+
|
4 |
+
# Hugging Face API URL for Roberta model
|
5 |
+
API_URL_ROBERTA = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
|
6 |
+
# Hugging Face API URL for text-to-speech model
|
7 |
+
API_URL_TTS = "https://api-inference.huggingface.co/models/suno/bark"
|
8 |
+
|
9 |
+
def query_roberta(api_token, payload):
|
10 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
11 |
+
response = requests.post(API_URL_ROBERTA, headers=headers, json=payload)
|
12 |
+
try:
|
13 |
+
return response.json()
|
14 |
+
except ValueError:
|
15 |
+
return {"error": "Invalid JSON response"}
|
16 |
+
|
17 |
+
def query_tts(api_token, payload):
|
18 |
+
headers = {"Authorization": f"Bearer {api_token}"}
|
19 |
+
response = requests.post(API_URL_TTS, headers=headers, json=payload)
|
20 |
+
return response.content
|
21 |
+
|
22 |
+
def chat_with_roberta(api_token, question, context):
|
23 |
+
payload = {
|
24 |
+
"inputs": {
|
25 |
+
"question": question,
|
26 |
+
"context": context
|
27 |
+
}
|
28 |
+
}
|
29 |
+
response = query_roberta(api_token, payload)
|
30 |
+
if "error" in response:
|
31 |
+
return f"Error: {response['error']}"
|
32 |
+
else:
|
33 |
+
try:
|
34 |
+
return response['answer']
|
35 |
+
except (IndexError, KeyError):
|
36 |
+
return f"Unexpected response structure: {response}"
|
37 |
+
|
38 |
+
def generate_speech(api_token, text):
|
39 |
+
payload = {"inputs": text}
|
40 |
+
audio_bytes = query_tts(api_token, payload)
|
41 |
+
return audio_bytes
|
42 |
+
|
43 |
+
def gradio_interface(api_token, context, question):
|
44 |
+
answer = chat_with_roberta(api_token, question, context)
|
45 |
+
audio_bytes = generate_speech(api_token, answer)
|
46 |
+
return answer, Audio(audio_bytes, autoplay=True)
|