Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,52 +4,48 @@ import json
|
|
| 4 |
from curl_cffi import requests
|
| 5 |
|
| 6 |
PORT = 7860
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
HTML = """
|
| 10 |
-
<!DOCTYPE html>
|
| 11 |
-
<html>
|
| 12 |
-
<body>
|
| 13 |
-
<h1>Nova-3 Playground</h1>
|
| 14 |
-
<input type="file" id="f" accept="audio/mpeg">
|
| 15 |
-
<button onclick="u()">Transcribe</button>
|
| 16 |
-
<p>Status: <span id="s">Idle</span></p>
|
| 17 |
-
<pre id="r" style="background:#eee;padding:10px"></pre>
|
| 18 |
-
<script>
|
| 19 |
-
async function u() {
|
| 20 |
-
const f = document.getElementById('f').files[0];
|
| 21 |
-
if(!f) return;
|
| 22 |
-
document.getElementById('s').innerText = "Processing...";
|
| 23 |
-
try {
|
| 24 |
-
const res = await fetch('/api', { method: 'POST', body: f });
|
| 25 |
-
const data = await res.json();
|
| 26 |
-
document.getElementById('r').innerText = JSON.stringify(data, null, 2);
|
| 27 |
-
document.getElementById('s').innerText = "Done";
|
| 28 |
-
} catch(e) {
|
| 29 |
-
document.getElementById('s').innerText = "Error";
|
| 30 |
-
}
|
| 31 |
-
}
|
| 32 |
-
</script>
|
| 33 |
-
</body>
|
| 34 |
-
</html>
|
| 35 |
"""
|
| 36 |
|
| 37 |
class Handler(http.server.BaseHTTPRequestHandler):
|
| 38 |
def do_GET(self):
|
| 39 |
-
self.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def do_POST(self):
|
| 45 |
-
if self.path == '/api':
|
| 46 |
-
|
| 47 |
-
body = self.rfile.read(
|
| 48 |
-
r = requests.post(
|
| 49 |
self.send_response(200)
|
| 50 |
self.send_header("Content-type", "application/json")
|
| 51 |
self.end_headers()
|
| 52 |
self.wfile.write(r.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
with socketserver.TCPServer(("", PORT), Handler) as d:
|
| 55 |
print(f"Server: {PORT}")
|
|
|
|
| 4 |
from curl_cffi import requests
|
| 5 |
|
| 6 |
PORT = 7860
|
| 7 |
+
STT_URL = "https://multi-modal.ai.cloudflare.com/api/inference?model=@cf/deepgram/nova-3&field=audio"
|
| 8 |
+
TTS_URL = "https://multi-modal.ai.cloudflare.com/api/inference"
|
| 9 |
|
| 10 |
HTML = """
|
| 11 |
+
<!DOCTYPE html><html><body><h1>Multi-Modal Playground</h1><div id="tabs"><button onclick="showTab('stt')">STT (Nova-3)</button><button onclick="showTab('tts')">TTS (AURA-1)</button></div><div id="sttTabContent" style="display:block;"><p>Upload audio:</p><input type="file" id="audioFile" accept="audio/*"><button onclick="transcribe()">Transcribe</button><p>Status: <span id="status">Idle</span></p><pre id="result" style="background:#eee;padding:10px"></pre></div><div id="ttsTabContent" style="display:none;"><p>Enter text:</p><input type="text" id="textInput" placeholder="Enter text to speak" style="width:100%;"><button onclick="generateAudio()">Generate Audio</button><p>Status: <span id="statusTTS">Idle</span></p><audio id="audioPlayer" controls style="width:100%;"></audio></div><script>function showTab(tab){document.getElementById('sttTabContent').style.display=tab==='stt'?'block':'none';document.getElementById('ttsTabContent').style.display=tab==='tts'?'block':'none';}async function transcribe(){const file=document.getElementById('audioFile').files[0];if(!file)return;document.getElementById('status').innerText='Processing...';try{const res=await fetch('/api/stt',{method:'POST',body:file});const data=await res.json();document.getElementById('result').innerText=JSON.stringify(data,null,2);document.getElementById('status').innerText='Done';}catch(e){document.getElementById('status').innerText='Error';}}async function generateAudio(){const text=document.getElementById('textInput').value;if(!text)return;document.getElementById('statusTTS').innerText='Generating...';try{const res=await fetch('/api/tts',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({text:text})});const data=await res.json();document.getElementById('audioPlayer').src='data:audio/webm;base64,'+data.audio;document.getElementById('statusTTS').innerText='Done';}catch(e){document.getElementById('statusTTS').innerText='Error';}}</script></body></html>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
|
| 14 |
class Handler(http.server.BaseHTTPRequestHandler):
|
| 15 |
def do_GET(self):
|
| 16 |
+
if self.path == '/':
|
| 17 |
+
self.send_response(200)
|
| 18 |
+
self.send_header("Content-type", "text/html")
|
| 19 |
+
self.end_headers()
|
| 20 |
+
self.wfile.write(HTML.encode())
|
| 21 |
+
else:
|
| 22 |
+
self.send_error(404)
|
| 23 |
|
| 24 |
def do_POST(self):
|
| 25 |
+
if self.path == '/api/stt':
|
| 26 |
+
content_length = int(self.headers['Content-Length'])
|
| 27 |
+
body = self.rfile.read(content_length)
|
| 28 |
+
r = requests.post(STT_URL, data=body, impersonate="chrome")
|
| 29 |
self.send_response(200)
|
| 30 |
self.send_header("Content-type", "application/json")
|
| 31 |
self.end_headers()
|
| 32 |
self.wfile.write(r.content)
|
| 33 |
+
elif self.path == '/api/tts':
|
| 34 |
+
content_length = int(self.headers['Content-Length'])
|
| 35 |
+
body_str = self.rfile.read(content_length).decode('utf-8')
|
| 36 |
+
req_data = json.loads(body_str)
|
| 37 |
+
text = req_data['text']
|
| 38 |
+
tts_payload = {"model": "@cf/myshell-ai/melotts", "params": {"prompt": text}}
|
| 39 |
+
r = requests.post(TTS_URL, json=tts_payload, impersonate="chrome")
|
| 40 |
+
resp_data = r.json()
|
| 41 |
+
audio_b64 = resp_data["response"]["audio"]
|
| 42 |
+
response = {"audio": audio_b64}
|
| 43 |
+
self.send_response(200)
|
| 44 |
+
self.send_header("Content-type", "application/json")
|
| 45 |
+
self.end_headers()
|
| 46 |
+
self.wfile.write(json.dumps(response).encode())
|
| 47 |
+
else:
|
| 48 |
+
self.send_error(404)
|
| 49 |
|
| 50 |
with socketserver.TCPServer(("", PORT), Handler) as d:
|
| 51 |
print(f"Server: {PORT}")
|