Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,70 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import torch
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
-
import story_generator #
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# β
|
| 10 |
class StoryRequest(BaseModel):
|
| 11 |
theme: str
|
| 12 |
reading_level: str
|
| 13 |
|
| 14 |
-
# β
Load Image Model (Cartoon-Style)
|
| 15 |
CACHE_DIR = "/tmp/huggingface"
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
)
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
@app.post("/generate_story_and_image")
|
| 24 |
def generate_story_and_image(request: StoryRequest):
|
| 25 |
"""Generates a story and an image based on the given theme and reading level."""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
@app.get("/")
|
| 52 |
def home():
|
| 53 |
-
return {"message": "Welcome to the Story & Image Generation API! Use /generate_story_and_image"}
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import torch
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
+
import story_generator # β
Correctly importing the Story Generator
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# β
Define the input request format
|
| 10 |
class StoryRequest(BaseModel):
|
| 11 |
theme: str
|
| 12 |
reading_level: str
|
| 13 |
|
| 14 |
+
# β
Load AI Image Model (Cartoon-Style)
|
| 15 |
CACHE_DIR = "/tmp/huggingface"
|
| 16 |
+
try:
|
| 17 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 18 |
+
"nitrosocke/Arcane-Diffusion",
|
| 19 |
+
cache_dir=CACHE_DIR,
|
| 20 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 21 |
+
)
|
| 22 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
+
print("β
Image generation model loaded successfully.")
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"β Failed to load image model: {e}")
|
| 26 |
+
pipe = None # Fallback if model fails
|
| 27 |
|
| 28 |
@app.post("/generate_story_and_image")
|
| 29 |
def generate_story_and_image(request: StoryRequest):
|
| 30 |
"""Generates a story and an image based on the given theme and reading level."""
|
| 31 |
+
try:
|
| 32 |
+
print(f"π Generating story for theme: {request.theme} and level: {request.reading_level}")
|
| 33 |
+
|
| 34 |
+
# β
Generate the story
|
| 35 |
+
if not hasattr(story_generator, "generate_story_and_questions"):
|
| 36 |
+
raise HTTPException(status_code=500, detail="Story generation function not found in story_generator.py")
|
| 37 |
+
|
| 38 |
+
story_result = story_generator.generate_story_and_questions(request.theme, request.reading_level)
|
| 39 |
+
story_text = story_result["story"]
|
| 40 |
+
|
| 41 |
+
# β
Generate the image
|
| 42 |
+
if not pipe:
|
| 43 |
+
raise HTTPException(status_code=500, detail="Image generation model failed to load.")
|
| 44 |
+
|
| 45 |
+
cartoon_prompt = f"A colorful, cartoon-style illustration of: {story_text}, vibrant colors, highly detailed, storybook fantasy."
|
| 46 |
+
print("ποΈ Generating Image for:", cartoon_prompt[:100])
|
| 47 |
+
|
| 48 |
+
image = pipe(cartoon_prompt, width=768, height=768).images[0] # β
Slightly smaller for faster generation
|
| 49 |
+
|
| 50 |
+
# β
Save image
|
| 51 |
+
image_path = "/tmp/generated_image.png"
|
| 52 |
+
image.save(image_path)
|
| 53 |
+
print("β
Image Saved!")
|
| 54 |
+
|
| 55 |
+
return {
|
| 56 |
+
"theme": request.theme,
|
| 57 |
+
"reading_level": request.reading_level,
|
| 58 |
+
"story": story_text,
|
| 59 |
+
"questions": story_result["questions"],
|
| 60 |
+
"image_url": f"https://your-api-url.com/generated_image.png" # Replace with actual host
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"β Error generating story and image: {e}")
|
| 65 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 66 |
+
|
| 67 |
+
# β
Welcome message at root
|
| 68 |
@app.get("/")
|
| 69 |
def home():
|
| 70 |
+
return {"message": "π Welcome to the Story & Image Generation API! Use /generate_story_and_image"}
|