Ridealist commited on
Commit
3c1ac4e
1 Parent(s): da0298e

refactor: apply user's input OpenAI api-key in API call

Browse files
Files changed (2) hide show
  1. modules/gpt_modules.py +14 -10
  2. modules/whisper_modules.py +15 -12
modules/gpt_modules.py CHANGED
@@ -1,19 +1,21 @@
1
  import openai
2
  import streamlit as st
3
 
4
- from dotenv import dotenv_values
5
 
6
- config = dotenv_values(".env")
7
 
8
- if config:
9
- openai.organization = config.get('OPENAI_ORGANIZATION')
10
- openai.api_key = config.get('OPENAI_API_KEY')
11
- else:
12
- openai.organization = st.secrets['OPENAI_ORGANIZATION']
13
- openai.api_key = st.secrets['OPENAI_API_KEY']
14
 
15
 
16
- def gpt_call(prompt, role="user"):
 
 
17
  response = openai.ChatCompletion.create(
18
  model="gpt-3.5-turbo",
19
  messages=[
@@ -25,7 +27,9 @@ def gpt_call(prompt, role="user"):
25
  return output_text
26
 
27
 
28
- def gpt_call_context(messages):
 
 
29
  response = openai.ChatCompletion.create(
30
  model="gpt-3.5-turbo",
31
  messages=messages
 
1
  import openai
2
  import streamlit as st
3
 
4
+ # from dotenv import dotenv_values
5
 
6
+ # config = dotenv_values(".env")
7
 
8
+ # if config:
9
+ # openai.organization = config.get('OPENAI_ORGANIZATION')
10
+ # openai.api_key = config.get('OPENAI_API_KEY')
11
+ # else:
12
+ # openai.organization = st.secrets['OPENAI_ORGANIZATION']
13
+ # openai.api_key = st.secrets['OPENAI_API_KEY']
14
 
15
 
16
+ def gpt_call(api_key, prompt, role="user"):
17
+
18
+ openai.api_key = api_key
19
  response = openai.ChatCompletion.create(
20
  model="gpt-3.5-turbo",
21
  messages=[
 
27
  return output_text
28
 
29
 
30
+ def gpt_call_context(api_key, messages):
31
+
32
+ openai.api_key = api_key
33
  response = openai.ChatCompletion.create(
34
  model="gpt-3.5-turbo",
35
  messages=messages
modules/whisper_modules.py CHANGED
@@ -1,26 +1,28 @@
1
  import openai
2
  import os
3
  import random
4
- import streamlit as st
5
 
6
  from langchain.prompts import PromptTemplate
7
  from modules.gpt_modules import gpt_call
8
- from dotenv import dotenv_values
9
 
10
- config = dotenv_values(".env")
11
 
12
- if config:
13
- openai.organization = config.get('OPENAI_ORGANIZATION')
14
- openai.api_key = config.get('OPENAI_API_KEY')
15
- else:
16
- openai.organization = st.secrets['OPENAI_ORGANIZATION'] #config.get('OPENAI_ORGANIZATION')
17
- openai.api_key = st.secrets['OPENAI_API_KEY'] #config.get('OPENAI_API_KEY')
18
 
19
 
20
- def debate_in_sound(audio):
21
  os.rename(audio, audio + '.wav')
22
  file = open(audio + '.wav', "rb")
23
 
 
 
24
  # user_words
25
  user_prompt = openai.Audio.transcribe("whisper-1", file).text
26
 
@@ -59,12 +61,13 @@ def debate_in_sound(audio):
59
  bot_prompt = prompt_template.format(
60
  prompt=user_prompt
61
  )
62
- response = gpt_call(bot_prompt)
63
 
64
  return response
65
 
66
 
67
- def whisper_transcribe(audio):
 
68
 
69
  audio_file= open("audio/audio.wav", "rb")
70
  result = openai.Audio.transcribe("whisper-1", audio_file).text
 
1
  import openai
2
  import os
3
  import random
4
+ # import streamlit as st
5
 
6
  from langchain.prompts import PromptTemplate
7
  from modules.gpt_modules import gpt_call
8
+ # from dotenv import dotenv_values
9
 
10
+ # config = dotenv_values(".env")
11
 
12
+ # if config:
13
+ # openai.organization = config.get('OPENAI_ORGANIZATION')
14
+ # openai.api_key = config.get('OPENAI_API_KEY')
15
+ # else:
16
+ # openai.organization = st.secrets['OPENAI_ORGANIZATION'] #config.get('OPENAI_ORGANIZATION')
17
+ # openai.api_key = st.secrets['OPENAI_API_KEY'] #config.get('OPENAI_API_KEY')
18
 
19
 
20
+ def debate_in_sound(api_key, audio):
21
  os.rename(audio, audio + '.wav')
22
  file = open(audio + '.wav', "rb")
23
 
24
+ openai.api_key = api_key
25
+
26
  # user_words
27
  user_prompt = openai.Audio.transcribe("whisper-1", file).text
28
 
 
61
  bot_prompt = prompt_template.format(
62
  prompt=user_prompt
63
  )
64
+ response = gpt_call(api_key, bot_prompt)
65
 
66
  return response
67
 
68
 
69
+ def whisper_transcribe(api_key, audio_file):
70
+ openai.api_key = api_key
71
 
72
  audio_file= open("audio/audio.wav", "rb")
73
  result = openai.Audio.transcribe("whisper-1", audio_file).text