Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,53 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
-
import uvicorn
|
| 4 |
import torch
|
| 5 |
from diffusers import StableDiffusionPipeline
|
| 6 |
import story_generator # Import Story Generator
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# β
Use a
|
| 11 |
-
CACHE_DIR = "/tmp/huggingface"
|
| 12 |
-
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 13 |
-
|
| 14 |
-
# β
Load Image Model (Arcane-Diffusion for Cartoon-Style Images)
|
| 15 |
-
print("Loading Cartoon Model...")
|
| 16 |
-
image_model_id = "nitrosocke/Arcane-Diffusion"
|
| 17 |
-
|
| 18 |
-
pipe = StableDiffusionPipeline.from_pretrained(
|
| 19 |
-
image_model_id,
|
| 20 |
-
cache_dir=CACHE_DIR, # β
Store model in `/tmp`
|
| 21 |
-
torch_dtype=torch_dtype
|
| 22 |
-
)
|
| 23 |
-
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
-
print("Image Model Loaded!")
|
| 25 |
-
|
| 26 |
-
# β
Define API Request Formats
|
| 27 |
class StoryRequest(BaseModel):
|
| 28 |
theme: str
|
| 29 |
reading_level: str
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return {
|
| 40 |
-
"theme": request.theme,
|
| 41 |
-
"reading_level": request.reading_level,
|
| 42 |
-
"story": result["story"],
|
| 43 |
-
"questions": result["questions"]
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
# β
Image Generation Endpoint
|
| 47 |
-
@app.post("/generate_image")
|
| 48 |
-
def generate_image(request: ImageRequest):
|
| 49 |
-
"""Generate a cartoon-style AI image based on the story text."""
|
| 50 |
-
cartoon_prompt = f"A colorful, cartoon-style illustration of: {request.story}, vibrant colors, highly detailed, storybook fantasy."
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
print("Generating Image for:", cartoon_prompt[:100])
|
| 53 |
-
|
| 54 |
-
# β
Generate Image
|
| 55 |
image = pipe(cartoon_prompt, width=1024, height=1024).images[0]
|
| 56 |
|
| 57 |
-
# β
Save
|
| 58 |
image_path = "generated_image.png"
|
| 59 |
image.save(image_path)
|
| 60 |
-
|
| 61 |
print("Image Saved!")
|
| 62 |
-
return {"message": "Image generated successfully!", "image_path": image_path}
|
| 63 |
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def home():
|
| 66 |
return {"message": "Welcome to the Story & Image Generation API! Use /generate_story_and_image"}
|
| 67 |
-
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
|
|
|
| 3 |
import torch
|
| 4 |
from diffusers import StableDiffusionPipeline
|
| 5 |
import story_generator # Import Story Generator
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
+
# β
Use a single request format
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
class StoryRequest(BaseModel):
|
| 11 |
theme: str
|
| 12 |
reading_level: str
|
| 13 |
|
| 14 |
+
# β
Load Image Model (Cartoon-Style)
|
| 15 |
+
CACHE_DIR = "/tmp/huggingface"
|
| 16 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 17 |
+
"nitrosocke/Arcane-Diffusion",
|
| 18 |
+
cache_dir=CACHE_DIR,
|
| 19 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 20 |
+
)
|
| 21 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
print(f"Generating story for theme: {request.theme} and level: {request.reading_level}")
|
| 27 |
+
|
| 28 |
+
# β
Generate story
|
| 29 |
+
story_result = story_generator.generate_story_and_questions(request.theme, request.reading_level)
|
| 30 |
+
story_text = story_result["story"]
|
| 31 |
+
|
| 32 |
+
# β
Generate image
|
| 33 |
+
cartoon_prompt = f"A colorful, cartoon-style illustration of: {story_text}, vibrant colors, highly detailed, storybook fantasy."
|
| 34 |
print("Generating Image for:", cartoon_prompt[:100])
|
|
|
|
|
|
|
| 35 |
image = pipe(cartoon_prompt, width=1024, height=1024).images[0]
|
| 36 |
|
| 37 |
+
# β
Save image
|
| 38 |
image_path = "generated_image.png"
|
| 39 |
image.save(image_path)
|
|
|
|
| 40 |
print("Image Saved!")
|
|
|
|
| 41 |
|
| 42 |
+
return {
|
| 43 |
+
"theme": request.theme,
|
| 44 |
+
"reading_level": request.reading_level,
|
| 45 |
+
"story": story_text,
|
| 46 |
+
"questions": story_result["questions"],
|
| 47 |
+
"image_path": image_path
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
# β
Fix the "Not Found" issue for the root URL
|
| 51 |
+
@app.get("/")
|
| 52 |
def home():
|
| 53 |
return {"message": "Welcome to the Story & Image Generation API! Use /generate_story_and_image"}
|
|
|