marks
commited on
Commit
·
8b6570a
1
Parent(s):
7e5d5c2
Using API instead of pydantic
Browse files- api_clients.py +20 -15
api_clients.py
CHANGED
|
@@ -5,7 +5,7 @@ import elevenlabs
|
|
| 5 |
import time
|
| 6 |
from contextlib import asynccontextmanager
|
| 7 |
from logger import setup_logger, log_execution_time, log_async_execution_time
|
| 8 |
-
from models import
|
| 9 |
|
| 10 |
logger = setup_logger("api_clients")
|
| 11 |
|
|
@@ -18,7 +18,7 @@ class OpenRouterClient:
|
|
| 18 |
self.base_url = "https://openrouter.ai/api/v1"
|
| 19 |
self.headers = {
|
| 20 |
"Authorization": f"Bearer {api_key}",
|
| 21 |
-
"HTTP-Referer": "https://
|
| 22 |
"X-Title": "URL to Podcast Generator", # Required by OpenRouter
|
| 23 |
"Content-Type": "application/json"
|
| 24 |
}
|
|
@@ -37,8 +37,8 @@ class OpenRouterClient:
|
|
| 37 |
# Update headers when API key changes
|
| 38 |
self.headers = {
|
| 39 |
"Authorization": f"Bearer {value}",
|
| 40 |
-
"HTTP-Referer": "https://
|
| 41 |
-
"X-Title": "URL to Podcast Generator",
|
| 42 |
"Content-Type": "application/json",
|
| 43 |
}
|
| 44 |
logger.info("OpenRouter API key updated successfully")
|
|
@@ -88,25 +88,25 @@ class OpenRouterClient:
|
|
| 88 |
logger.error("Prompt too short or missing")
|
| 89 |
raise ValueError("Please provide a more detailed prompt")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
Based on the following content, create an engaging podcast script:
|
| 94 |
|
| 95 |
-
Context: {prompt if prompt else 'Create
|
| 96 |
|
| 97 |
-
Content
|
| 98 |
{content}
|
| 99 |
|
| 100 |
-
|
| 101 |
-
"""
|
| 102 |
|
| 103 |
try:
|
| 104 |
request_data = {
|
| 105 |
"model": model_id,
|
| 106 |
"messages": [
|
| 107 |
-
{"role": "system", "content":
|
| 108 |
-
{"role": "user", "content":
|
| 109 |
-
]
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
|
| 112 |
async with self.get_session() as session:
|
|
@@ -114,9 +114,14 @@ Generate a well-structured podcast script that explains this content in a clear,
|
|
| 114 |
f"{self.base_url}/chat/completions",
|
| 115 |
json=request_data
|
| 116 |
) as response:
|
| 117 |
-
response.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
data = await response.json()
|
| 119 |
return data['choices'][0]['message']['content']
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
logger.error(f"Script generation failed", exc_info=True)
|
| 122 |
raise
|
|
|
|
| 5 |
import time
|
| 6 |
from contextlib import asynccontextmanager
|
| 7 |
from logger import setup_logger, log_execution_time, log_async_execution_time
|
| 8 |
+
from models import OpenRouterModel
|
| 9 |
|
| 10 |
logger = setup_logger("api_clients")
|
| 11 |
|
|
|
|
| 18 |
self.base_url = "https://openrouter.ai/api/v1"
|
| 19 |
self.headers = {
|
| 20 |
"Authorization": f"Bearer {api_key}",
|
| 21 |
+
"HTTP-Referer": "https://localhost:7860", # Required by OpenRouter
|
| 22 |
"X-Title": "URL to Podcast Generator", # Required by OpenRouter
|
| 23 |
"Content-Type": "application/json"
|
| 24 |
}
|
|
|
|
| 37 |
# Update headers when API key changes
|
| 38 |
self.headers = {
|
| 39 |
"Authorization": f"Bearer {value}",
|
| 40 |
+
"HTTP-Referer": "https://localhost:7860",
|
| 41 |
+
"X-Title": "URL to Podcast Generator",
|
| 42 |
"Content-Type": "application/json",
|
| 43 |
}
|
| 44 |
logger.info("OpenRouter API key updated successfully")
|
|
|
|
| 88 |
logger.error("Prompt too short or missing")
|
| 89 |
raise ValueError("Please provide a more detailed prompt")
|
| 90 |
|
| 91 |
+
system_prompt = "You are a professional podcast script writer. Create engaging, conversational content that sounds natural when read aloud."
|
| 92 |
+
user_prompt = f"""Write a podcast script based on the following content. Make it engaging and easy to follow.
|
|
|
|
| 93 |
|
| 94 |
+
Context: {prompt if prompt else 'Create an informative and engaging podcast episode'}
|
| 95 |
|
| 96 |
+
Content:
|
| 97 |
{content}
|
| 98 |
|
| 99 |
+
Format the script in a clear, readable way with appropriate spacing and structure."""
|
|
|
|
| 100 |
|
| 101 |
try:
|
| 102 |
request_data = {
|
| 103 |
"model": model_id,
|
| 104 |
"messages": [
|
| 105 |
+
{"role": "system", "content": system_prompt},
|
| 106 |
+
{"role": "user", "content": user_prompt}
|
| 107 |
+
],
|
| 108 |
+
"temperature": 0.7,
|
| 109 |
+
"max_tokens": 2000
|
| 110 |
}
|
| 111 |
|
| 112 |
async with self.get_session() as session:
|
|
|
|
| 114 |
f"{self.base_url}/chat/completions",
|
| 115 |
json=request_data
|
| 116 |
) as response:
|
| 117 |
+
if response.status != 200:
|
| 118 |
+
error_text = await response.text()
|
| 119 |
+
logger.error(f"OpenRouter API error: {error_text}")
|
| 120 |
+
raise ValueError(f"API request failed: {error_text}")
|
| 121 |
+
|
| 122 |
data = await response.json()
|
| 123 |
return data['choices'][0]['message']['content']
|
| 124 |
+
|
| 125 |
except Exception as e:
|
| 126 |
logger.error(f"Script generation failed", exc_info=True)
|
| 127 |
raise
|