File size: 1,680 Bytes
c9802db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dc2fbc
 
c9802db
2dc2fbc
c9802db
2dc2fbc
 
 
 
c9802db
 
 
2dc2fbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c9802db
 
381e865
c9802db
 
381e865
 
 
c9802db
381e865
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import whisper
import openai
import os
import requests

model = whisper.load_model("base")

openai.api_key = os.getenv("OPENAI_API_KEY")
LEONARDO_API_KEY = os.getenv("LEONARDO_API_KEY")

def transcribe_audio(audio_path):
    result = model.transcribe(audio_path)
    return result['text']

def split_story_to_pages(story_text):
    api_key = os.getenv("GROQ_API_KEY")

    prompt = f"""
    Divide the following children's story into 4-6 illustrated pages.
    For each page, return a JSON list with:
    - "text": short narration for that page
    - "image_prompt": scene description for image generation

    Make it fun and visual.

    Story: {story_text}
    """

    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    data = {
        "messages": [
            {"role": "system", "content": "You are a helpful children's book assistant."},
            {"role": "user", "content": prompt}
        ],
        "model": "mixtral-8x7b-32768",  # Or try "llama3-70b-8192"
    }

    response = requests.post("https://api.groq.com/openai/v1/chat/completions",
                             headers=headers, json=data)

    output = response.json()
    story_data = output["choices"][0]["message"]["content"]
    return eval(story_data)

def generate_image(prompt):
    api_key = os.getenv("DEEPAI_API_KEY")

    response = requests.post(
        "https://api.deepai.org/api/text2img",
        data={'text': prompt},
        headers={'api-key': api_key}
    )

    if response.status_code == 200:
        return response.json().get('output_url')
    else:
        return "https://via.placeholder.com/512?text=Image+Error"