abdalraheemdmd commited on
Commit
8481c28
Β·
verified Β·
1 Parent(s): 019b8b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -45
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 writable cache directory for image generation models
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
- class ImageRequest(BaseModel):
32
- story: str
33
-
34
- # βœ… Story Generation Endpoint
35
- @app.post("/generate_story")
36
- def generate_story_endpoint(request: StoryRequest):
37
- """Generate a story based on theme and reading level."""
38
- result = story_generator.generate_story_and_questions(request.theme, request.reading_level)
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 Image
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
- @app.get("/")
 
 
 
 
 
 
 
 
 
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"}