Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,19 @@ import http.server
|
|
| 2 |
import socketserver
|
| 3 |
import json
|
| 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):
|
|
@@ -44,6 +50,25 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
|
| 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 |
|
|
|
|
| 2 |
import socketserver
|
| 3 |
import json
|
| 4 |
from curl_cffi import requests
|
| 5 |
+
from openai import OpenAI
|
| 6 |
|
| 7 |
PORT = 7860
|
| 8 |
STT_URL = "https://multi-modal.ai.cloudflare.com/api/inference?model=@cf/deepgram/nova-3&field=audio"
|
| 9 |
TTS_URL = "https://multi-modal.ai.cloudflare.com/api/inference"
|
| 10 |
|
| 11 |
+
client = OpenAI(
|
| 12 |
+
base_url="https://integrate.api.nvidia.com/v1",
|
| 13 |
+
api_key="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"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
HTML = """
|
| 17 |
+
<!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><button onclick="showTab('chat')">Chat (Llama)</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><div id="chatTabContent" style="display:none;"><div id="messages" style="height:300px;overflow-y:scroll;border:1px solid #ccc;padding:10px;margin-bottom:10px;background:#eee;"></div><input type="text" id="chatInput" placeholder="Type message..." style="width:70%;"><button onclick="sendMessage()">Send</button><p>Status: <span id="statusChat">Idle</span></p></div><script>let messages=[];function showTab(tab){document.getElementById('sttTabContent').style.display=tab==='stt'?'block':'none';document.getElementById('ttsTabContent').style.display=tab==='tts'?'block':'none';document.getElementById('chatTabContent').style.display=tab==='chat'?'block':'none';if(tab==='chat'){document.getElementById('chatInput').focus();}}function addMessage(role,content){const div=document.getElementById('messages');const msg=document.createElement('div');msg.innerText=`${role}: ${content}`;div.appendChild(msg);div.scrollTop=div.scrollHeight;}async function sendMessage(){const input=document.getElementById('chatInput');const text=input.value.trim();if(!text)return;addMessage('user',text);input.value='';document.getElementById('statusChat').innerText='Thinking...';messages.push({role:'user',content:text});try{const res=await fetch('/api/chat',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({messages:messages})});const data=await res.json();const response=data.response;addMessage('assistant',response);messages.push({role:'assistant',content:response});document.getElementById('statusChat').innerText='Done';}catch(e){document.getElementById('statusChat').innerText='Error';}}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>
|
| 18 |
"""
|
| 19 |
|
| 20 |
class Handler(http.server.BaseHTTPRequestHandler):
|
|
|
|
| 50 |
self.send_header("Content-type", "application/json")
|
| 51 |
self.end_headers()
|
| 52 |
self.wfile.write(json.dumps(response).encode())
|
| 53 |
+
elif self.path == '/api/chat':
|
| 54 |
+
content_length = int(self.headers['Content-Length'])
|
| 55 |
+
body_str = self.rfile.read(content_length).decode('utf-8')
|
| 56 |
+
req_data = json.loads(body_str)
|
| 57 |
+
messages = req_data['messages']
|
| 58 |
+
completion = client.chat.completions.create(
|
| 59 |
+
model="meta/llama-3.2-1b-instruct",
|
| 60 |
+
messages=messages,
|
| 61 |
+
temperature=0.2,
|
| 62 |
+
top_p=0.7,
|
| 63 |
+
max_tokens=1024,
|
| 64 |
+
stream=False
|
| 65 |
+
)
|
| 66 |
+
response_text = completion.choices[0].message.content
|
| 67 |
+
response = {"response": response_text}
|
| 68 |
+
self.send_response(200)
|
| 69 |
+
self.send_header("Content-type", "application/json")
|
| 70 |
+
self.end_headers()
|
| 71 |
+
self.wfile.write(json.dumps(response).encode())
|
| 72 |
else:
|
| 73 |
self.send_error(404)
|
| 74 |
|