Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,27 +9,43 @@ import requests
|
|
9 |
|
10 |
# Configuration
|
11 |
OPENROUTER_API_KEY = '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'
|
12 |
-
OPENROUTER_MODEL = "
|
13 |
TARGET_RESOLUTION = (1080, 1920)
|
14 |
OUTPUT_VIDEO_FILENAME = "final_video.mp4"
|
15 |
CAPTION_COLOR = "white"
|
|
|
|
|
16 |
|
17 |
# Placeholder for Kokoro TTS
|
18 |
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 |
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://
|
27 |
-
headers={
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
)
|
31 |
-
response.raise_for_status()
|
32 |
-
|
|
|
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.")
|
@@ -37,8 +53,8 @@ def generate_script(topic):
|
|
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
|
42 |
|
43 |
def generate_audio(script_parts, temp_folder):
|
44 |
full_audio = AudioSegment.empty()
|
|
|
9 |
|
10 |
# Configuration
|
11 |
OPENROUTER_API_KEY = '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'
|
12 |
+
OPENROUTER_MODEL = "tngtech/deepseek-r1t-chimera:free"
|
13 |
TARGET_RESOLUTION = (1080, 1920)
|
14 |
OUTPUT_VIDEO_FILENAME = "final_video.mp4"
|
15 |
CAPTION_COLOR = "white"
|
16 |
+
YOUR_SITE_URL = "http://localhost" # Replace with your site URL
|
17 |
+
YOUR_SITE_NAME = "YouTube Short Creator" # Replace with your site name
|
18 |
|
19 |
# Placeholder for Kokoro TTS
|
20 |
def kokoro_tts(text):
|
21 |
# TODO: Replace with actual Kokoro TTS implementation
|
22 |
# Should return path to generated audio file
|
23 |
return "dummy_audio.wav"
|
24 |
+
|
25 |
def generate_script(topic):
|
26 |
try:
|
|
|
27 |
response = requests.post(
|
28 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
29 |
+
headers={
|
30 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
31 |
+
"Content-Type": "application/json",
|
32 |
+
"HTTP-Referer": YOUR_SITE_URL,
|
33 |
+
"X-Title": YOUR_SITE_NAME,
|
34 |
+
},
|
35 |
+
data=json.dumps({
|
36 |
+
"model": OPENROUTER_MODEL,
|
37 |
+
"messages": [
|
38 |
+
{
|
39 |
+
"role": "user",
|
40 |
+
"content": f"Generate a script about {topic} divided into parts, and output it as a JSON array of strings."
|
41 |
+
}
|
42 |
+
],
|
43 |
+
}),
|
44 |
+
timeout=10
|
45 |
)
|
46 |
+
response.raise_for_status()
|
47 |
+
response_data = response.json()
|
48 |
+
script_json = response_data["choices"][0]["message"]["content"]
|
49 |
return json.loads(script_json)
|
50 |
except requests.exceptions.ConnectionError as e:
|
51 |
raise Exception(f"Failed to connect to OpenRouter API: {str(e)}. Please check your internet connection or DNS settings.")
|
|
|
53 |
raise Exception(f"OpenRouter API returned an error: {str(e)}. Please verify your API key and model.")
|
54 |
except requests.exceptions.RequestException as e:
|
55 |
raise Exception(f"An error occurred while contacting OpenRouter API: {str(e)}")
|
56 |
+
except (json.JSONDecodeError, KeyError):
|
57 |
+
raise Exception("Failed to parse API response as JSON or unexpected response format.")
|
58 |
|
59 |
def generate_audio(script_parts, temp_folder):
|
60 |
full_audio = AudioSegment.empty()
|