GH111 commited on
Commit
d9b5fef
·
1 Parent(s): 0c5599e

Create al.py

Browse files
Files changed (1) hide show
  1. al.py +75 -0
al.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import libraries
2
+ import gradio as gr
3
+ from transformers import pipeline
4
+ from gtts import gTTS
5
+ from io import BytesIO
6
+ from PIL import Image
7
+ from diffusers import DiffusionPipeline
8
+
9
+ # Use a pipeline as a high-level helper for text generation
10
+ vision_alpha_pipe = pipeline("text-generation", model="NousResearch/Nous-Hermes-2-Vision-Alpha")
11
+
12
+ # Initialize DiffusionPipeline for text-to-image
13
+ image_generation_pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-video-diffusion-img2vid-xt")
14
+
15
+ # Set the context for the storyteller
16
+ messages = [{"role": "system", "content": "You are a magical storyteller, creating wonderful tales for kids. Make them imaginative and full of joy!"}]
17
+
18
+ # Initialize page number
19
+ current_page = 0
20
+
21
+ # Define the Storyteller function
22
+ def StorytellerNous(character, child_name, lesson_choice, tell_story, _):
23
+ global current_page
24
+
25
+ # Set the characters and lesson based on user choices
26
+ character_info = f"Once upon a time, {child_name} met {character}. "
27
+ lesson_info = f"Today's lesson is about {lesson_choice}. "
28
+
29
+ messages.append({"role": "user", "content": tell_story})
30
+
31
+ # Generate story using Nous-Hermes-2-Vision-Alpha
32
+ input_text = character_info + lesson_info + tell_story
33
+ story_reply = vision_alpha_pipe(input_text, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, top_k=50, top_p=0.95)[0]['generated_text']
34
+
35
+ messages.append({"role": "assistant", "content": story_reply})
36
+
37
+ # Convert text to speech
38
+ tts = gTTS(text=story_reply, lang='en', slow=False)
39
+ audio_io = BytesIO()
40
+ tts.save(audio_io)
41
+ audio_io.seek(0)
42
+
43
+ # Convert text to image using DiffusionPipeline
44
+ image_reply = image_generation_pipe(story_reply)
45
+
46
+ # Display the story on separate pages
47
+ story_pages = story_reply.split("\n\n") # Split the story into pages
48
+ current_page = min(current_page, len(story_pages) - 1) # Ensure the current_page is within bounds
49
+
50
+ return story_pages[current_page], Audio(data=audio_io.read(), autoplay=True), image_reply
51
+
52
+ # Create the Gradio Interface with styling
53
+ demo = gr.Interface(
54
+ fn=StorytellerNous,
55
+ inputs=[
56
+ gr.Textbox("text", label="Child's Name"),
57
+ gr.Dropdown(["unicorn", "dragon", "wizard"], label="Choose a Character"),
58
+ gr.Dropdown(["kindness", "creativity", "bravery"], label="Choose a Lesson"),
59
+ gr.Textbox("text", label="Start the Story with"),
60
+ gr.Button("Next Page"),
61
+ ],
62
+ outputs=["text", "audio", "image"],
63
+ title="📖 Storytelling Magic",
64
+ description="A magical storyteller app for kids! Choose characters, add your name, and select the lesson you want to learn.",
65
+ live=True, # Enable live updates for CSS changes
66
+ css=f"""body {{
67
+ background-image: url('https://www.bing.com/images/create/a-castle-ai-metaverse-style/1-657576205c7146f2b7f2f8d1c552810f?id=dZs6kpD2HfmH4eojx%2bHjdA%3d%3d&view=detailv2&idpp=genimg&FORM=GCRIDP&mode=overlay');
68
+ background-size: cover;
69
+ background-position: center;
70
+ font-family: 'Comic Sans MS', cursive, sans-serif; /* Optional: Change the font */
71
+ }}""",
72
+ )
73
+
74
+ # Launch the Gradio Interface
75
+ demo.launch()