adnaan05 commited on
Commit
4846288
·
verified ·
1 Parent(s): ffebf32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -11
app.py CHANGED
@@ -8,41 +8,32 @@ from gtts import gTTS
8
  import io
9
  from groq import Groq
10
 
11
- # Initialize the Groq client
12
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
13
 
14
- # Load the Whisper model
15
- model = whisper.load_model("base") # You can choose other models like "small", "medium", "large"
16
 
17
  def process_audio(file_path):
18
  try:
19
- # Load the audio file
20
  audio = whisper.load_audio(file_path)
21
 
22
- # Transcribe the audio using Whisper
23
  result = model.transcribe(audio)
24
  text = result["text"]
25
 
26
- # Generate a response using Groq
27
  chat_completion = client.chat.completions.create(
28
  messages=[{"role": "user", "content": text}],
29
- model="llama3-8b-8192", # Replace with the correct model if necessary
30
  )
31
 
32
- # Access the response using dot notation
33
  response_message = chat_completion.choices[0].message.content.strip()
34
 
35
- # Convert the response text to speech
36
  tts = gTTS(response_message)
37
  response_audio_io = io.BytesIO()
38
  tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
39
  response_audio_io.seek(0)
40
 
41
- # Save audio to a file to ensure it's generated correctly
42
  with open("response.mp3", "wb") as audio_file:
43
  audio_file.write(response_audio_io.getvalue())
44
 
45
- # Return the response text and the path to the saved audio file
46
  return response_message, "response.mp3"
47
 
48
  except Exception as e:
 
8
  import io
9
  from groq import Groq
10
 
 
11
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
12
 
13
+ model = whisper.load_model("base")
 
14
 
15
  def process_audio(file_path):
16
  try:
 
17
  audio = whisper.load_audio(file_path)
18
 
 
19
  result = model.transcribe(audio)
20
  text = result["text"]
21
 
 
22
  chat_completion = client.chat.completions.create(
23
  messages=[{"role": "user", "content": text}],
24
+ model="llama3-8b-8192",
25
  )
26
 
 
27
  response_message = chat_completion.choices[0].message.content.strip()
28
 
 
29
  tts = gTTS(response_message)
30
  response_audio_io = io.BytesIO()
31
  tts.write_to_fp(response_audio_io) # Save the audio to the BytesIO object
32
  response_audio_io.seek(0)
33
 
 
34
  with open("response.mp3", "wb") as audio_file:
35
  audio_file.write(response_audio_io.getvalue())
36
 
 
37
  return response_message, "response.mp3"
38
 
39
  except Exception as e: