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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -32
app.py CHANGED
@@ -1,36 +1,81 @@
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
 
1
+ import os
 
2
  import wave
3
+ from fastapi import FastAPI, Response, HTTPException
4
+ from pydantic import BaseModel
5
+ import google.generativeai as genai
6
+ from google.generativeai import types
7
+
8
+ # --- Configuration and API Key ---
9
+ # It is recommended to set your Google API key as a secret in your Hugging Face Space settings.
10
+ # The key for the secret should be 'GOOGLE_API_KEY'.
11
+ try:
12
+ GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
13
+ if not GOOGLE_API_KEY:
14
+ raise ValueError("Google API key not found. Please set it in your Hugging Face Space secrets.")
15
+ genai.configure(api_key=GOOGLE_API_KEY)
16
+ except Exception as e:
17
+ # This will help in debugging if the key is not set.
18
+ print(f"Error during API key configuration: {e}")
19
+
20
+
21
+ # --- Pydantic Model for Request Body ---
22
+ class TextToSpeechRequest(BaseModel):
23
+ text: str = "Say cheerfully: Have a wonderful day!"
24
+ voice_name: str = "Kore"
25
+ output_filename: str = "output.wav"
26
+
27
+
28
+ # --- FastAPI App Initialization ---
29
+ app = FastAPI()
30
+
31
+
32
+ @app.get("/")
33
+ def read_root():
34
+ return {"message": "Welcome to the Text-to-Speech API using Gemini. Use the /generate-audio/ endpoint to create audio."}
35
+
36
+
37
+ @app.post("/generate-audio/")
38
+ async def generate_audio(request: TextToSpeechRequest):
39
+ """
40
+ This endpoint generates audio from the provided text using Google's Gemini model.
41
+ """
42
+ try:
43
+ # --- Text-to-Speech Generation ---
44
+ response = genai.generate_text(
45
+ model="gemini-2.5-flash-preview-tts",
46
+ prompt=request.text,
47
+ options=types.GenerationOptions(
48
+ response_modalities=["AUDIO"],
49
+ speech_config=types.SpeechConfig(
50
+ voice_config=types.VoiceConfig(
51
+ prebuilt_voice_config=types.PrebuiltVoiceConfig(
52
+ voice_name=request.voice_name,
53
+ )
54
+ )
55
+ ),
56
  )
57
+ )
58
+
59
+ # --- Extract Audio Data ---
60
+ if not response.candidates or not response.candidates[0].content.parts or not response.candidates[0].content.parts[0].inline_data.data:
61
+ raise HTTPException(status_code=500, detail="Audio data could not be generated.")
62
+
63
+ audio_data = response.candidates[0].content.parts[0].inline_data.data
64
+
65
+ # --- Save to a WAV file in memory ---
66
+ import io
67
+ buffer = io.BytesIO()
68
+ with wave.open(buffer, "wb") as wf:
69
+ wf.setnchannels(1)
70
+ wf.setsampwidth(2)
71
+ wf.setframerate(24000)
72
+ wf.writeframes(audio_data)
73
+ buffer.seek(0)
74
+
75
+ # --- Return Audio File as Response ---
76
+ return Response(content=buffer.getvalue(), media_type="audio/wav", headers={"Content-Disposition": f"attachment; filename={request.output_filename}"})
77
 
78
+ except Exception as e:
79
+ raise HTTPException(status_code=500, detail=str(e))
80
 
81
+ # To run this locally, use the command: uvicorn app:app --reload