mgbam commited on
Commit
5bc0a64
·
verified ·
1 Parent(s): 24dfa15

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +34 -0
utils.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import openai
4
+ from dotenv import load_dotenv
5
+
6
+ def init_openai():
7
+ load_dotenv()
8
+ openai.api_key = os.getenv("OPENAI_API_KEY")
9
+
10
+ async def transcribe_audio(file_path: str) -> str:
11
+ """
12
+ Transcribe an audio file using OpenAI Whisper API.
13
+ """
14
+ with open(file_path, "rb") as audio_file:
15
+ response = openai.Audio.transcribe(
16
+ model="whisper-1",
17
+ file=audio_file
18
+ )
19
+ return response.get("text", "")
20
+
21
+ async def chat_with_gpt(system_prompt: str, user_message: str) -> str:
22
+ """
23
+ Send a chat message to OpenAI and return the assistant reply.
24
+ """
25
+ messages = [
26
+ {"role": "system", "content": system_prompt},
27
+ {"role": "user", "content": user_message}
28
+ ]
29
+ response = openai.ChatCompletion.create(
30
+ model="gpt-4o-mini",
31
+ messages=messages,
32
+ temperature=0.7
33
+ )
34
+ return response.choices[0].message.content