testdeep123 commited on
Commit
ad33580
·
verified ·
1 Parent(s): 3d3cf6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -19,16 +19,26 @@ def kokoro_tts(text):
19
  # TODO: Replace with actual Kokoro TTS implementation
20
  # Should return path to generated audio file
21
  return "dummy_audio.wav"
22
-
23
  def generate_script(topic):
24
- prompt = f"Generate a script about {topic} divided into parts, and output it as a JSON array of strings."
25
- response = requests.post(
26
- "https://api.openrouter.com/v1/completions",
27
- headers={"Authorization": f"Bearer {OPENROUTER_API_KEY}"},
28
- json={"model": OPENROUTER_MODEL, "prompt": prompt}
29
- )
30
- script_json = response.json()["choices"][0]["text"]
31
- return json.loads(script_json)
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def generate_audio(script_parts, temp_folder):
34
  full_audio = AudioSegment.empty()
 
19
  # TODO: Replace with actual Kokoro TTS implementation
20
  # Should return path to generated audio file
21
  return "dummy_audio.wav"
 
22
  def generate_script(topic):
23
+ try:
24
+ prompt = f"Generate a script about {topic} divided into parts, and output it as a JSON array of strings."
25
+ response = requests.post(
26
+ "https://api.openrouter.com/v1/completions",
27
+ headers={"Authorization": f"Bearer {OPENROUTER_API_KEY}"},
28
+ json={"model": OPENROUTER_MODEL, "prompt": prompt},
29
+ timeout=10 # Add timeout to avoid hanging
30
+ )
31
+ response.raise_for_status() # Raise an exception for HTTP errors
32
+ script_json = response.json()["choices"][0]["text"]
33
+ return json.loads(script_json)
34
+ except requests.exceptions.ConnectionError as e:
35
+ raise Exception(f"Failed to connect to OpenRouter API: {str(e)}. Please check your internet connection or DNS settings.")
36
+ except requests.exceptions.HTTPError as e:
37
+ raise Exception(f"OpenRouter API returned an error: {str(e)}. Please verify your API key and model.")
38
+ except requests.exceptions.RequestException as e:
39
+ raise Exception(f"An error occurred while contacting OpenRouter API: {str(e)}")
40
+ except json.JSONDecodeError:
41
+ raise Exception("Failed to parse API response as JSON. Please check the API response format.")
42
 
43
  def generate_audio(script_parts, temp_folder):
44
  full_audio = AudioSegment.empty()