Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import os
|
| 2 |
-
from openai import OpenAI
|
| 3 |
import streamlit as st
|
| 4 |
import requests
|
| 5 |
from PIL import Image
|
|
@@ -7,13 +6,18 @@ from io import BytesIO
|
|
| 7 |
import replicate
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# Page configuration must be the first Streamlit command
|
| 11 |
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
|
| 19 |
# Uncomment if using Google Cloud TTS
|
|
@@ -38,21 +42,23 @@ def generate_story(name: str, theme: str) -> str:
|
|
| 38 |
with protagonist {name} and theme {theme}. Use playful tone,
|
| 39 |
simple dialogue, and a clear beginning, middle, and end.
|
| 40 |
"""
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Format the prompt with user inputs
|
| 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 =
|
| 56 |
response = client.chat.completions.create(
|
| 57 |
model="gpt-4",
|
| 58 |
messages=[{"role": "user", "content": prompt}],
|
|
@@ -113,10 +119,15 @@ def generate_image_dalle(prompt: str, protagonist: str) -> str:
|
|
| 113 |
Call OpenAI Image API to create one 512×512 image from the prompt.
|
| 114 |
Returns the image URL.
|
| 115 |
"""
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# Format the prompt with user inputs
|
| 122 |
full_prompt = img_prompt_template.format(
|
|
@@ -126,7 +137,7 @@ def generate_image_dalle(prompt: str, protagonist: str) -> str:
|
|
| 126 |
|
| 127 |
try:
|
| 128 |
# Usar la nueva versión de la API de OpenAI (v1.x)
|
| 129 |
-
client =
|
| 130 |
response = client.images.generate(
|
| 131 |
prompt=full_prompt,
|
| 132 |
n=1,
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
from PIL import Image
|
|
|
|
| 6 |
import replicate
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
|
| 9 |
+
# Para la nueva versión de OpenAI
|
| 10 |
+
from openai import OpenAI
|
| 11 |
+
|
| 12 |
# Page configuration must be the first Streamlit command
|
| 13 |
st.set_page_config(page_title="CuentAI – AI Story Maker", layout="wide")
|
| 14 |
|
| 15 |
# Load environment variables
|
| 16 |
load_dotenv()
|
| 17 |
+
|
| 18 |
+
# Configuración de APIs
|
| 19 |
replicate_token = os.getenv("REPLICATE_API_TOKEN")
|
| 20 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 21 |
|
| 22 |
# Optional TTS setup
|
| 23 |
# Uncomment if using Google Cloud TTS
|
|
|
|
| 42 |
with protagonist {name} and theme {theme}. Use playful tone,
|
| 43 |
simple dialogue, and a clear beginning, middle, and end.
|
| 44 |
"""
|
| 45 |
+
# Prompt predeterminado integrado en caso de que fallen los archivos externos
|
| 46 |
+
default_template = (
|
| 47 |
+
"You are a children's story author. "
|
| 48 |
+
"Write a 300-400 word children's story where the protagonist is named {name} "
|
| 49 |
+
"and the plot is about {theme}. Use a friendly style and simple dialogue. "
|
| 50 |
+
"Include a clear beginning, middle, and end."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Intentar cargar desde archivo, con fallback al predeterminado
|
| 54 |
+
prompt_template = load_prompt("prompts/story_prompt.txt", default_template)
|
| 55 |
|
| 56 |
# Format the prompt with user inputs
|
| 57 |
prompt = prompt_template.format(name=name, theme=theme)
|
| 58 |
|
| 59 |
try:
|
| 60 |
# Usar la nueva versión de la API de OpenAI (v1.x)
|
| 61 |
+
client = OpenAI(api_key=openai_api_key)
|
| 62 |
response = client.chat.completions.create(
|
| 63 |
model="gpt-4",
|
| 64 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 119 |
Call OpenAI Image API to create one 512×512 image from the prompt.
|
| 120 |
Returns the image URL.
|
| 121 |
"""
|
| 122 |
+
# Prompt predeterminado para generación de imágenes
|
| 123 |
+
default_template = (
|
| 124 |
+
"Create a colorful children's book style illustration showing: {scene_description} "
|
| 125 |
+
"With {protagonist_name} as the main character. Use bright colors, simple shapes, "
|
| 126 |
+
"and a child-friendly art style."
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Intentar cargar el prompt desde archivo con fallback al predeterminado
|
| 130 |
+
img_prompt_template = load_prompt("prompts/image_prompt.txt", default_template)
|
| 131 |
|
| 132 |
# Format the prompt with user inputs
|
| 133 |
full_prompt = img_prompt_template.format(
|
|
|
|
| 137 |
|
| 138 |
try:
|
| 139 |
# Usar la nueva versión de la API de OpenAI (v1.x)
|
| 140 |
+
client = OpenAI(api_key=openai_api_key)
|
| 141 |
response = client.images.generate(
|
| 142 |
prompt=full_prompt,
|
| 143 |
n=1,
|