Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
import streamlit as st
|
| 4 |
import requests
|
| 5 |
from PIL import Image
|
|
@@ -12,7 +12,7 @@ st.set_page_config(page_title="CuentAI – AI Story Maker", layout="wide")
|
|
| 12 |
|
| 13 |
# Load environment variables
|
| 14 |
load_dotenv()
|
| 15 |
-
|
| 16 |
replicate_token = os.getenv("REPLICATE_API_TOKEN")
|
| 17 |
|
| 18 |
# Optional TTS setup
|
|
@@ -20,13 +20,16 @@ replicate_token = os.getenv("REPLICATE_API_TOKEN")
|
|
| 20 |
# from google.cloud import texttospeech
|
| 21 |
|
| 22 |
# Function to load prompt templates
|
| 23 |
-
def load_prompt(file_path):
|
|
|
|
| 24 |
try:
|
| 25 |
with open(file_path, 'r', encoding='utf-8') as file:
|
| 26 |
return file.read().strip()
|
| 27 |
except Exception as e:
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Story generation function
|
| 32 |
def generate_story(name: str, theme: str) -> str:
|
|
@@ -48,7 +51,9 @@ def generate_story(name: str, theme: str) -> str:
|
|
| 48 |
prompt = prompt_template.format(name=name, theme=theme)
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
model="gpt-4",
|
| 53 |
messages=[{"role": "user", "content": prompt}],
|
| 54 |
temperature=0.8,
|
|
@@ -120,12 +125,14 @@ def generate_image_dalle(prompt: str, protagonist: str) -> str:
|
|
| 120 |
)
|
| 121 |
|
| 122 |
try:
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
prompt=full_prompt,
|
| 125 |
n=1,
|
| 126 |
size="512x512"
|
| 127 |
)
|
| 128 |
-
return response
|
| 129 |
except Exception as e:
|
| 130 |
st.error(f"Error generating image: {e}")
|
| 131 |
# Return a placeholder image URL
|
|
|
|
| 1 |
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
import streamlit as st
|
| 4 |
import requests
|
| 5 |
from PIL import Image
|
|
|
|
| 12 |
|
| 13 |
# Load environment variables
|
| 14 |
load_dotenv()
|
| 15 |
+
# El cliente OpenAI se inicializa en cada función para evitar problemas de compatibilidad
|
| 16 |
replicate_token = os.getenv("REPLICATE_API_TOKEN")
|
| 17 |
|
| 18 |
# Optional TTS setup
|
|
|
|
| 20 |
# from google.cloud import texttospeech
|
| 21 |
|
| 22 |
# Function to load prompt templates
|
| 23 |
+
def load_prompt(file_path, default_prompt=""):
|
| 24 |
+
"""Cargar un archivo de prompt o devolver un valor predeterminado si falla"""
|
| 25 |
try:
|
| 26 |
with open(file_path, 'r', encoding='utf-8') as file:
|
| 27 |
return file.read().strip()
|
| 28 |
except Exception as e:
|
| 29 |
+
# No mostrar error si tenemos un valor predeterminado
|
| 30 |
+
if not default_prompt:
|
| 31 |
+
st.warning(f"No se pudo cargar el archivo {file_path}. Usando prompt predeterminado.")
|
| 32 |
+
return default_prompt
|
| 33 |
|
| 34 |
# Story generation function
|
| 35 |
def generate_story(name: str, theme: str) -> str:
|
|
|
|
| 51 |
prompt = prompt_template.format(name=name, theme=theme)
|
| 52 |
|
| 53 |
try:
|
| 54 |
+
# Usar la nueva versión de la API de OpenAI (v1.x)
|
| 55 |
+
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 56 |
+
response = client.chat.completions.create(
|
| 57 |
model="gpt-4",
|
| 58 |
messages=[{"role": "user", "content": prompt}],
|
| 59 |
temperature=0.8,
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
try:
|
| 128 |
+
# Usar la nueva versión de la API de OpenAI (v1.x)
|
| 129 |
+
client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 130 |
+
response = client.images.generate(
|
| 131 |
prompt=full_prompt,
|
| 132 |
n=1,
|
| 133 |
size="512x512"
|
| 134 |
)
|
| 135 |
+
return response.data[0].url
|
| 136 |
except Exception as e:
|
| 137 |
st.error(f"Error generating image: {e}")
|
| 138 |
# Return a placeholder image URL
|