SketchTo3D / app.py
chmawia's picture
Update app.py
6264062 verified
import streamlit as st
import requests
from PIL import Image
from io import BytesIO
# Hugging Face API URL (Stable Diffusion or any text-to-image model)
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
def generate_image(prompt, api_key):
"""Generate a cartoon image from text using Hugging Face API"""
headers = {"Authorization": f"Bearer {api_key}"}
payload = {"inputs": prompt}
try:
response = requests.post(API_URL, headers=headers, json=payload)
response.raise_for_status() # Raise an error for bad status codes
image = Image.open(BytesIO(response.content))
return image
except requests.exceptions.RequestException as e:
st.error(f"Error generating image: {e}")
return None
# Streamlit UI
st.title("Text-to-3D Cartoon Generator 🎨")
st.write("Enter a short story, and we'll convert it into a 3D cartoon!")
# User input for Hugging Face API Key
api_key = st.text_input("Enter your Hugging Face API Key:", type="password")
# User input for story
story_text = st.text_area("Enter your story:", "Once upon a time...")
if st.button("Generate 3D Cartoon"):
if not api_key:
st.warning("Please enter your Hugging Face API Key to proceed.")
else:
with st.spinner("Generating cartoon image... ⏳"):
# Generate cartoon image
cartoon_image = generate_image(story_text, api_key)
if cartoon_image:
st.success("Image generated successfully!")
st.image(cartoon_image, caption="Generated Cartoon", use_column_width=True)
# Option to download the generated image
buf = BytesIO()
cartoon_image.save(buf, format="PNG")
byte_im = buf.getvalue()
st.download_button(
label="Download Image",
data=byte_im,
file_name="generated_cartoon.png",
mime="image/png"
)
# Placeholder for 3D conversion
st.write("Next step: Convert this image to a 3D model! πŸš€")
if st.button("Convert to 3D (Simulated)"):
with st.spinner("Converting to 3D... ⏳"):
# Simulate a delay for 3D conversion
st.write("3D conversion is not implemented yet. This is a placeholder.")
st.write("You can integrate tools like OpenAI's Shap-E or Blender for 3D conversion.")
# Sidebar instructions
st.sidebar.header("Instructions")
st.sidebar.write("1. Enter your Hugging Face API Key.")
st.sidebar.write("2. Enter a story in the text box.")
st.sidebar.write("3. Click 'Generate 3D Cartoon'.")
st.sidebar.write("4. View the generated cartoon and explore 3D options.")