Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,15 +7,32 @@ import torch
|
|
| 7 |
story_gen = pipeline("text-generation", model="OpenAssistant/reward-model-deberta-v3-large")
|
| 8 |
image_pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda")
|
| 9 |
|
| 10 |
-
# 2. Generate Story + Image
|
| 11 |
def generate(topic):
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
story = story_gen(story_prompt, max_length=300)[0]['generated_text']
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# 3. Build the Web Interface
|
| 21 |
with gr.Blocks() as app:
|
|
|
|
| 7 |
story_gen = pipeline("text-generation", model="OpenAssistant/reward-model-deberta-v3-large")
|
| 8 |
image_pipe = StableDiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16).to("cuda")
|
| 9 |
|
|
|
|
| 10 |
def generate(topic):
|
| 11 |
+
if not topic.strip(): # Check if input is empty
|
| 12 |
+
return "Please enter a valid topic (e.g., 'Ancient Rome' or 'Renewable Energy').", None
|
|
|
|
| 13 |
|
| 14 |
+
try:
|
| 15 |
+
# System Prompt
|
| 16 |
+
system_prompt = (
|
| 17 |
+
"You are an expert educational assistant. Your task is to create a 100-word story that is: "
|
| 18 |
+
"1. Engaging and age-appropriate for teenagers. "
|
| 19 |
+
"2. Factually accurate and aligned with the topic. "
|
| 20 |
+
"3. Free of inappropriate or harmful content. "
|
| 21 |
+
"4. Written in a clear and simple style. "
|
| 22 |
+
f"Write a story about: {topic}."
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Generate Story
|
| 26 |
+
story = story_gen(system_prompt, max_length=300)[0]['generated_text']
|
| 27 |
+
|
| 28 |
+
# Generate Image
|
| 29 |
+
image_prompt = f"Educational illustration about: {story}"
|
| 30 |
+
image = image_pipe(image_prompt).images[0]
|
| 31 |
+
|
| 32 |
+
return story, image
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Oops! Something went wrong. Please try again with a different topic.", None
|
| 36 |
|
| 37 |
# 3. Build the Web Interface
|
| 38 |
with gr.Blocks() as app:
|