Spaces:
Runtime error
Runtime error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -5,17 +5,19 @@ import json
|
|
| 5 |
import anthropic
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
# Initialize the Claude client with API key from environment
|
| 9 |
-
api_key = os.
|
| 10 |
-
if not api_key
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Create the client
|
| 14 |
-
try:
|
| 15 |
-
client = anthropic.Anthropic(api_key=api_key)
|
| 16 |
-
except Exception as e:
|
| 17 |
-
st.sidebar.error(f"Error initializing Anthropic API: {str(e)}")
|
| 18 |
client = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# Define available templates
|
| 21 |
TEMPLATES = {
|
|
@@ -120,7 +122,7 @@ def generate_storyboard(title, purpose, audience):
|
|
| 120 |
|
| 121 |
try:
|
| 122 |
response = client.messages.create(
|
| 123 |
-
model="claude-3-sonnet-
|
| 124 |
max_tokens=4000,
|
| 125 |
temperature=0.7,
|
| 126 |
system="You are an expert presentation designer specializing in creating logical, compelling PowerPoint storyboards. Always output valid JSON without explanations or extra text.",
|
|
@@ -130,6 +132,9 @@ def generate_storyboard(title, purpose, audience):
|
|
| 130 |
)
|
| 131 |
response_text = response.content[0].text
|
| 132 |
|
|
|
|
|
|
|
|
|
|
| 133 |
# Extract the JSON from the response
|
| 134 |
json_start = response_text.find("[")
|
| 135 |
json_end = response_text.rfind("]") + 1
|
|
@@ -141,11 +146,14 @@ def generate_storyboard(title, purpose, audience):
|
|
| 141 |
return parsed_json
|
| 142 |
else:
|
| 143 |
progress_text.error("JSON not found in response")
|
|
|
|
| 144 |
# Create a default storyboard as fallback
|
| 145 |
return default_storyboard(title)
|
| 146 |
except Exception as e:
|
| 147 |
with st.sidebar:
|
| 148 |
st.error(f"Error generating storyboard: {str(e)}")
|
|
|
|
|
|
|
| 149 |
return default_storyboard(title)
|
| 150 |
|
| 151 |
def default_storyboard(title):
|
|
@@ -212,7 +220,7 @@ def generate_slide_content(slide_info, template_name):
|
|
| 212 |
|
| 213 |
try:
|
| 214 |
response = client.messages.create(
|
| 215 |
-
model="claude-3-sonnet-
|
| 216 |
max_tokens=2000,
|
| 217 |
temperature=0.5,
|
| 218 |
system="You are an expert presentation content creator. Always output valid JSON without explanations or extra text.",
|
|
@@ -231,9 +239,12 @@ def generate_slide_content(slide_info, template_name):
|
|
| 231 |
return json.loads(json_str)
|
| 232 |
else:
|
| 233 |
# Fallback: create a basic slide
|
|
|
|
| 234 |
return default_slide_content(slide_info)
|
| 235 |
except Exception as e:
|
| 236 |
st.sidebar.error(f"Error generating slide content: {str(e)}")
|
|
|
|
|
|
|
| 237 |
# Return a default structure
|
| 238 |
return default_slide_content(slide_info)
|
| 239 |
|
|
|
|
| 5 |
import anthropic
|
| 6 |
import os
|
| 7 |
|
| 8 |
+
# Initialize the Claude client with API key from environment (Huggingface secrets)
|
| 9 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 10 |
+
if not api_key:
|
| 11 |
+
st.sidebar.error("ANTHROPIC_API_KEY not found in environment variables")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
client = None
|
| 13 |
+
else:
|
| 14 |
+
# Create the client
|
| 15 |
+
try:
|
| 16 |
+
client = anthropic.Anthropic(api_key=api_key)
|
| 17 |
+
st.sidebar.success("Anthropic API client initialized successfully")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
st.sidebar.error(f"Error initializing Anthropic API: {str(e)}")
|
| 20 |
+
client = None
|
| 21 |
|
| 22 |
# Define available templates
|
| 23 |
TEMPLATES = {
|
|
|
|
| 122 |
|
| 123 |
try:
|
| 124 |
response = client.messages.create(
|
| 125 |
+
model="claude-3-7-sonnet-20250219",
|
| 126 |
max_tokens=4000,
|
| 127 |
temperature=0.7,
|
| 128 |
system="You are an expert presentation designer specializing in creating logical, compelling PowerPoint storyboards. Always output valid JSON without explanations or extra text.",
|
|
|
|
| 132 |
)
|
| 133 |
response_text = response.content[0].text
|
| 134 |
|
| 135 |
+
# For debugging, show raw response
|
| 136 |
+
st.sidebar.write("Raw response: " + response_text[:200] + "...")
|
| 137 |
+
|
| 138 |
# Extract the JSON from the response
|
| 139 |
json_start = response_text.find("[")
|
| 140 |
json_end = response_text.rfind("]") + 1
|
|
|
|
| 146 |
return parsed_json
|
| 147 |
else:
|
| 148 |
progress_text.error("JSON not found in response")
|
| 149 |
+
st.sidebar.error(f"Full response: {response_text}")
|
| 150 |
# Create a default storyboard as fallback
|
| 151 |
return default_storyboard(title)
|
| 152 |
except Exception as e:
|
| 153 |
with st.sidebar:
|
| 154 |
st.error(f"Error generating storyboard: {str(e)}")
|
| 155 |
+
import traceback
|
| 156 |
+
st.error(traceback.format_exc())
|
| 157 |
return default_storyboard(title)
|
| 158 |
|
| 159 |
def default_storyboard(title):
|
|
|
|
| 220 |
|
| 221 |
try:
|
| 222 |
response = client.messages.create(
|
| 223 |
+
model="claude-3-7-sonnet-20250219",
|
| 224 |
max_tokens=2000,
|
| 225 |
temperature=0.5,
|
| 226 |
system="You are an expert presentation content creator. Always output valid JSON without explanations or extra text.",
|
|
|
|
| 239 |
return json.loads(json_str)
|
| 240 |
else:
|
| 241 |
# Fallback: create a basic slide
|
| 242 |
+
st.sidebar.error(f"JSON not found in response: {response_text}")
|
| 243 |
return default_slide_content(slide_info)
|
| 244 |
except Exception as e:
|
| 245 |
st.sidebar.error(f"Error generating slide content: {str(e)}")
|
| 246 |
+
import traceback
|
| 247 |
+
st.sidebar.error(traceback.format_exc())
|
| 248 |
# Return a default structure
|
| 249 |
return default_slide_content(slide_info)
|
| 250 |
|