Poojashetty357 commited on
Commit
d31e1fe
Β·
verified Β·
1 Parent(s): a6ea5eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -24
app.py CHANGED
@@ -1,25 +1,23 @@
1
- # πŸ“Œ Step 1: Install required packages
2
- #!pip install --upgrade openai gradio fpdf gTTS
3
-
4
- # πŸ“Œ Step 2: Import Libraries
5
  from dotenv import load_dotenv
6
  import os
7
  import gradio as gr
8
- from openai import OpenAI
9
  from fpdf import FPDF
10
  from datetime import datetime
11
  from gtts import gTTS
12
 
13
- # πŸ“Œ Step 3: Load environment variables from .env file
14
  load_dotenv()
15
  api_key = os.getenv("OPENAI_API_KEY")
16
- client = OpenAI(api_key=api_key)
 
 
 
17
 
18
- # πŸ“Œ Step 4: Directory to save stories
19
  story_dir = "storybot_stories"
20
  os.makedirs(story_dir, exist_ok=True)
21
 
22
- # πŸ“Œ Step 5: Save story to PDF
23
  def save_story_as_pdf(story_text, character_name=""):
24
  date_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
25
  file_name = f"{character_name or 'story'}_{date_str}.pdf"
@@ -45,14 +43,12 @@ def save_story_as_pdf(story_text, character_name=""):
45
  pdf.output(file_path)
46
  return file_path
47
 
48
- # πŸ“Œ Step 6: Convert story to speech
49
  def text_to_speech(text):
 
50
  tts = gTTS(text)
51
- audio_path = "/content/story_audio.mp3"
52
  tts.save(audio_path)
53
  return audio_path
54
 
55
- # πŸ“Œ Step 7: Generate story from LLM
56
  def generate_story(age_group, theme, character_name, tone, length):
57
  length_instruction = {
58
  "Short": "about 100 words",
@@ -72,23 +68,21 @@ Make the tone of the story {tone.lower()}. End it with the phrase "The End".
72
  ]
73
 
74
  try:
75
- response = client.chat.completions.create(
76
  model="gpt-3.5-turbo",
77
  messages=messages,
78
  temperature=0.7,
79
- max_tokens=1024,
80
- stop=None
81
  )
82
- print("LLM Response:", response) # Debug print
83
  story = response.choices[0].message.content
 
84
  except Exception as e:
85
- print("LLM ERROR:", e)
86
- story = f"An error occurred: {str(e)}"
87
 
88
  audio_path = text_to_speech(story)
89
- return story, story, audio_path
90
 
91
- # πŸ“Œ Step 8: Gradio Interface
92
  with gr.Blocks() as app:
93
  gr.Markdown("## πŸ“– Magic StoryBot for Kids")
94
 
@@ -103,12 +97,11 @@ with gr.Blocks() as app:
103
  pdf_output = gr.File(label="Download Your Story", file_types=[".pdf"], visible=True)
104
 
105
  def start_story(age, theme, name, tone, length):
106
- story, _, audio = generate_story(age, theme, name, tone, length)
107
  return story, audio
108
 
109
  def export_pdf(story, name):
110
- pdf_path = save_story_as_pdf(story, name)
111
- return pdf_path
112
 
113
  start_btn = gr.Button("🌟 Generate Story")
114
  save_btn = gr.Button("πŸ“₯ Save Story as PDF")
@@ -117,6 +110,8 @@ with gr.Blocks() as app:
117
  inputs=[age_group, theme, character_name, tone, length],
118
  outputs=[story_output, story_audio])
119
 
120
- save_btn.click(export_pdf, inputs=[story_output, character_name], outputs=pdf_output)
 
 
121
 
122
  app.launch()
 
 
 
 
 
1
  from dotenv import load_dotenv
2
  import os
3
  import gradio as gr
4
+ import openai
5
  from fpdf import FPDF
6
  from datetime import datetime
7
  from gtts import gTTS
8
 
9
+ # Load environment variables from .env
10
  load_dotenv()
11
  api_key = os.getenv("OPENAI_API_KEY")
12
+ if not api_key:
13
+ raise ValueError("❌ OPENAI_API_KEY not found in environment.")
14
+
15
+ openai.api_key = api_key
16
 
17
+ # Directory to save stories
18
  story_dir = "storybot_stories"
19
  os.makedirs(story_dir, exist_ok=True)
20
 
 
21
  def save_story_as_pdf(story_text, character_name=""):
22
  date_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
23
  file_name = f"{character_name or 'story'}_{date_str}.pdf"
 
43
  pdf.output(file_path)
44
  return file_path
45
 
 
46
  def text_to_speech(text):
47
+ audio_path = "story_audio.mp3"
48
  tts = gTTS(text)
 
49
  tts.save(audio_path)
50
  return audio_path
51
 
 
52
  def generate_story(age_group, theme, character_name, tone, length):
53
  length_instruction = {
54
  "Short": "about 100 words",
 
68
  ]
69
 
70
  try:
71
+ response = openai.ChatCompletion.create(
72
  model="gpt-3.5-turbo",
73
  messages=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
+ print("❌ LLM ERROR:", e)
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
 
 
97
  pdf_output = gr.File(label="Download Your Story", file_types=[".pdf"], visible=True)
98
 
99
  def start_story(age, theme, name, tone, length):
100
+ story, audio = generate_story(age, theme, name, tone, length)
101
  return story, audio
102
 
103
  def export_pdf(story, name):
104
+ return save_story_as_pdf(story, name)
 
105
 
106
  start_btn = gr.Button("🌟 Generate Story")
107
  save_btn = gr.Button("πŸ“₯ Save Story as PDF")
 
110
  inputs=[age_group, theme, character_name, tone, length],
111
  outputs=[story_output, story_audio])
112
 
113
+ save_btn.click(export_pdf,
114
+ inputs=[story_output, character_name],
115
+ outputs=pdf_output)
116
 
117
  app.launch()