Spaces:
Sleeping
Sleeping
Commit
·
fbe86b3
1
Parent(s):
678e06c
Code fixing
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import urllib.request
|
|
| 9 |
import torchaudio
|
| 10 |
from scipy.spatial.distance import cosine
|
| 11 |
from RealtimeSTT import AudioToTextRecorder
|
| 12 |
-
from fastapi import FastAPI
|
| 13 |
from fastrtc import Stream, AsyncStreamHandler, ReplyOnPause, get_cloudflare_turn_credentials_async, get_cloudflare_turn_credentials
|
| 14 |
import json
|
| 15 |
import io
|
|
@@ -618,19 +618,16 @@ async def get_cloudflare_credentials():
|
|
| 618 |
# Check if HF_TOKEN is set in environment
|
| 619 |
hf_token = os.environ.get("HF_TOKEN")
|
| 620 |
|
| 621 |
-
# If not set,
|
| 622 |
if not hf_token:
|
| 623 |
-
|
| 624 |
-
print("Warning: HF_TOKEN environment variable not set. Please set your own Hugging Face token.")
|
| 625 |
-
# Try to use the Hugging Face token from the environment
|
| 626 |
-
from huggingface_hub import HfApi
|
| 627 |
try:
|
|
|
|
| 628 |
api = HfApi()
|
| 629 |
hf_token = api.token
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
print("Error: Failed to get Hugging Face token. TURN relay may not work properly.")
|
| 634 |
|
| 635 |
# Get Cloudflare TURN credentials using the Hugging Face token
|
| 636 |
if hf_token:
|
|
@@ -639,7 +636,7 @@ async def get_cloudflare_credentials():
|
|
| 639 |
except Exception as e:
|
| 640 |
print(f"Error getting Cloudflare TURN credentials: {e}")
|
| 641 |
|
| 642 |
-
# Fallback
|
| 643 |
return {
|
| 644 |
"iceServers": [
|
| 645 |
{
|
|
@@ -655,16 +652,35 @@ def setup_fastrtc_handler():
|
|
| 655 |
handler = DiarizationHandler(diarization_system)
|
| 656 |
|
| 657 |
# Get server-side credentials (longer TTL)
|
| 658 |
-
server_credentials =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
|
|
|
|
| 660 |
stream = Stream(
|
| 661 |
handler=handler,
|
| 662 |
modality="audio",
|
| 663 |
mode="receive",
|
| 664 |
-
rtc_configuration=get_cloudflare_credentials
|
| 665 |
-
server_rtc_configuration=server_credentials # Server-side credentials with longer TTL
|
| 666 |
)
|
| 667 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 668 |
return stream
|
| 669 |
|
| 670 |
|
|
@@ -689,14 +705,23 @@ def create_interface():
|
|
| 689 |
document.getElementById('start-fastrtc').addEventListener('click', function() {
|
| 690 |
document.getElementById('fastrtc-status').textContent = 'Connecting...';
|
| 691 |
// FastRTC will initialize the connection
|
| 692 |
-
fetch('/start-rtc', {
|
| 693 |
-
|
| 694 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
document.getElementById('fastrtc-status').textContent = 'Connected! Speak now...';
|
| 696 |
-
}
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
|
|
|
|
|
|
|
|
|
| 700 |
});
|
| 701 |
</script>
|
| 702 |
</div>
|
|
@@ -872,9 +897,14 @@ rtc_stream.mount(app)
|
|
| 872 |
|
| 873 |
# 5) Expose an endpoint to trigger the client-side RTC handshake
|
| 874 |
@app.post("/start-rtc")
|
| 875 |
-
async def start_rtc():
|
| 876 |
-
|
| 877 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 878 |
|
| 879 |
# 6) Local dev via uvicorn; HF Spaces will auto-detect 'app' and ignore this
|
| 880 |
if __name__ == "__main__":
|
|
|
|
| 9 |
import torchaudio
|
| 10 |
from scipy.spatial.distance import cosine
|
| 11 |
from RealtimeSTT import AudioToTextRecorder
|
| 12 |
+
from fastapi import FastAPI, Request
|
| 13 |
from fastrtc import Stream, AsyncStreamHandler, ReplyOnPause, get_cloudflare_turn_credentials_async, get_cloudflare_turn_credentials
|
| 14 |
import json
|
| 15 |
import io
|
|
|
|
| 618 |
# Check if HF_TOKEN is set in environment
|
| 619 |
hf_token = os.environ.get("HF_TOKEN")
|
| 620 |
|
| 621 |
+
# If not set, try to get from huggingface_hub
|
| 622 |
if not hf_token:
|
| 623 |
+
print("Warning: HF_TOKEN environment variable not set. Trying to get token from huggingface_hub.")
|
|
|
|
|
|
|
|
|
|
| 624 |
try:
|
| 625 |
+
from huggingface_hub import HfApi
|
| 626 |
api = HfApi()
|
| 627 |
hf_token = api.token
|
| 628 |
+
except Exception as e:
|
| 629 |
+
print(f"Error getting Hugging Face token: {e}")
|
| 630 |
+
hf_token = None
|
|
|
|
| 631 |
|
| 632 |
# Get Cloudflare TURN credentials using the Hugging Face token
|
| 633 |
if hf_token:
|
|
|
|
| 636 |
except Exception as e:
|
| 637 |
print(f"Error getting Cloudflare TURN credentials: {e}")
|
| 638 |
|
| 639 |
+
# Fallback configuration if no token
|
| 640 |
return {
|
| 641 |
"iceServers": [
|
| 642 |
{
|
|
|
|
| 652 |
handler = DiarizationHandler(diarization_system)
|
| 653 |
|
| 654 |
# Get server-side credentials (longer TTL)
|
| 655 |
+
server_credentials = None
|
| 656 |
+
try:
|
| 657 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 658 |
+
if hf_token:
|
| 659 |
+
server_credentials = get_cloudflare_turn_credentials(hf_token=hf_token, ttl=360000)
|
| 660 |
+
else:
|
| 661 |
+
try:
|
| 662 |
+
from huggingface_hub import HfApi
|
| 663 |
+
api = HfApi()
|
| 664 |
+
hf_token = api.token
|
| 665 |
+
if hf_token:
|
| 666 |
+
server_credentials = get_cloudflare_turn_credentials(hf_token=hf_token, ttl=360000)
|
| 667 |
+
except:
|
| 668 |
+
print("Could not get server-side credentials. Using client-side only.")
|
| 669 |
+
except Exception as e:
|
| 670 |
+
print(f"Error getting server credentials: {e}")
|
| 671 |
|
| 672 |
+
# Create the Stream with appropriate configuration
|
| 673 |
stream = Stream(
|
| 674 |
handler=handler,
|
| 675 |
modality="audio",
|
| 676 |
mode="receive",
|
| 677 |
+
rtc_configuration=get_cloudflare_credentials # Async function for client-side credentials
|
|
|
|
| 678 |
)
|
| 679 |
|
| 680 |
+
# Set server-side credentials if available
|
| 681 |
+
if server_credentials:
|
| 682 |
+
stream.server_rtc_configuration = server_credentials
|
| 683 |
+
|
| 684 |
return stream
|
| 685 |
|
| 686 |
|
|
|
|
| 705 |
document.getElementById('start-fastrtc').addEventListener('click', function() {
|
| 706 |
document.getElementById('fastrtc-status').textContent = 'Connecting...';
|
| 707 |
// FastRTC will initialize the connection
|
| 708 |
+
fetch('/start-rtc', {
|
| 709 |
+
method: 'POST',
|
| 710 |
+
headers: {
|
| 711 |
+
'Content-Type': 'application/json'
|
| 712 |
+
}
|
| 713 |
+
})
|
| 714 |
+
.then(response => response.json())
|
| 715 |
+
.then(data => {
|
| 716 |
+
if (data.status === 'success') {
|
| 717 |
document.getElementById('fastrtc-status').textContent = 'Connected! Speak now...';
|
| 718 |
+
} else {
|
| 719 |
+
document.getElementById('fastrtc-status').textContent = 'Connection error: ' + data.error;
|
| 720 |
+
}
|
| 721 |
+
})
|
| 722 |
+
.catch(error => {
|
| 723 |
+
document.getElementById('fastrtc-status').textContent = 'Connection error: ' + error;
|
| 724 |
+
});
|
| 725 |
});
|
| 726 |
</script>
|
| 727 |
</div>
|
|
|
|
| 897 |
|
| 898 |
# 5) Expose an endpoint to trigger the client-side RTC handshake
|
| 899 |
@app.post("/start-rtc")
|
| 900 |
+
async def start_rtc(request: Request):
|
| 901 |
+
try:
|
| 902 |
+
# Initialize client connection
|
| 903 |
+
await rtc_stream.start_client()
|
| 904 |
+
return {"status": "success"}
|
| 905 |
+
except Exception as e:
|
| 906 |
+
print(f"Error starting RTC client: {e}")
|
| 907 |
+
return {"status": "error", "error": str(e)}
|
| 908 |
|
| 909 |
# 6) Local dev via uvicorn; HF Spaces will auto-detect 'app' and ignore this
|
| 910 |
if __name__ == "__main__":
|