Athspi commited on
Commit
94de3c6
·
verified ·
1 Parent(s): 22004d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -152
app.py CHANGED
@@ -1,155 +1,36 @@
1
- # main.py
2
- from fastapi import FastAPI, Query, HTTPException
3
- from fastapi.responses import FileResponse
4
- from gtts import gTTS, gTTSError # Import gTTSError for specific error handling
5
- import uuid
6
- import os # Import os module for path operations
7
- import tempfile # Import tempfile for better temporary file handling
8
-
9
- # --- Configuration ---
10
- # Use tempfile to get a cross-platform temporary directory
11
- TEMP_DIR = tempfile.gettempdir()
12
- # Ensure the temporary directory exists
13
- os.makedirs(TEMP_DIR, exist_ok=True)
14
-
15
- # --- FastAPI App Initialization ---
16
- app = FastAPI(
17
- title="gTTS API",
18
- description="A simple API to convert text to speech using Google Text-to-Speech (gTTS). Supports multiple languages including Tamil, Sinhala, and many others.",
19
- version="1.2.0", # Increment version for documentation changes
 
 
 
 
 
 
 
 
 
 
 
20
  )
21
 
22
- # --- API Endpoints ---
23
-
24
- @app.get("/", tags=["General"])
25
- def read_root():
26
- """
27
- Root endpoint providing a welcome message.
28
- """
29
- return {"message": "Welcome to the gTTS API. Use the /tts endpoint to generate speech."}
30
-
31
- @app.get("/tts", tags=["Text-to-Speech"])
32
- def text_to_speech(
33
- text: str = Query(
34
- ..., # Ellipsis makes the parameter required
35
- min_length=1,
36
- max_length=500, # Adjust max length as needed
37
- title="Text to Convert",
38
- description="The text you want to convert into speech (1-500 characters)."
39
- ),
40
- lang: str = Query(
41
- "en", # Default language is English
42
- min_length=2,
43
- max_length=10, # Allow for language codes like 'en-us', 'zh-CN' etc.
44
- title="Language Code",
45
- description="The BCP 47 language code for the speech synthesis (e.g., 'en', 'es', 'ta', 'si', 'ja', 'zh-CN'). See gTTS documentation for supported languages."
46
- )
47
- ):
48
- """
49
- Converts the provided text into an MP3 audio file using the specified language.
50
-
51
- - **text**: The text to synthesize (required).
52
- - **lang**: The language code (e.g., 'en', 'es', 'fr', 'ta', 'si'). Defaults to 'en'. **Crucially, gTTS must support this language code.**
53
- """
54
- try:
55
- # Generate a unique filename in the configured temporary directory
56
- filename = os.path.join(TEMP_DIR, f"{uuid.uuid4().hex}.mp3")
57
-
58
- # Create gTTS object with text and language
59
- # Use slow=False for normal speed speech
60
- tts_object = gTTS(text=text, lang=lang, slow=False)
61
-
62
- # Save the audio file
63
- tts_object.save(filename)
64
-
65
- # Return the audio file as a response
66
- # The 'filename' parameter sets the download name for the browser
67
- return FileResponse(
68
- path=filename,
69
- media_type="audio/mpeg",
70
- filename=f"speech_{lang}.mp3" # Suggest a filename like speech_en.mp3 or speech_ta.mp3
71
- # Consider adding background task for cleanup as mentioned in previous examples
72
- )
73
-
74
- except gTTSError as e:
75
- # Handle specific gTTS errors (like invalid language code, network issues)
76
- detail_message = f"gTTS Error: {str(e)}. Ensure the language code '{lang}' is supported and text is appropriate for the language."
77
- # Check common error patterns
78
- if "400 (Bad Request)" in str(e) or "Language not supported" in str(e):
79
- raise HTTPException(status_code=400, detail=detail_message)
80
- elif "500 (Internal Server Error)" in str(e) or "Failed to connect" in str(e):
81
- # Treat these as potential temporary Google service issues
82
- raise HTTPException(status_code=503, detail=f"Service Error: {str(e)}. Could be a temporary issue with the TTS service.")
83
- else: # Other gTTS errors
84
- raise HTTPException(status_code=503, detail=detail_message) # 503 Service Unavailable likely
85
-
86
- except ValueError as e:
87
- # Potentially handle other value errors if gTTS raises them for certain inputs
88
- raise HTTPException(status_code=400, detail=f"Input Error: {str(e)}")
89
-
90
- except Exception as e:
91
- # Catch any other unexpected errors
92
- # Log the error for debugging
93
- # import logging
94
- # logging.exception(f"An unexpected error occurred during TTS generation for lang='{lang}'")
95
- raise HTTPException(status_code=500, detail=f"Internal Server Error: An unexpected error occurred.")
96
 
97
- # --- How to Run (Instructions) ---
98
- # 1. Save this code as `main.py`.
99
- # 2. Install necessary libraries:
100
- # pip install fastapi "uvicorn[standard]" gTTS
101
- # 3. Run the FastAPI server using Uvicorn:
102
- # uvicorn main:app --reload
103
- #
104
- # --- How to Use - Examples ---
105
- # Open your browser or use a tool like curl/Postman.
106
- # Access the TTS endpoint with the 'text' and 'lang' query parameters.
107
- # NOTE: Text containing non-ASCII characters needs to be URL-encoded. Most browsers do this automatically.
108
- #
109
- # - English (en - Default):
110
- # Text: "Hello, world!"
111
- # URL: http://127.0.0.1:8000/tts?text=Hello%2C%20world%21
112
- #
113
- # - Spanish (es):
114
- # Text: "Hola Mundo"
115
- # URL: http://127.0.0.1:8000/tts?text=Hola%20Mundo&lang=es
116
- #
117
- # - French (fr):
118
- # Text: "Bonjour le monde"
119
- # URL: http://127.0.0.1:8000/tts?text=Bonjour%20le%20monde&lang=fr
120
- #
121
- # - German (de):
122
- # Text: "Hallo Welt"
123
- # URL: http://127.0.0.1:8000/tts?text=Hallo%20Welt&lang=de
124
- #
125
- # - Tamil (ta):
126
- # Text: "வணக்கம் உலகம்"
127
- # URL: http://127.0.0.1:8000/tts?text=%E0%AE%B5%E0%AE%A3%E0%AE%95%E0%AF%8D%E0%AE%95%E0%AE%AE%E0%AF%8D%20%E0%AE%89%E0%AE%B2%E0%AE%95%E0%AE%AE%E0%AF%8D&lang=ta
128
- #
129
- # - Sinhala (si):
130
- # Text: "හෙලෝ ලෝකය"
131
- # URL: http://127.0.0.1:8000/tts?text=%E0%B7%84%E0%B7%99%E0%B6%BD%E0%B7%9D%20%E0%B6%BD%E0%B7%9D%E0%B6%9A%E0%B6%BA&lang=si
132
- #
133
- # - Japanese (ja):
134
- # Text: "こんにちは世界"
135
- # URL: http://127.0.0.1:8000/tts?text=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF%E4%B8%96%E7%95%8C&lang=ja
136
- #
137
- # - Chinese (Mandarin, Simplified) (zh-CN):
138
- # Text: "你好世界"
139
- # URL: http://127.0.0.1:8000/tts?text=%E4%BD%A0%E5%A5%BD%E4%B8%96%E7%95%8C&lang=zh-CN
140
- #
141
- # - Russian (ru):
142
- # Text: "Привет мир"
143
- # URL: http://127.0.0.1:8000/tts?text=%D0%9F%D1%80%D0%B8%D0%B2%D0%B5%D1%82%20%D0%BC%D0%B8%D1%80&lang=ru
144
- #
145
- # - Hindi (hi):
146
- # Text: "नमस्ते दुनिया"
147
- # URL: http://127.0.0.1:8000/tts?text=%E0%A4%A8%E0%A4%AE%E0%A4%B8%E0%A5%8D%E0%A4%A4%E0%A5%87%20%E0%A4%A6%E0%A5%81%E0%A4%A8%E0%A4%BF%E0%A4%AF%E0%A4%BE&lang=hi
148
- #
149
- # - Arabic (ar):
150
- # Text: "مرحبا بالعالم"
151
- # URL: http://127.0.0.1:8000/tts?text=%D9%85%D8%B1%D8%AD%D8%A8%D8%A7%20%D8%A8%D8%A7%D9%84%D8%B9%D8%A7%D9%84%D9%85&lang=ar
152
- #
153
- # Find more supported language codes in the gTTS documentation or common lists of BCP 47 codes.
154
- # The API will return an MP3 file download or playback depending on your browser/client.
155
- # If you provide an unsupported language code, you should get a 400 Bad Request error.
 
1
+ Give complete all code add fastapi how in huggin space give complete all code from google import genai
2
+ from google.genai import types
3
+ import wave
4
+ from google.colab import userdata
5
+
6
+ # Set up the wave file to save the output:
7
+ def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
8
+ with wave.open(filename, "wb") as wf:
9
+ wf.setnchannels(channels)
10
+ wf.setsampwidth(sample_width)
11
+ wf.setframerate(rate)
12
+ wf.writeframes(pcm)
13
+
14
+ # Retrieve the API key from Colab's Secrets Manage
15
+ GOOGLE_API_KEY = userdata.get('GOOGLE_API_KEY')
16
+ client = genai.Client(api_key=GOOGLE_API_KEY)
17
+
18
+ response = client.models.generate_content(
19
+ model="gemini-2.5-flash-preview-tts",
20
+ contents="Say cheerfully: Have a wonderful day!",
21
+ config=types.GenerateContentConfig(
22
+ response_modalities=["AUDIO"],
23
+ speech_config=types.SpeechConfig(
24
+ voice_config=types.VoiceConfig(
25
+ prebuilt_voice_config=types.PrebuiltVoiceConfig(
26
+ voice_name='Kore',
27
+ )
28
+ )
29
+ ),
30
+ )
31
  )
32
 
33
+ data = response.candidates[0].content.parts[0].inline_data.data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
+ file_name='out.wav'
36
+ wave_file(file_name, data) # Saves the file to current directory