Spaces:
Sleeping
Sleeping
File size: 6,878 Bytes
87727f0 b0359f9 87727f0 b0359f9 87727f0 b0359f9 |
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 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
import json
from openai import OpenAI
from pydantic import BaseModel
from typing import List
from image_generator import get_image
import gradio as gr
class StepByStepAIResponse(BaseModel):
title: str
story_segments: List[str]
image_prompts: List[str]
class GetTranslation(BaseModel):
translated_text: List[str]
def generate_story(k, prompt, api_key):
"""Generate a story with k segments and initial prompt"""
if api_key == "":
raise gr.Error("Please add your OpenAI API key ")
else:
try:
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=[
{
"role": "system",
"content": f"""
Your expertise lies in weaving captivating narratives for children, complemented by images that vividly bring each tale to life. Embark on a creative endeavor to construct a story segmented into {k} distinct chapters, each a cornerstone of an enchanting journey for a young audience.
The input prompt will be on Arabic, but the output must be in English.
**Task Overview**:
1. **Story Development**:
- Craft a narrative divided into {k} parts, with a strict 50-word limit for each.
- Start with an engaging introduction that lays the foundation for the adventure.
- Ensure each part naturally progresses from the previous, crafting a fluid story that escalates to an exhilarating climax.
- Wrap up the narrative with a gratifying conclusion that ties all story threads together.
- Keep character continuity intact across the story, with consistent presence from beginning to end.
- You must describe the characters in details in every image prompt.
- Use language and themes that are child-friendly, imbued with wonder, and easy to visualize.
- The story will talk about {prompt}
2. **Image Generation Instructions for Image Models**:
- For every story part, create a comprehensive prompt for generating an image that encapsulates the scene's essence. Each prompt should:
- Offer a detailed description of the scene, characters, and critical elements, providing enough specificity for the image model to create a consistent and coherent visual.
- Request the images be in an anime style to ensure visual consistency throughout.
- Given the image model's isolated processing, reintroduce characters, settings, and pivotal details in each prompt to maintain narrative and visual continuity.
- Focus on visual storytelling components that enhance the story segments, steering clear of direct text inclusion in the images.
**Key Points**:
- Due to the image model's lack of recall, stress the need for self-contained prompts that reintroduce crucial elements each time. This strategy guarantees that, although generated independently, each image mirrors a continuous and cohesive visual story.
Through your skill in melding textual and visual storytelling, you will breathe life into this magical tale, offering young readers a journey to remember through both prose and illustration.
""",
},
],
functions=[
{
"name": "get_story_segments_and_image_prompts",
"description": "Get user answer in series of segment and image prompts",
"parameters": StepByStepAIResponse.model_json_schema(),
}
],
function_call={
"name": "get_story_segments_and_image_prompts"
}, # Corrected to match the defined function name
temperature=1,
max_tokens=1000,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
output = json.loads(response.choices[0].message.function_call.arguments)
sbs = StepByStepAIResponse(**output)
return sbs
except Exception as error:
print(str(error))
raise gr.Error(
"An error occurred while generating the story. Please try again."
)
def get_Arabic_translation(story_segments, api_key):
if api_key == "":
raise gr.Error("Please add your OpenAI API key ")
else:
try:
client = OpenAI(api_key=api_key)
response = client.chat.completions.create(
model="gpt-4-turbo-preview",
messages=[
{
"role": "system",
"content": f"""
You are an expert translator of text from English to Arabic.
On the following, you can find the input text that you need to translate to Arabic:
{story_segments}
Translate it from English to Arabic.
""",
},
],
functions=[
{
"name": "translate_text_from_english_to_arabic",
"description": "Translate the text from English to Arabic.",
"parameters": GetTranslation.model_json_schema(),
}
],
function_call={
"name": "translate_text_from_english_to_arabic"
}, # Corrected to match the defined function name
temperature=1,
max_tokens=1000,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
output = json.loads(response.choices[0].message.function_call.arguments)
sbs = GetTranslation(**output)
return sbs
except Exception as error:
print(str(error))
raise gr.Error(
"An error occurred while translating the text. Please try again."
)
def get_text_image_pairs(k, prompt, api_key_openai, api_key_stability_ai):
describtion = generate_story(k, prompt, api_key_openai)
segements_translation = get_Arabic_translation(
describtion.story_segments, api_key_openai
)
images_names = [
get_image(itm, api_key_stability_ai) for itm in describtion.image_prompts
]
return (segements_translation.translated_text, images_names)
|