Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +72 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,74 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
"""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
# Configuración
|
| 5 |
+
TEXT_ENDPOINT = "https://lrs1lm2jji.execute-api.us-east-1.amazonaws.com/dev/generate"
|
| 6 |
+
IMAGE_ENDPOINT = "https://lrs1lm2jji.execute-api.us-east-1.amazonaws.com/dev/generate-image"
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="OpenAI App", layout="centered")
|
| 9 |
+
|
| 10 |
+
st.title("🧠 Aplicación IA con OpenAI y Lambda")
|
| 11 |
+
|
| 12 |
+
tab1, tab2 = st.tabs(["Generar Texto", "Generar Imagen"])
|
| 13 |
+
|
| 14 |
+
with tab1:
|
| 15 |
+
st.subheader("✍️ Texto Generado por IA")
|
| 16 |
+
prompt = st.text_area("Escribe tu prompt aquí:", height=150)
|
| 17 |
+
|
| 18 |
+
if st.button("Generar Texto"):
|
| 19 |
+
if prompt.strip() == "":
|
| 20 |
+
st.warning("Escribe un prompt primero.")
|
| 21 |
+
else:
|
| 22 |
+
with st.spinner("Generando..."):
|
| 23 |
+
try:
|
| 24 |
+
res = requests.post(TEXT_ENDPOINT, json={"prompt": prompt})
|
| 25 |
+
res.raise_for_status()
|
| 26 |
+
result = res.json()
|
| 27 |
+
|
| 28 |
+
st.success(f"Respuesta generada con éxito: {res}")
|
| 29 |
+
|
| 30 |
+
if "response" in result:
|
| 31 |
+
st.text_area("Texto:", value=result["response"], height=300)
|
| 32 |
+
elif "text" in result:
|
| 33 |
+
st.text_area("Texto:", value=result["text"], height=300)
|
| 34 |
+
|
| 35 |
+
if "imageUrl" in result:
|
| 36 |
+
st.image(result["imageUrl"], caption="Imagen generada", use_column_width=True)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
st.error(f"Error al generar: {e}")
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
with tab2:
|
| 42 |
+
st.subheader("🖼️ Imagen Generada por IA")
|
| 43 |
+
img_prompt = st.text_input("Describe la imagen que deseas:")
|
| 44 |
+
|
| 45 |
+
if st.button("Generar Imagen"):
|
| 46 |
+
if img_prompt.strip() == "":
|
| 47 |
+
st.warning("Escribe una descripción primero.")
|
| 48 |
+
else:
|
| 49 |
+
with st.spinner("Generando imagen..."):
|
| 50 |
+
try:
|
| 51 |
+
st.info("⏳ Enviando solicitud para generar imagen...")
|
| 52 |
+
st.write("🔍 Prompt enviado:", img_prompt)
|
| 53 |
+
|
| 54 |
+
headers = {
|
| 55 |
+
"Content-Type": "application/json",
|
| 56 |
+
"User-Agent": "Python-Requests"
|
| 57 |
+
}
|
| 58 |
+
res = requests.post(IMAGE_ENDPOINT, json={"prompt": img_prompt}, headers=headers)
|
| 59 |
+
st.write("📬 Código de respuesta:", res.status_code)
|
| 60 |
+
st.write("📦 Respuesta cruda:", res.text)
|
| 61 |
+
res.raise_for_status()
|
| 62 |
+
data = res.json()
|
| 63 |
+
st.write("🧩 JSON recibido:", data)
|
| 64 |
+
url = data.get("image_url") or data.get("imageUrl") or data.get("url")
|
| 65 |
+
|
| 66 |
+
if url:
|
| 67 |
+
st.success("✅ Imagen generada:")
|
| 68 |
+
st.image(url, use_container_width =True)
|
| 69 |
+
else:
|
| 70 |
+
st.error("⚠️ No se pudo obtener la URL de imagen.")
|
| 71 |
+
except Exception as e:
|
| 72 |
+
st.error(f"❌ Error al generar imagen: {e}")
|
| 73 |
+
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|