Spaces:
Runtime error
Runtime error
Download utils.py from Inam65/AI_Story_Illustrator: direct link, hf CLI and curl.
- Browser
- Download file 1.68 kB
-
https://huggingface.co/spaces/Inam65/AI_Story_Illustrator/resolve/main/utils.py
- Command line
-
hf download hf://spaces/Inam65/AI_Story_Illustrator/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Inam65/AI_Story_Illustrator/resolve/main/utils.py
1.68 kB
| 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" | |