Suryaboss commited on
Commit
91f7734
·
verified ·
1 Parent(s): 77136d7

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. index.html +94 -0
  3. requirements.txt +3 -0
  4. server.py +55 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10-slim
2
+
3
+ WORKDIR /app
4
+
5
+ # Install dependencies
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+
9
+ # Copy the server files
10
+ COPY . .
11
+
12
+ # Hugging Face Spaces natively routes traffic on port 7860
13
+ EXPOSE 7860
14
+
15
+ # Start the FastAPI server using uvicorn mapped to the HF port
16
+ CMD ["uvicorn", "server:app", "--host", "0.0.0.0", "--port", "7860"]
index.html ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Edge TTS Python Tester</title>
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <style>
7
+ body { font-family: system-ui, sans-serif; max-width: 600px; margin: 40px auto; padding: 20px; background: #fafafa; color: #333; }
8
+ .card { background: white; padding: 24px; border-radius: 12px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); border: 1px solid #eaeaea; }
9
+ select, textarea, input { width: 100%; box-sizing: border-box; padding: 12px; margin: 8px 0 20px 0; border: 1px solid #ddd; border-radius: 6px; font-size: 15px; }
10
+ button { background: #0070f3; color: white; border: none; padding: 14px 24px; border-radius: 6px; cursor: pointer; width: 100%; font-weight: bold; font-size: 16px; transition: background 0.2s; }
11
+ button:hover { background: #0051b3; }
12
+ button:disabled { background: #ccc; cursor: not-allowed; }
13
+ audio { width: 100%; margin-top: 20px; border-radius: 8px; }
14
+ label { font-weight: 600; font-size: 14px; }
15
+ </style>
16
+ </head>
17
+ <body>
18
+ <div class="card">
19
+ <h2 style="margin-top: 0; text-align: center;">🎙️ Edge TTS Native Tester</h2>
20
+
21
+ <label>API Server URL (leave blank for auto-detect):</label>
22
+ <input type="text" id="serverUrl" placeholder="http://localhost:8000" value="">
23
+
24
+ <label>Select Neural Voice:</label>
25
+ <select id="voiceId">
26
+ <option value="en-US-JennyNeural">Jenny (US English - Female)</option>
27
+ <option value="en-US-GuyNeural">Guy (US English - Male)</option>
28
+ <option value="hi-IN-SwaraNeural" selected>Swara (Hindi - Female)</option>
29
+ <option value="hi-IN-MadhurNeural">Madhur (Hindi - Male)</option>
30
+ <option value="en-IN-NeerjaNeural">Neerja (Indian English - Female)</option>
31
+ <option value="en-IN-PrabhatNeural">Prabhat (Indian English - Male)</option>
32
+ </select>
33
+
34
+ <label>Text to Synthesize:</label>
35
+ <textarea id="text" rows="4">नमस्ते! मैं माइक्रोसॉफ्ट एज टीटीएस का परीक्षण कर रही हूं।</textarea>
36
+
37
+ <button onclick="generateAudio()" id="genBtn">Generate Speech</button>
38
+
39
+ <div id="status" style="margin-top: 15px; color: #666; font-size: 14px; text-align: center;">Ready. Click Generate to test the Proxy.</div>
40
+ <audio id="audioPlayer" controls style="display: none;"></audio>
41
+ </div>
42
+
43
+ <script>
44
+ async function generateAudio() {
45
+ const btn = document.getElementById('genBtn');
46
+ const statusBox = document.getElementById('status');
47
+ const audio = document.getElementById('audioPlayer');
48
+
49
+ let baseUrl = document.getElementById('serverUrl').value.trim() || window.location.origin;
50
+ // If running as local file directly without a server, fallback to default port 8000
51
+ if (baseUrl === "null" || baseUrl.startsWith("file://")) baseUrl = "http://localhost:8000";
52
+
53
+ const text = encodeURIComponent(document.getElementById('text').value);
54
+ const voice = encodeURIComponent(document.getElementById('voiceId').value);
55
+
56
+ btn.disabled = true;
57
+ btn.innerText = "Synthesizing...";
58
+ statusBox.style.color = "#666";
59
+ statusBox.innerText = `Routing request to ${baseUrl}...`;
60
+ audio.style.display = "none";
61
+
62
+ try {
63
+ // The API endpoint we built is GET /tts?text=...&voice=...
64
+ const url = `${baseUrl}/tts?text=${text}&voice=${voice}&rate=%2B0%25`;
65
+
66
+ statusBox.innerText = `Establishing fast socket to Microsoft Edge...`;
67
+ const response = await fetch(url);
68
+
69
+ if (!response.ok) {
70
+ const errorText = await response.text();
71
+ throw new Error(`Server Error ${response.status}: ${errorText}`);
72
+ }
73
+
74
+ // Construct a playable Object URL directly from the raw mp3 blob
75
+ const blob = await response.blob();
76
+ const blobUrl = URL.createObjectURL(blob);
77
+
78
+ audio.src = blobUrl;
79
+ audio.style.display = "block";
80
+ audio.play();
81
+ statusBox.innerText = "✅ Success! Edge TTS generated without 403 Ban.";
82
+ statusBox.style.color = "green";
83
+ } catch (error) {
84
+ console.error(error);
85
+ statusBox.innerText = "❌ Error: " + error.message;
86
+ statusBox.style.color = "red";
87
+ } finally {
88
+ btn.disabled = false;
89
+ btn.innerText = "Generate Speech";
90
+ }
91
+ }
92
+ </script>
93
+ </body>
94
+ </html>
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi==0.115.6
2
+ uvicorn==0.34.0
3
+ edge-tts==6.6.1
server.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Response, HTTPException
2
+ from fastapi.responses import HTMLResponse
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ import edge_tts
5
+ import tempfile
6
+ import os
7
+
8
+ app = FastAPI()
9
+
10
+ # Allow CORS for Next.js to call directly or Server-to-Server
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ @app.get("/")
19
+ def read_root():
20
+ # Automatically serve the HTML Tester UI if it exists alongside the server script
21
+ html_path = os.path.join(os.path.dirname(__file__), "index.html")
22
+ if os.path.exists(html_path):
23
+ with open(html_path, "r", encoding="utf-8") as f:
24
+ return HTMLResponse(content=f.read())
25
+ return {"status": "Edge TTS Proxy is running securely"}
26
+
27
+ @app.get("/tts")
28
+ async def generate_tts(text: str, voice: str = "en-US-JennyNeural", rate: str = "+0%"):
29
+ if not text:
30
+ raise HTTPException(status_code=400, detail="Missing text parameter")
31
+
32
+ try:
33
+ # edge-tts python library natively bypasses 403 API IP Bans automatically!
34
+ communicate = edge_tts.Communicate(text, voice, rate=rate)
35
+
36
+ with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as fp:
37
+ temp_path = fp.name
38
+
39
+ await communicate.save(temp_path)
40
+
41
+ with open(temp_path, "rb") as f:
42
+ audio_data = f.read()
43
+
44
+ os.remove(temp_path)
45
+
46
+ return Response(content=audio_data, media_type="audio/mpeg")
47
+ except Exception as e:
48
+ print(f"Server Error during TTS synthesis: {e}")
49
+ raise HTTPException(status_code=500, detail=str(e))
50
+
51
+ if __name__ == "__main__":
52
+ import uvicorn
53
+ # Important: Cloud hosts like Render inject the dynamic port into PORT env var
54
+ port = int(os.environ.get("PORT", 8000))
55
+ uvicorn.run(app, host="0.0.0.0", port=port)