Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -19,12 +19,8 @@ replicate_token = os.getenv("REPLICATE_API_TOKEN")
|
|
| 19 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 20 |
|
| 21 |
# Funciones para comunicarse con OpenAI directamente a trav茅s de requests en lugar de usar el SDK
|
| 22 |
-
def openai_chat_completion(prompt, model="gpt-
|
| 23 |
"""Usa la API de OpenAI directamente a trav茅s de requests para evitar problemas con el SDK"""
|
| 24 |
-
|
| 25 |
-
# Mostrar informaci贸n para debugging
|
| 26 |
-
st.info(f"Usando clave de API: {openai_api_key[:5]}...{openai_api_key[-4:]}")
|
| 27 |
-
|
| 28 |
headers = {
|
| 29 |
"Content-Type": "application/json",
|
| 30 |
"Authorization": f"Bearer {openai_api_key}"
|
|
@@ -38,29 +34,21 @@ def openai_chat_completion(prompt, model="gpt-4-turbo-preview", temperature=0.8,
|
|
| 38 |
}
|
| 39 |
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"https://api.openai.com/v1/chat/completions",
|
| 49 |
-
headers=headers,
|
| 50 |
-
json=payload,
|
| 51 |
-
timeout=60 # Aumentar timeout para evitar errores por tiempo
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
st.write(f"Estado de respuesta: {response.status_code}")
|
| 55 |
-
|
| 56 |
-
# Si hay un error, mostrar el detalle
|
| 57 |
-
if response.status_code != 200:
|
| 58 |
-
st.error(f"Error de API: {response.status_code} - {response.text}")
|
| 59 |
-
return None
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
except Exception as e:
|
| 65 |
st.error(f"Error con la API de OpenAI: {str(e)}")
|
| 66 |
return None
|
|
@@ -81,16 +69,23 @@ def openai_generate_image(prompt):
|
|
| 81 |
}
|
| 82 |
|
| 83 |
try:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
except Exception as e:
|
| 92 |
-
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
# Optional TTS setup
|
| 96 |
# Uncomment if using Google Cloud TTS
|
|
@@ -129,13 +124,6 @@ def generate_story(name: str, theme: str) -> str:
|
|
| 129 |
# Format the prompt with user inputs
|
| 130 |
prompt = prompt_template.format(name=name, theme=theme)
|
| 131 |
|
| 132 |
-
# Mostrar mensaje de carga
|
| 133 |
-
with st.spinner("Generando tu historia personalizada..."):
|
| 134 |
-
# Informaci贸n de depuraci贸n
|
| 135 |
-
st.write("Datos de entrada:")
|
| 136 |
-
st.write(f"- Protagonista: {name}")
|
| 137 |
-
st.write(f"- Tema: {theme}")
|
| 138 |
-
|
| 139 |
try:
|
| 140 |
# Usar nuestra funci贸n personalizada que se comunica directamente con la API
|
| 141 |
story = openai_chat_completion(prompt)
|
|
@@ -301,44 +289,124 @@ def generate_audio_tts(text: str, filename="narration.mp3") -> str:
|
|
| 301 |
def set_custom_css():
|
| 302 |
st.markdown("""
|
| 303 |
<style>
|
| 304 |
-
@import url('https://fonts.googleapis.com/css2?family=
|
| 305 |
|
| 306 |
* {
|
| 307 |
-
font-family: '
|
| 308 |
}
|
| 309 |
|
| 310 |
h1, h2, h3 {
|
| 311 |
-
color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
}
|
| 313 |
|
| 314 |
.stApp {
|
| 315 |
-
background-color: #
|
| 316 |
}
|
| 317 |
|
| 318 |
.stButton>button {
|
| 319 |
-
background-color: #
|
| 320 |
color: white;
|
| 321 |
-
font-weight:
|
| 322 |
-
border-radius:
|
| 323 |
-
padding:
|
| 324 |
border: none;
|
|
|
|
|
|
|
|
|
|
| 325 |
}
|
| 326 |
|
| 327 |
.stButton>button:hover {
|
| 328 |
-
background-color: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
}
|
| 330 |
|
|
|
|
| 331 |
.scene-container {
|
| 332 |
background-color: white;
|
| 333 |
-
padding:
|
| 334 |
-
border-radius:
|
| 335 |
-
box-shadow: 0
|
| 336 |
-
margin-bottom:
|
|
|
|
| 337 |
}
|
| 338 |
|
| 339 |
.story-text {
|
| 340 |
font-size: 18px;
|
| 341 |
-
line-height: 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
}
|
| 343 |
</style>
|
| 344 |
""", unsafe_allow_html=True)
|
|
|
|
| 19 |
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 20 |
|
| 21 |
# Funciones para comunicarse con OpenAI directamente a trav茅s de requests en lugar de usar el SDK
|
| 22 |
+
def openai_chat_completion(prompt, model="gpt-3.5-turbo", temperature=0.8, max_tokens=600):
|
| 23 |
"""Usa la API de OpenAI directamente a trav茅s de requests para evitar problemas con el SDK"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
headers = {
|
| 25 |
"Content-Type": "application/json",
|
| 26 |
"Authorization": f"Bearer {openai_api_key}"
|
|
|
|
| 34 |
}
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
with st.spinner("Generando historia personalizada..."):
|
| 38 |
+
response = requests.post(
|
| 39 |
+
"https://api.openai.com/v1/chat/completions",
|
| 40 |
+
headers=headers,
|
| 41 |
+
json=payload,
|
| 42 |
+
timeout=60 # Aumentar timeout para evitar errores por tiempo
|
| 43 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Si hay un error, mostrar el detalle de forma discreta
|
| 46 |
+
if response.status_code != 200:
|
| 47 |
+
st.error("No se pudo generar la historia. Intenta de nuevo m谩s tarde.")
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
response_data = response.json()
|
| 51 |
+
return response_data["choices"][0]["message"]["content"]
|
| 52 |
except Exception as e:
|
| 53 |
st.error(f"Error con la API de OpenAI: {str(e)}")
|
| 54 |
return None
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
try:
|
| 72 |
+
with st.spinner("Creando ilustraci贸n m谩gica..."):
|
| 73 |
+
response = requests.post(
|
| 74 |
+
"https://api.openai.com/v1/images/generations",
|
| 75 |
+
headers=headers,
|
| 76 |
+
json=payload,
|
| 77 |
+
timeout=60
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if response.status_code != 200:
|
| 81 |
+
st.error("No se pudo generar la ilustraci贸n. Se usar谩 una imagen de muestra.")
|
| 82 |
+
return "https://via.placeholder.com/512x512.png?text=Imagen+No+Disponible"
|
| 83 |
+
|
| 84 |
+
return response.json()["data"][0]["url"]
|
| 85 |
except Exception as e:
|
| 86 |
+
# Error discreto sin mensajes t茅cnicos
|
| 87 |
+
st.error("Ocurri贸 un problema al crear la ilustraci贸n.")
|
| 88 |
+
return "https://via.placeholder.com/512x512.png?text=Imagen+No+Disponible"
|
| 89 |
|
| 90 |
# Optional TTS setup
|
| 91 |
# Uncomment if using Google Cloud TTS
|
|
|
|
| 124 |
# Format the prompt with user inputs
|
| 125 |
prompt = prompt_template.format(name=name, theme=theme)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
try:
|
| 128 |
# Usar nuestra funci贸n personalizada que se comunica directamente con la API
|
| 129 |
story = openai_chat_completion(prompt)
|
|
|
|
| 289 |
def set_custom_css():
|
| 290 |
st.markdown("""
|
| 291 |
<style>
|
| 292 |
+
@import url('https://fonts.googleapis.com/css2?family=Quicksand:wght@400;500;600;700&display=swap');
|
| 293 |
|
| 294 |
* {
|
| 295 |
+
font-family: 'Quicksand', sans-serif;
|
| 296 |
}
|
| 297 |
|
| 298 |
h1, h2, h3 {
|
| 299 |
+
color: #6C63FF;
|
| 300 |
+
font-weight: 700;
|
| 301 |
+
}
|
| 302 |
+
|
| 303 |
+
h1 {
|
| 304 |
+
font-size: 2.5rem;
|
| 305 |
+
margin-bottom: 0.5rem;
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
h3 {
|
| 309 |
+
font-size: 1.5rem;
|
| 310 |
+
font-weight: 500;
|
| 311 |
+
opacity: 0.8;
|
| 312 |
+
margin-bottom: 2rem;
|
| 313 |
}
|
| 314 |
|
| 315 |
.stApp {
|
| 316 |
+
background-color: #F8F9FA;
|
| 317 |
}
|
| 318 |
|
| 319 |
.stButton>button {
|
| 320 |
+
background-color: #6C63FF;
|
| 321 |
color: white;
|
| 322 |
+
font-weight: 600;
|
| 323 |
+
border-radius: 50px;
|
| 324 |
+
padding: 12px 24px;
|
| 325 |
border: none;
|
| 326 |
+
transition: all 0.3s ease;
|
| 327 |
+
font-size: 16px;
|
| 328 |
+
box-shadow: 0 4px 6px rgba(108, 99, 255, 0.2);
|
| 329 |
}
|
| 330 |
|
| 331 |
.stButton>button:hover {
|
| 332 |
+
background-color: #5A52E0;
|
| 333 |
+
transform: translateY(-2px);
|
| 334 |
+
box-shadow: 0 6px 10px rgba(108, 99, 255, 0.3);
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
.stButton>button:active {
|
| 338 |
+
transform: translateY(0);
|
| 339 |
+
box-shadow: 0 2px 4px rgba(108, 99, 255, 0.2);
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
/* Colorful tabs for scenes */
|
| 343 |
+
.stTabs [data-baseweb="tab-list"] {
|
| 344 |
+
gap: 8px;
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
.stTabs [data-baseweb="tab"] {
|
| 348 |
+
background-color: #F0F0FF;
|
| 349 |
+
border-radius: 10px 10px 0 0;
|
| 350 |
+
padding: 8px 16px;
|
| 351 |
+
border: none;
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
.stTabs [aria-selected="true"] {
|
| 355 |
+
background-color: #6C63FF !important;
|
| 356 |
+
color: white !important;
|
| 357 |
}
|
| 358 |
|
| 359 |
+
/* Card-like containers for each scene */
|
| 360 |
.scene-container {
|
| 361 |
background-color: white;
|
| 362 |
+
padding: 24px;
|
| 363 |
+
border-radius: 16px;
|
| 364 |
+
box-shadow: 0 8px 16px rgba(0,0,0,0.08);
|
| 365 |
+
margin-bottom: 24px;
|
| 366 |
+
border: 1px solid #f0f0f0;
|
| 367 |
}
|
| 368 |
|
| 369 |
.story-text {
|
| 370 |
font-size: 18px;
|
| 371 |
+
line-height: 1.7;
|
| 372 |
+
color: #333;
|
| 373 |
+
}
|
| 374 |
+
|
| 375 |
+
/* Improve sidebar appearance */
|
| 376 |
+
[data-testid="stSidebar"] > div:first-child {
|
| 377 |
+
background-color: #F9F7FF;
|
| 378 |
+
padding: 2rem 1rem;
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
/* Error and success messages */
|
| 382 |
+
.element-container div[data-testid="stAlert"] {
|
| 383 |
+
border-radius: 10px;
|
| 384 |
+
padding: 12px;
|
| 385 |
+
}
|
| 386 |
+
|
| 387 |
+
/* Input fields */
|
| 388 |
+
.stTextInput>div>div>input {
|
| 389 |
+
border-radius: 10px;
|
| 390 |
+
border: 2px solid #E0E0FF;
|
| 391 |
+
padding: 12px 16px;
|
| 392 |
+
font-size: 16px;
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
.stTextInput>div>div>input:focus {
|
| 396 |
+
border-color: #6C63FF;
|
| 397 |
+
box-shadow: 0 0 0 2px rgba(108, 99, 255, 0.2);
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
/* Slider styling */
|
| 401 |
+
.stSlider [data-baseweb="slider"] {
|
| 402 |
+
height: 6px;
|
| 403 |
+
}
|
| 404 |
+
|
| 405 |
+
.stSlider [data-baseweb="thumb"] {
|
| 406 |
+
background-color: #6C63FF;
|
| 407 |
+
border-color: #6C63FF;
|
| 408 |
+
width: 20px;
|
| 409 |
+
height: 20px;
|
| 410 |
}
|
| 411 |
</style>
|
| 412 |
""", unsafe_allow_html=True)
|