Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -50,6 +50,10 @@ def text_to_speech(text):
|
|
| 50 |
return audio_path
|
| 51 |
|
| 52 |
def generate_story(age_group, theme, character_name, tone, length):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
length_instruction = {
|
| 54 |
"Short": "about 100 words",
|
| 55 |
"Medium": "about 200 words",
|
|
@@ -62,27 +66,30 @@ Use the theme "{theme}" and the main character's name is "{character_name}".
|
|
| 62 |
Make the tone of the story {tone.lower()}. End it with the phrase "The End".
|
| 63 |
"""
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
{"role": "user", "content": "Please generate the story now."}
|
| 68 |
-
]
|
| 69 |
|
| 70 |
try:
|
| 71 |
-
response =
|
| 72 |
model="gpt-3.5-turbo",
|
| 73 |
-
messages=
|
|
|
|
|
|
|
|
|
|
| 74 |
temperature=0.7,
|
| 75 |
max_tokens=1024
|
| 76 |
)
|
| 77 |
story = response.choices[0].message.content
|
| 78 |
print("✅ Story generated")
|
| 79 |
except Exception as e:
|
| 80 |
-
|
|
|
|
| 81 |
story = "An error occurred while generating the story."
|
| 82 |
|
| 83 |
audio_path = text_to_speech(story)
|
| 84 |
return story, audio_path
|
| 85 |
|
|
|
|
| 86 |
with gr.Blocks() as app:
|
| 87 |
gr.Markdown("## 📖 Magic StoryBot for Kids")
|
| 88 |
|
|
|
|
| 50 |
return audio_path
|
| 51 |
|
| 52 |
def generate_story(age_group, theme, character_name, tone, length):
|
| 53 |
+
import openai
|
| 54 |
+
from openai import OpenAI
|
| 55 |
+
|
| 56 |
+
# Reconstruct the prompt
|
| 57 |
length_instruction = {
|
| 58 |
"Short": "about 100 words",
|
| 59 |
"Medium": "about 200 words",
|
|
|
|
| 66 |
Make the tone of the story {tone.lower()}. End it with the phrase "The End".
|
| 67 |
"""
|
| 68 |
|
| 69 |
+
# ⚠️ NEW SDK STYLE: OpenAI client
|
| 70 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
|
|
|
| 71 |
|
| 72 |
try:
|
| 73 |
+
response = client.chat.completions.create(
|
| 74 |
model="gpt-3.5-turbo",
|
| 75 |
+
messages=[
|
| 76 |
+
{"role": "system", "content": system_prompt},
|
| 77 |
+
{"role": "user", "content": "Please generate the story now."}
|
| 78 |
+
],
|
| 79 |
temperature=0.7,
|
| 80 |
max_tokens=1024
|
| 81 |
)
|
| 82 |
story = response.choices[0].message.content
|
| 83 |
print("✅ Story generated")
|
| 84 |
except Exception as e:
|
| 85 |
+
import traceback
|
| 86 |
+
print("❌ LLM ERROR:\n", traceback.format_exc())
|
| 87 |
story = "An error occurred while generating the story."
|
| 88 |
|
| 89 |
audio_path = text_to_speech(story)
|
| 90 |
return story, audio_path
|
| 91 |
|
| 92 |
+
|
| 93 |
with gr.Blocks() as app:
|
| 94 |
gr.Markdown("## 📖 Magic StoryBot for Kids")
|
| 95 |
|