marks
commited on
Commit
·
94bdc12
1
Parent(s):
42683f3
Made OpenRouter more robust
Browse files- api_clients.py +26 -50
- app.py +2 -4
api_clients.py
CHANGED
|
@@ -13,6 +13,11 @@ class OpenRouterClient:
|
|
| 13 |
def __init__(self, api_key: str):
|
| 14 |
logger.info("Initializing OpenRouter client")
|
| 15 |
self.api_key = api_key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
logger.debug("OpenRouter client initialized successfully")
|
| 17 |
|
| 18 |
@property
|
|
@@ -50,18 +55,14 @@ class OpenRouterClient:
|
|
| 50 |
ValueError: If API request fails
|
| 51 |
"""
|
| 52 |
logger.info("Fetching available models from OpenRouter")
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
logger.info(f"Successfully fetched {len(models)} models")
|
| 62 |
-
logger.debug(f"Available models: {[model['name'] for model in models]}")
|
| 63 |
-
return [(model['id'], f"{model['name']} ({model['context_length']} tokens)")
|
| 64 |
-
for model in models]
|
| 65 |
|
| 66 |
@log_async_execution_time(logger)
|
| 67 |
async def generate_script(self, content: str, prompt: str, model_id: str) -> str:
|
|
@@ -83,48 +84,23 @@ class OpenRouterClient:
|
|
| 83 |
raise ValueError("Please provide a more detailed prompt")
|
| 84 |
|
| 85 |
try:
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"Script generation failed", exc_info=True)
|
| 97 |
raise
|
| 98 |
|
| 99 |
-
async def _make_script_request(self, session, content, prompt, model_id):
|
| 100 |
-
async with session.post(
|
| 101 |
-
f"{self.base_url}/chat/completions",
|
| 102 |
-
json={
|
| 103 |
-
"model": model_id,
|
| 104 |
-
"messages": [
|
| 105 |
-
{
|
| 106 |
-
"role": "system",
|
| 107 |
-
"content": "You are an expert podcast script writer. Create engaging, conversational content."
|
| 108 |
-
},
|
| 109 |
-
{
|
| 110 |
-
"role": "user",
|
| 111 |
-
"content": f"""Based on this content: {content}
|
| 112 |
-
Create a 3-minute podcast script focusing on: {prompt}
|
| 113 |
-
Format as a natural conversation with clear speaker parts.
|
| 114 |
-
Include [HOST] and [GUEST] markers for different voices."""
|
| 115 |
-
}
|
| 116 |
-
]
|
| 117 |
-
}
|
| 118 |
-
) as response:
|
| 119 |
-
logger.debug("Sending script generation request")
|
| 120 |
-
|
| 121 |
-
if response.status != 200:
|
| 122 |
-
error_msg = await response.text()
|
| 123 |
-
logger.error(f"Script generation failed: {error_msg}")
|
| 124 |
-
raise ValueError(f"Script generation failed: {error_msg}")
|
| 125 |
-
|
| 126 |
-
return await response.json()
|
| 127 |
-
|
| 128 |
class ElevenLabsClient:
|
| 129 |
"""Handles ElevenLabs API interactions with detailed performance tracking"""
|
| 130 |
|
|
|
|
| 13 |
def __init__(self, api_key: str):
|
| 14 |
logger.info("Initializing OpenRouter client")
|
| 15 |
self.api_key = api_key
|
| 16 |
+
self.base_url = "https://openrouter.ai/api/v1"
|
| 17 |
+
self.headers = {
|
| 18 |
+
"Authorization": f"Bearer {api_key}",
|
| 19 |
+
"Content-Type": "application/json"
|
| 20 |
+
}
|
| 21 |
logger.debug("OpenRouter client initialized successfully")
|
| 22 |
|
| 23 |
@property
|
|
|
|
| 55 |
ValueError: If API request fails
|
| 56 |
"""
|
| 57 |
logger.info("Fetching available models from OpenRouter")
|
| 58 |
+
response = requests.get(
|
| 59 |
+
f"{self.base_url}/models",
|
| 60 |
+
headers=self.headers
|
| 61 |
+
)
|
| 62 |
+
response.raise_for_status()
|
| 63 |
+
models = response.json()["data"]
|
| 64 |
+
# Return tuple of (id, display_name) for each model
|
| 65 |
+
return [(model["id"], model["name"]) for model in models]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
@log_async_execution_time(logger)
|
| 68 |
async def generate_script(self, content: str, prompt: str, model_id: str) -> str:
|
|
|
|
| 84 |
raise ValueError("Please provide a more detailed prompt")
|
| 85 |
|
| 86 |
try:
|
| 87 |
+
response = requests.post(
|
| 88 |
+
f"{self.base_url}/chat/completions",
|
| 89 |
+
headers=self.headers,
|
| 90 |
+
json={
|
| 91 |
+
"model": model_id,
|
| 92 |
+
"messages": [
|
| 93 |
+
{"role": "system", "content": "You are a podcast script writer."},
|
| 94 |
+
{"role": "user", "content": f"Create a podcast script from this content: {content}"}
|
| 95 |
+
]
|
| 96 |
+
}
|
| 97 |
+
)
|
| 98 |
+
response.raise_for_status()
|
| 99 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 100 |
except Exception as e:
|
| 101 |
logger.error(f"Script generation failed", exc_info=True)
|
| 102 |
raise
|
| 103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
class ElevenLabsClient:
|
| 105 |
"""Handles ElevenLabs API interactions with detailed performance tracking"""
|
| 106 |
|
app.py
CHANGED
|
@@ -89,11 +89,9 @@ def create_ui():
|
|
| 89 |
try:
|
| 90 |
client = OpenRouterClient(key)
|
| 91 |
models = await client.get_models()
|
| 92 |
-
return gr.Dropdown(
|
| 93 |
-
choices={name: id for id, name in models}
|
| 94 |
-
)
|
| 95 |
except Exception as e:
|
| 96 |
-
return gr.Dropdown(choices=
|
| 97 |
|
| 98 |
def update_elevenlabs_voices(key):
|
| 99 |
if not key:
|
|
|
|
| 89 |
try:
|
| 90 |
client = OpenRouterClient(key)
|
| 91 |
models = await client.get_models()
|
| 92 |
+
return gr.Dropdown(choices=models)
|
|
|
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
+
return gr.Dropdown(choices=[("error", f"Error: {str(e)}")])
|
| 95 |
|
| 96 |
def update_elevenlabs_voices(key):
|
| 97 |
if not key:
|