|
import whisper |
|
from gtts import gTTS |
|
from pydub import AudioSegment |
|
|
|
import os |
|
from llamaapi import LlamaAPI |
|
from openai import OpenAI |
|
from loguru import logger |
|
|
|
|
|
llama = LlamaAPI("LL-AirERHEk0jLIE1yEPvMXeobNfLsqLWJWcxLRS53obrZ3XyqMTfZc4EAuOs7r3wso") |
|
|
|
api_key = "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" |
|
os.environ["OPENAI_API_KEY"] = api_key |
|
client = OpenAI() |
|
|
|
|
|
def convert_to_mp3(input_path, output_path): |
|
audio = AudioSegment.from_file(input_path) |
|
|
|
audio.export(output_path, format="mp3") |
|
print(f"Audio converted to MP3 and saved as: {output_path}") |
|
|
|
|
|
|
|
|
|
|
|
def transcribe_audio(audio_path): |
|
""" |
|
Transcribes audio to text using Whisper. |
|
|
|
Parameters: |
|
- audio_path (str): The path to the audio file. |
|
|
|
Returns: |
|
- str: The transcribed text. |
|
""" |
|
model = whisper.load_model("base") |
|
audio = whisper.load_audio(audio_path) |
|
audio = whisper.pad_or_trim(audio) |
|
mel = whisper.log_mel_spectrogram(audio).to(model.device) |
|
|
|
_, probs = model.detect_language(mel) |
|
print(f"Detected language: {max(probs, key=probs.get)}") |
|
|
|
options = whisper.DecodingOptions() |
|
result = model.decode(mel, options) |
|
logger.success(f'Detecting completed, result: {result.text}') |
|
return result.text |
|
|
|
|
|
def text_to_speech(text, filename="/content/response.mp3"): |
|
""" |
|
Converts text to speech using gTTS and saves the audio file. |
|
|
|
Parameters: |
|
- text (str): The text to convert to speech. |
|
- filename (str): The filename for the saved audio file. |
|
""" |
|
tts = gTTS(text=text, lang='en') |
|
tts.save(filename) |
|
|
|
|
|
|
|
|
|
def audio_chat_with_model(prompt, model_type="gpt-4"): |
|
|
|
if model_type == "gpt-4": |
|
try: |
|
chat_completion = client.chat.completions.create( |
|
model="gpt-4", |
|
messages=[ |
|
{"role": "system", "content": "Imagine you're a supportive language tutor engaging in a conversation with an ESL learner. " |
|
"Learner is interested in discussing a variety of topics to improve their English speaking skills. " |
|
"Your goal is to encourage learners to speak as much as possible, gently correct any mistakes, and provide constructive feedback." |
|
"Adapt your conversation to include vocabulary and grammatical structures relevant to the discussion, ensuring a rich, engaging dialogue. " |
|
"Correct errors in a positive manner and encourage the learner to ask questions and express opinions freely. " |
|
"Conclude with a summary of their strengths during the session and offer specific advice for continued language improvement."}, |
|
{"role": "user", "content": prompt}, |
|
] |
|
) |
|
return chat_completion.choices[0].message.content.strip() |
|
except Exception as e: |
|
return f"An error occurred with GPT-4: {e}" |