DreamStream-1 commited on
Commit
33e434f
·
verified ·
1 Parent(s): 368fca9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -85,7 +85,7 @@ SERVER_URL = os.getenv("SERVER_URL", "https://your-huggingface-space-url.hf.spac
85
 
86
  # Initialize OpenAI client
87
  if OPENAI_API_KEY:
88
- client = openai.OpenAI(api_key=OPENAI_API_KEY)
89
  logger.info("✅ OpenAI client initialized successfully")
90
  else:
91
  logger.warning("⚠️ OpenAI API key not found - voice transcription will be disabled")
@@ -272,7 +272,7 @@ async def transcribe_voice_with_openai(file_path: str) -> str:
272
 
273
  # First attempt with English-specific prompt and language restriction
274
  with open(file_path, 'rb') as audio_file:
275
- transcript = client.audio.transcriptions.create(
276
  model="whisper-1",
277
  file=audio_file,
278
  language="en", # Force English first
@@ -287,7 +287,7 @@ async def transcribe_voice_with_openai(file_path: str) -> str:
287
  logger.warning(f"[Transcribe] First attempt failed, trying with Urdu-specific prompt")
288
 
289
  with open(file_path, 'rb') as audio_file:
290
- transcript = client.audio.transcriptions.create(
291
  model="whisper-1",
292
  file=audio_file,
293
  language="ur", # Force Urdu
@@ -302,7 +302,7 @@ async def transcribe_voice_with_openai(file_path: str) -> str:
302
  logger.warning(f"[Transcribe] Second attempt failed, trying with mixed language prompt")
303
 
304
  with open(file_path, 'rb') as audio_file:
305
- transcript = client.audio.transcriptions.create(
306
  model="whisper-1",
307
  file=audio_file,
308
  prompt="This is a voice message for a veterinary products bot. Language: English or Urdu only. Common words: hi, hello, salam, one, two, three, aik, do, teen, search, browse, download, catalog, products, categories, contact, availability, hydropex, heposel, bromacid, tribiotic, symodex, adek gold. Please transcribe any speech you can hear, even if unclear. Numbers and menu selections are important."
@@ -2059,14 +2059,14 @@ Please provide:
2059
  Format your response professionally with emojis and clear structure. Keep it concise but informative.
2060
  """
2061
 
2062
- response = client.chat.completions.create(
2063
  model="gpt-4o",
2064
  messages=[{"role": "user", "content": prompt}],
2065
  temperature=0.7,
2066
  max_tokens=400
2067
  )
2068
 
2069
- ai_response = response.choices[0].message.content.strip()
2070
 
2071
  # Add instructions for selection
2072
  selection_instructions = (
@@ -2242,14 +2242,14 @@ If the query is outside the menu system, politely clarify and offer to return to
2242
  Always keep responses professional, concise, and user-friendly.
2243
  """
2244
 
2245
- response = client.chat.completions.create(
2246
  model="gpt-4o",
2247
  messages=[{"role": "user", "content": prompt}],
2248
  temperature=0.7,
2249
  max_tokens=300
2250
  )
2251
 
2252
- ai_response = response.choices[0].message.content.strip()
2253
 
2254
  # Translate response if needed
2255
  if reply_language == 'ur':
@@ -2767,14 +2767,14 @@ Response:
2767
  """
2768
 
2769
  # Get AI response
2770
- response = client.chat.completions.create(
2771
  model="gpt-4o",
2772
  messages=[{"role": "user", "content": prompt}],
2773
  temperature=0.7,
2774
  max_tokens=500
2775
  )
2776
 
2777
- ai_response = response.choices[0].message.content.strip()
2778
 
2779
  # Add instructions for returning to main menu
2780
  if reply_language == 'ur':
 
85
 
86
  # Initialize OpenAI client
87
  if OPENAI_API_KEY:
88
+ openai.api_key = OPENAI_API_KEY
89
  logger.info("✅ OpenAI client initialized successfully")
90
  else:
91
  logger.warning("⚠️ OpenAI API key not found - voice transcription will be disabled")
 
272
 
273
  # First attempt with English-specific prompt and language restriction
274
  with open(file_path, 'rb') as audio_file:
275
+ transcript = openai.Audio.transcribe(
276
  model="whisper-1",
277
  file=audio_file,
278
  language="en", # Force English first
 
287
  logger.warning(f"[Transcribe] First attempt failed, trying with Urdu-specific prompt")
288
 
289
  with open(file_path, 'rb') as audio_file:
290
+ transcript = openai.Audio.transcribe(
291
  model="whisper-1",
292
  file=audio_file,
293
  language="ur", # Force Urdu
 
302
  logger.warning(f"[Transcribe] Second attempt failed, trying with mixed language prompt")
303
 
304
  with open(file_path, 'rb') as audio_file:
305
+ transcript = openai.Audio.transcribe(
306
  model="whisper-1",
307
  file=audio_file,
308
  prompt="This is a voice message for a veterinary products bot. Language: English or Urdu only. Common words: hi, hello, salam, one, two, three, aik, do, teen, search, browse, download, catalog, products, categories, contact, availability, hydropex, heposel, bromacid, tribiotic, symodex, adek gold. Please transcribe any speech you can hear, even if unclear. Numbers and menu selections are important."
 
2059
  Format your response professionally with emojis and clear structure. Keep it concise but informative.
2060
  """
2061
 
2062
+ response = openai.ChatCompletion.create(
2063
  model="gpt-4o",
2064
  messages=[{"role": "user", "content": prompt}],
2065
  temperature=0.7,
2066
  max_tokens=400
2067
  )
2068
 
2069
+ ai_response = response.choices[0].message['content'].strip()
2070
 
2071
  # Add instructions for selection
2072
  selection_instructions = (
 
2242
  Always keep responses professional, concise, and user-friendly.
2243
  """
2244
 
2245
+ response = openai.ChatCompletion.create(
2246
  model="gpt-4o",
2247
  messages=[{"role": "user", "content": prompt}],
2248
  temperature=0.7,
2249
  max_tokens=300
2250
  )
2251
 
2252
+ ai_response = response.choices[0].message['content'].strip()
2253
 
2254
  # Translate response if needed
2255
  if reply_language == 'ur':
 
2767
  """
2768
 
2769
  # Get AI response
2770
+ response = openai.ChatCompletion.create(
2771
  model="gpt-4o",
2772
  messages=[{"role": "user", "content": prompt}],
2773
  temperature=0.7,
2774
  max_tokens=500
2775
  )
2776
 
2777
+ ai_response = response.choices[0].message['content'].strip()
2778
 
2779
  # Add instructions for returning to main menu
2780
  if reply_language == 'ur':