DevsDoCode commited on
Commit
ab709a8
·
verified ·
1 Parent(s): 7629a8a

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +20 -0
  2. elevenlabs.py +28 -0
  3. requirements.txt +5 -0
  4. server_fastAPI.py +42 -0
Dockerfile ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as the base image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the requirements file into the container
8
+ COPY requirements.txt .
9
+
10
+ # Install the required packages
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Copy the rest of the application code into the container
14
+ COPY . .
15
+
16
+ # Expose the port that FastAPI will run on
17
+ EXPOSE 7860
18
+
19
+ # Command to run the FastAPI application
20
+ CMD ["uvicorn", "SERVER_fastAPI:app", "--host", "0.0.0.0", "--port", "7860"]
elevenlabs.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ def generate_speech(model, voice, input_text):
4
+ url = f"https://api.elevenlabs.io/v1/text-to-speech/{voice}?allow_unauthenticated=1"
5
+ headers = {
6
+ "Content-Type": "application/json"
7
+ }
8
+ data = {
9
+ "text": input_text,
10
+ "model_id": model,
11
+ }
12
+
13
+ response = requests.post(url, json=data, headers=headers)
14
+
15
+ if response.status_code == 200:
16
+ return response.content
17
+ else:
18
+ print(f"Failed to generate speech: {response.status_code}, {response.text}")
19
+ return [response.status_code, response.text]
20
+
21
+ if __name__ == "__main__":
22
+ text = """हमारा भारत एक विविधताओं से भरा देश है।
23
+ यहाँ कई भाषाएँ, संस्कृतियाँ और परंपराएँ एक साथ फलती-फूलती हैं।
24
+ प्रकृति ने भी इस देश को अपार सुंदरता से सजाया है।
25
+ पहाड़, नदियाँ, समुद्र और हरे-भरे जंगल, सभी इसकी शोभा बढ़ाते हैं।"""
26
+ model = "eleven_multilingual_v2"
27
+ voice = "XB0fDUnXU5powFXDhCwa"
28
+ print(generate_speech(model, voice, text))
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ fastapi==0.110.2
2
+ Flask==3.0.3
3
+ Requests==2.31.0
4
+ uvicorn==0.29.0
5
+ openai==1.51.0
server_fastAPI.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Response
2
+ from pydantic import BaseModel
3
+ from elevenlabs import generate_speech
4
+
5
+ app = FastAPI()
6
+
7
+ class SpeechRequest(BaseModel):
8
+ model: str
9
+ input: str
10
+ voice: str
11
+
12
+ @app.post("/v1/audio/speech")
13
+ async def create_speech(request: SpeechRequest):
14
+ try:
15
+ # Map the OpenAI-style request to your ElevenLabs function
16
+ result = generate_speech(
17
+ model=request.model,
18
+ voice=request.voice,
19
+ input_text=request.input
20
+ )
21
+
22
+ if isinstance(result, list):
23
+ # If result is a list, it means there was an error
24
+ raise HTTPException(status_code=result[0], detail=result[1])
25
+
26
+ # If successful, return the audio content
27
+ return Response(content=result, media_type="audio/mpeg")
28
+
29
+ except Exception as e:
30
+ raise HTTPException(status_code=500, detail=str(e))
31
+
32
+ @app.get("/")
33
+ async def root():
34
+ return {"message": "Welcome to the Text-to-Speech API"}
35
+
36
+ if __name__ == "__main__":
37
+ import uvicorn
38
+ uvicorn.run(app, host="0.0.0.0", port=7860)
39
+
40
+ # print("Sample URL: http://localhost:7860/v1/audio/speech?model=eleven_multilingual_v2&input=Hello+world&voice=XB0fDUnXU5powFXDhCwa")
41
+
42
+