amedcj commited on
Commit
b72c5a4
·
verified ·
1 Parent(s): 2310741

Update app.py

Browse files

Updated the code

Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -6,13 +6,10 @@ import wave
6
  import io
7
 
8
  # --- Configuration ---
9
- # You can change the voice here. Find other names like 'Puck', 'Charon', etc. in the documentation.
10
  VOICE_NAME = 'Fenrir'
11
- # We remove the "cheerfully" style instruction to keep the accent attempt cleaner.
12
 
13
  # --- API Client Initialization ---
14
  try:
15
- # Client loads the key from the environment variable GEMINI_API_KEY set in Hugging Face Secrets.
16
  client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
17
  except Exception as e:
18
  print(f"Error initializing Gemini client: {e}. Ensure GEMINI_API_KEY secret is set.")
@@ -30,16 +27,14 @@ def wave_file(filename, pcm, channels=1, rate=24000, sample_width=2):
30
  # --- The Gradio Interface Function ---
31
  def gemini_tts_kurmanji(kurmanji_text: str) -> str:
32
  """
33
- Takes a Kurmanji text prompt and uses a natural language instruction to guide the TTS.
34
  """
35
  if not client:
36
- # Raise a Gradio error if the client failed to initialize (usually the API key issue)
37
  raise gr.Error("Gemini API Client failed to initialize. Check the GEMINI_API_KEY secret.")
38
 
39
  print(f"Attempting to generate Kurmanji speech: '{kurmanji_text}' with voice {VOICE_NAME}")
40
 
41
  try:
42
- # The key change: Instruct the model to speak the text in Kurdish Kurmanji.
43
  prompt = f"Speak the following text in Kurdish Kurmanji: {kurmanji_text}"
44
 
45
  response = client.models.generate_content(
@@ -57,6 +52,19 @@ def gemini_tts_kurmanji(kurmanji_text: str) -> str:
57
  )
58
  )
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  data = response.candidates[0].content.parts[0].inline_data.data
61
  file_name = 'kurmanji_output.wav'
62
  wave_file(file_name, data)
@@ -75,7 +83,7 @@ demo = gr.Interface(
75
  fn=gemini_tts_kurmanji,
76
  inputs=gr.Textbox(
77
  lines=3,
78
- placeholder="Mînak: Silav, roj baş. Ez dixwazim Kurdî biaxivim.", # Example Kurmanji Text
79
  label="Kurmanji Text to Convert"
80
  ),
81
  outputs=gr.Audio(
@@ -83,7 +91,7 @@ demo = gr.Interface(
83
  label="Generated Kurmanji Speech"
84
  ),
85
  title=f"🗣️ Gemini TTS for Kurdish Kurmanji (Voice: {VOICE_NAME})",
86
- description="Uses Gemini 2.5 Flash and natural language prompting to generate Kurdish Kurmanji speech."
87
  )
88
 
89
  if __name__ == "__main__":
 
6
  import io
7
 
8
  # --- Configuration ---
 
9
  VOICE_NAME = 'Fenrir'
 
10
 
11
  # --- API Client Initialization ---
12
  try:
 
13
  client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
14
  except Exception as e:
15
  print(f"Error initializing Gemini client: {e}. Ensure GEMINI_API_KEY secret is set.")
 
27
  # --- The Gradio Interface Function ---
28
  def gemini_tts_kurmanji(kurmanji_text: str) -> str:
29
  """
30
+ Takes a Kurmanji text prompt, handles API errors, and checks the response content.
31
  """
32
  if not client:
 
33
  raise gr.Error("Gemini API Client failed to initialize. Check the GEMINI_API_KEY secret.")
34
 
35
  print(f"Attempting to generate Kurmanji speech: '{kurmanji_text}' with voice {VOICE_NAME}")
36
 
37
  try:
 
38
  prompt = f"Speak the following text in Kurdish Kurmanji: {kurmanji_text}"
39
 
40
  response = client.models.generate_content(
 
52
  )
53
  )
54
 
55
+ # --- NEW: Robust Error Checking ---
56
+ if not response.candidates or not response.candidates[0].content:
57
+ # Check for block reasons (e.g., safety, policy)
58
+ block_reason = response.candidates[0].finish_reason.name if response.candidates else "NO_CANDIDATE"
59
+
60
+ # This is the most important part for debugging your 'NoneType' error:
61
+ raise gr.Error(
62
+ f"TTS Generation Failed. Reason: The model returned an empty response. "
63
+ f"The finish reason was: {block_reason}. "
64
+ f"This may indicate a quota limit, or the model could not generate the requested Kurmanji speech."
65
+ )
66
+
67
+ # Proceed if the content part is valid
68
  data = response.candidates[0].content.parts[0].inline_data.data
69
  file_name = 'kurmanji_output.wav'
70
  wave_file(file_name, data)
 
83
  fn=gemini_tts_kurmanji,
84
  inputs=gr.Textbox(
85
  lines=3,
86
+ placeholder="Mînak: Silav, roj baş. Ez dixwazim Kurdî biaxivim.",
87
  label="Kurmanji Text to Convert"
88
  ),
89
  outputs=gr.Audio(
 
91
  label="Generated Kurmanji Speech"
92
  ),
93
  title=f"🗣️ Gemini TTS for Kurdish Kurmanji (Voice: {VOICE_NAME})",
94
+ description="Uses Gemini 2.5 Flash and natural language prompting. Check the logs for specific failure reasons."
95
  )
96
 
97
  if __name__ == "__main__":