Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1,190 +1,87 @@ | |
|  | |
| 1 | 
             
            import torch
         | 
| 2 | 
            -
            from  | 
| 3 | 
            -
            import  | 
| 4 | 
            -
            import  | 
| 5 | 
             
            import story_generator
         | 
|  | |
|  | |
|  | |
|  | |
| 6 |  | 
| 7 | 
            -
             | 
| 8 | 
            -
            CACHE_DIR = "/tmp/huggingface"
         | 
| 9 |  | 
| 10 | 
            -
            #  | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
            story_tokenizer = GPT2Tokenizer.from_pretrained(STORY_MODEL_NAME, cache_dir=CACHE_DIR)
         | 
| 15 | 
            -
            story_model = GPT2LMHeadModel.from_pretrained(STORY_MODEL_NAME, cache_dir=CACHE_DIR)
         | 
| 16 |  | 
| 17 | 
            -
            #  | 
| 18 | 
            -
             | 
| 19 | 
            -
            # ------------------------
         | 
| 20 | 
            -
            QUESTION_MODEL_NAME = "abdalraheemdmd/question-gene"
         | 
| 21 | 
            -
            question_tokenizer = GPT2Tokenizer.from_pretrained(QUESTION_MODEL_NAME, cache_dir=CACHE_DIR)
         | 
| 22 | 
            -
            question_model = GPT2LMHeadModel.from_pretrained(QUESTION_MODEL_NAME, cache_dir=CACHE_DIR)
         | 
| 23 |  | 
| 24 | 
            -
            #  | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
                 | 
|  | |
| 29 |  | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
                 | 
| 33 | 
            -
                 | 
| 34 | 
            -
                with torch.no_grad():
         | 
| 35 | 
            -
                    output = story_model.generate(
         | 
| 36 | 
            -
                        input_ids,
         | 
| 37 | 
            -
                        max_new_tokens=max_new_tokens,
         | 
| 38 | 
            -
                        temperature=temperature,
         | 
| 39 | 
            -
                        top_k=20,
         | 
| 40 | 
            -
                        top_p=0.7,
         | 
| 41 | 
            -
                        do_sample=True,
         | 
| 42 | 
            -
                        early_stopping=True,
         | 
| 43 | 
            -
                        pad_token_id=story_tokenizer.pad_token_id,
         | 
| 44 | 
            -
                        eos_token_id=story_tokenizer.eos_token_id,
         | 
| 45 | 
            -
                        attention_mask=input_ids.ne(story_tokenizer.pad_token_id)
         | 
| 46 | 
            -
                    )
         | 
| 47 | 
            -
                return story_tokenizer.decode(output[0], skip_special_tokens=True)
         | 
| 48 |  | 
| 49 | 
            -
             | 
| 50 | 
            -
             | 
| 51 | 
            -
                 | 
| 52 | 
            -
                 | 
| 53 | 
            -
             | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
                     | 
| 57 | 
            -
                     | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 66 | 
            -
             | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 74 | 
            -
                     | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 78 | 
            -
             | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 81 | 
            -
             | 
| 82 | 
            -
             | 
| 83 | 
            -
             | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
                     | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
                containing keywords like "learn" or "lesson". Returns the last matching sentence.
         | 
| 96 | 
            -
                """
         | 
| 97 | 
            -
                sentences = re.split(r'\.|\n', story)
         | 
| 98 | 
            -
                lesson_sentences = [
         | 
| 99 | 
            -
                    s.strip() for s in sentences
         | 
| 100 | 
            -
                    if ("learn" in s.lower() or "lesson" in s.lower()) and len(s.strip()) > 20
         | 
| 101 | 
            -
                ]
         | 
| 102 | 
            -
                if lesson_sentences:
         | 
| 103 | 
            -
                    return lesson_sentences[-1]
         | 
| 104 | 
            -
                else:
         | 
| 105 | 
            -
                    return "No explicit lesson found."
         | 
| 106 | 
            -
             | 
| 107 | 
            -
            def format_question(question_prompt, correct_answer, distractors):
         | 
| 108 | 
            -
                """
         | 
| 109 | 
            -
                Combines the correct answer with three distractors, shuffles the options,
         | 
| 110 | 
            -
                and formats the question as a multiple-choice question.
         | 
| 111 | 
            -
                """
         | 
| 112 | 
            -
                # Ensure exactly 3 distractors are available
         | 
| 113 | 
            -
                if len(distractors) < 3:
         | 
| 114 | 
            -
                    default_distractors = ["Option X", "Option Y", "Option Z"]
         | 
| 115 | 
            -
                    while len(distractors) < 3:
         | 
| 116 | 
            -
                        distractors.append(default_distractors[len(distractors) % len(default_distractors)])
         | 
| 117 | 
            -
                else:
         | 
| 118 | 
            -
                    distractors = random.sample(distractors, 3)
         | 
| 119 | 
            -
                options = distractors + [correct_answer]
         | 
| 120 | 
            -
                random.shuffle(options)
         | 
| 121 | 
            -
                letters = ["A", "B", "C", "D"]
         | 
| 122 | 
            -
                correct_letter = letters[options.index(correct_answer)]
         | 
| 123 | 
            -
                options_text = "\n".join(f"{letters[i]}) {option}" for i, option in enumerate(options))
         | 
| 124 | 
            -
                question_text = f"{question_prompt}\n{options_text}\nCorrect Answer: {correct_letter}"
         | 
| 125 | 
            -
                return question_text
         | 
| 126 | 
            -
             | 
| 127 | 
            -
            def dynamic_fallback_questions(story):
         | 
| 128 | 
            -
                """
         | 
| 129 | 
            -
                Generates three multiple-choice questions based on dynamic story content.
         | 
| 130 | 
            -
                Each question uses a randomly chosen template and shuffles its options.
         | 
| 131 | 
            -
                """
         | 
| 132 | 
            -
                protagonist = extract_protagonist(story)
         | 
| 133 | 
            -
                characters = extract_characters(story)
         | 
| 134 | 
            -
                themes = extract_themes(story)
         | 
| 135 | 
            -
                lesson = extract_lesson(story)
         | 
| 136 |  | 
| 137 | 
            -
                 | 
| 138 | 
            -
             | 
| 139 | 
            -
                     | 
| 140 | 
            -
                    "Which theme best represents the narrative?",
         | 
| 141 | 
            -
                    "What subject is central to the story?"
         | 
| 142 | 
            -
                ]
         | 
| 143 | 
            -
                q1_prompt = random.choice(theme_templates)
         | 
| 144 | 
            -
                correct_theme = " and ".join(themes) if themes else "learning"
         | 
| 145 | 
            -
                q1_distractors = ["sports and competition", "cooking and baking", "weather and seasons", "technology and innovation"]
         | 
| 146 | 
            -
                q1 = format_question(q1_prompt, correct_theme, q1_distractors)
         | 
| 147 | 
            -
                
         | 
| 148 | 
            -
                # --- Question 2: Primary Character ---
         | 
| 149 | 
            -
                character_templates = [
         | 
| 150 | 
            -
                    "Who is the primary character in the story?",
         | 
| 151 | 
            -
                    "Which character drives the main action in the narrative?",
         | 
| 152 | 
            -
                    "Who is the central figure in the story?"
         | 
| 153 | 
            -
                ]
         | 
| 154 | 
            -
                q2_prompt = random.choice(character_templates)
         | 
| 155 | 
            -
                if protagonist:
         | 
| 156 | 
            -
                    correct_character = protagonist
         | 
| 157 | 
            -
                elif characters:
         | 
| 158 | 
            -
                    correct_character = characters[0]
         | 
| 159 | 
            -
                else:
         | 
| 160 | 
            -
                    correct_character = "The main character"
         | 
| 161 | 
            -
                q2_distractors = ["a mysterious stranger", "an unknown visitor", "a supporting character", "a sidekick"]
         | 
| 162 | 
            -
                q2 = format_question(q2_prompt, correct_character, q2_distractors)
         | 
| 163 | 
            -
                
         | 
| 164 | 
            -
                # --- Question 3: Lesson/Moral ---
         | 
| 165 | 
            -
                lesson_templates = [
         | 
| 166 | 
            -
                    "What lesson did the characters learn by the end of the story?",
         | 
| 167 | 
            -
                    "What moral can be inferred from the narrative?",
         | 
| 168 | 
            -
                    "What is the key takeaway from the story?"
         | 
| 169 | 
            -
                ]
         | 
| 170 | 
            -
                q3_prompt = random.choice(lesson_templates)
         | 
| 171 | 
            -
                if lesson and lesson != "No explicit lesson found.":
         | 
| 172 | 
            -
                    correct_lesson = lesson  # full sentence without truncation
         | 
| 173 | 
            -
                else:
         | 
| 174 | 
            -
                    correct_lesson = "understanding and growth"
         | 
| 175 | 
            -
                q3_distractors = ["always be silent", "never try new things", "do nothing", "ignore opportunities"]
         | 
| 176 | 
            -
                q3 = format_question(q3_prompt, correct_lesson, q3_distractors)
         | 
| 177 | 
            -
                
         | 
| 178 | 
            -
                return f"{q1}\n\n{q2}\n\n{q3}"
         | 
| 179 | 
            -
             | 
| 180 | 
            -
            def generate_story_and_questions(theme, reading_level):
         | 
| 181 | 
            -
                """
         | 
| 182 | 
            -
                Generates a story using the story generation model and then creates dynamic,
         | 
| 183 | 
            -
                multiple-choice questions based on that story.
         | 
| 184 | 
            -
                """
         | 
| 185 | 
            -
                story = generate_story(theme, reading_level)
         | 
| 186 | 
            -
                questions = dynamic_fallback_questions(story)
         | 
| 187 | 
            -
                return {"story": story, "questions": questions}
         | 
| 188 |  | 
| 189 | 
            -
             | 
| 190 | 
            -
             | 
|  | 
|  | |
| 1 | 
            +
            import os
         | 
| 2 | 
             
            import torch
         | 
| 3 | 
            +
            from fastapi import FastAPI, HTTPException
         | 
| 4 | 
            +
            from fastapi.responses import JSONResponse
         | 
| 5 | 
            +
            from pydantic import BaseModel
         | 
| 6 | 
             
            import story_generator
         | 
| 7 | 
            +
            from diffusers import DiffusionPipeline
         | 
| 8 | 
            +
            from PIL import Image
         | 
| 9 | 
            +
            import io
         | 
| 10 | 
            +
            import base64
         | 
| 11 |  | 
| 12 | 
            +
            app = FastAPI()
         | 
|  | |
| 13 |  | 
| 14 | 
            +
            # Set Hugging Face cache directories
         | 
| 15 | 
            +
            os.environ["HF_HOME"] = "/tmp/huggingface"
         | 
| 16 | 
            +
            os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
         | 
| 17 | 
            +
            os.environ["HF_HUB_CACHE"] = "/tmp/huggingface"
         | 
|  | |
|  | |
| 18 |  | 
| 19 | 
            +
            # Enable GPU if available
         | 
| 20 | 
            +
            device = "cuda" if torch.cuda.is_available() else "cpu"
         | 
|  | |
|  | |
|  | |
|  | |
| 21 |  | 
| 22 | 
            +
            # Load image generation model
         | 
| 23 | 
            +
            IMAGE_MODEL = "lykon/dreamshaper-8"
         | 
| 24 | 
            +
            pipeline = DiffusionPipeline.from_pretrained(
         | 
| 25 | 
            +
                IMAGE_MODEL,
         | 
| 26 | 
            +
                torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
         | 
| 27 | 
            +
            ).to(device)
         | 
| 28 |  | 
| 29 | 
            +
            # Define request schema
         | 
| 30 | 
            +
            class StoryRequest(BaseModel):
         | 
| 31 | 
            +
                theme: str
         | 
| 32 | 
            +
                reading_level: str
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 33 |  | 
| 34 | 
            +
            @app.post("/generate_story_questions_images")
         | 
| 35 | 
            +
            def generate_story_questions_images(request: StoryRequest):
         | 
| 36 | 
            +
                """Generates a story, dynamic questions, and cartoonish storybook images."""
         | 
| 37 | 
            +
                try:
         | 
| 38 | 
            +
                    print(f"π Generating story for theme: {request.theme} and level: {request.reading_level}")
         | 
| 39 | 
            +
                    
         | 
| 40 | 
            +
                    # Generate story and questions
         | 
| 41 | 
            +
                    story_result = story_generator.generate_story_and_questions(request.theme, request.reading_level)
         | 
| 42 | 
            +
                    story_text = story_result.get("story", "").strip()
         | 
| 43 | 
            +
                    questions = story_result.get("questions", "").strip()
         | 
| 44 | 
            +
                    
         | 
| 45 | 
            +
                    if not story_text:
         | 
| 46 | 
            +
                        raise HTTPException(status_code=500, detail="Story generation failed.")
         | 
| 47 | 
            +
                    
         | 
| 48 | 
            +
                    # Split the story into up to 6 paragraphs
         | 
| 49 | 
            +
                    paragraphs = [p.strip() for p in story_text.split("\n") if p.strip()][:6]
         | 
| 50 | 
            +
                    
         | 
| 51 | 
            +
                    # Batch image generation:
         | 
| 52 | 
            +
                    prompts = [
         | 
| 53 | 
            +
                        (
         | 
| 54 | 
            +
                            f"Children's storybook illustration of: {p}. "
         | 
| 55 | 
            +
                            "Soft pastel colors, hand-drawn style, friendly characters, warm lighting, "
         | 
| 56 | 
            +
                            "fantasy setting, watercolor texture, storybook illustration, beautiful composition."
         | 
| 57 | 
            +
                        )
         | 
| 58 | 
            +
                        for p in paragraphs
         | 
| 59 | 
            +
                    ]
         | 
| 60 | 
            +
                    print(f"Generating images for {len(prompts)} paragraphs concurrently...")
         | 
| 61 | 
            +
                    
         | 
| 62 | 
            +
                    # Single batched call to generate images concurrently
         | 
| 63 | 
            +
                    results = pipeline(prompt=prompts, num_inference_steps=15, height=768, width=768).images
         | 
| 64 | 
            +
                    
         | 
| 65 | 
            +
                    images = []
         | 
| 66 | 
            +
                    for image in results:
         | 
| 67 | 
            +
                        img_byte_arr = io.BytesIO()
         | 
| 68 | 
            +
                        image.save(img_byte_arr, format="PNG")
         | 
| 69 | 
            +
                        img_byte_arr.seek(0)
         | 
| 70 | 
            +
                        base64_image = base64.b64encode(img_byte_arr.getvalue()).decode("utf-8")
         | 
| 71 | 
            +
                        images.append(base64_image)
         | 
| 72 | 
            +
                    
         | 
| 73 | 
            +
                    return JSONResponse(content={
         | 
| 74 | 
            +
                        "theme": request.theme,
         | 
| 75 | 
            +
                        "reading_level": request.reading_level,
         | 
| 76 | 
            +
                        "story": story_text,
         | 
| 77 | 
            +
                        "questions": questions,
         | 
| 78 | 
            +
                        "images": images
         | 
| 79 | 
            +
                    })
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 80 |  | 
| 81 | 
            +
                except Exception as e:
         | 
| 82 | 
            +
                    print(f"β Error generating story/questions/images: {e}")
         | 
| 83 | 
            +
                    raise HTTPException(status_code=500, detail=str(e))
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 84 |  | 
| 85 | 
            +
            @app.get("/")
         | 
| 86 | 
            +
            def home():
         | 
| 87 | 
            +
                return {"message": "π Welcome to the Story, Question & Image API!"}
         |