Oviya commited on
Commit
28880f0
·
1 Parent(s): 80e5a3d
Files changed (1) hide show
  1. Findingword.py +12 -3
Findingword.py CHANGED
@@ -1,5 +1,5 @@
1
  import openai
2
- from flask import Flask, jsonify, request, send_from_directory, send_file, Blueprint
3
  import os
4
  from google.cloud import texttospeech
5
  from flask_cors import CORS
@@ -27,7 +27,15 @@ TRANSCRIPT_FOLDER = 'static/transcripts'
27
 
28
  # Set your OpenAI API key (left as-is per your request)
29
  # openai.api_key = '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'
30
- openai.api_key = os.getenv("OPENAI_API_KEY", "")
 
 
 
 
 
 
 
 
31
  # Initialize Google Cloud TTS client (local mode)
32
  client = texttospeech.TextToSpeechClient()
33
 
@@ -60,6 +68,7 @@ def get_vocabulary_word_from_openai():
60
  prompt = "Pick a simple vocabulary word suitable for children (ages 6–8) and provide its meaning in very easy English. Do not repeat words from previous responses. Format: 'Word: [word]. Meaning: [meaning].'"
61
 
62
  try:
 
63
  response = openai.chat.completions.create(
64
  model="gpt-3.5-turbo",
65
  messages=[
@@ -103,7 +112,7 @@ def get_vocabulary_word_from_openai():
103
 
104
  def generate_sentence(word, meaning):
105
  prompt = f"Create a sentence using the word '{word}' that fully demonstrates its meaning: {meaning}"
106
-
107
  response = openai.chat.completions.create(
108
  model="gpt-3.5-turbo",
109
  messages=[
 
1
  import openai
2
+ from flask import Flask, jsonify, request, send_from_directory, send_file, Blueprint, current_app
3
  import os
4
  from google.cloud import texttospeech
5
  from flask_cors import CORS
 
27
 
28
  # Set your OpenAI API key (left as-is per your request)
29
  # openai.api_key = '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'
30
+ # openai.api_key = os.getenv("OPENAI_API_KEY", "")
31
+ # --- OpenAI key handling (same as vocab builder) ---
32
+ _OPENAI_API_KEY_FALLBACK = os.getenv("OPENAI_API_KEY", "")
33
+
34
+ def _ensure_openai_key():
35
+ """Set openai.api_key from Flask config or env before each API call."""
36
+ api_key = (current_app.config.get("OPENAI_API_KEY") if current_app else None) or _OPENAI_API_KEY_FALLBACK
37
+ if api_key:
38
+ openai.api_key = api_key
39
  # Initialize Google Cloud TTS client (local mode)
40
  client = texttospeech.TextToSpeechClient()
41
 
 
68
  prompt = "Pick a simple vocabulary word suitable for children (ages 6–8) and provide its meaning in very easy English. Do not repeat words from previous responses. Format: 'Word: [word]. Meaning: [meaning].'"
69
 
70
  try:
71
+ _ensure_openai_key()
72
  response = openai.chat.completions.create(
73
  model="gpt-3.5-turbo",
74
  messages=[
 
112
 
113
  def generate_sentence(word, meaning):
114
  prompt = f"Create a sentence using the word '{word}' that fully demonstrates its meaning: {meaning}"
115
+ _ensure_openai_key()
116
  response = openai.chat.completions.create(
117
  model="gpt-3.5-turbo",
118
  messages=[