Download utils.py from seberthe/BaraMusik: direct link, hf CLI and curl.
- Browser
- Download file 1.44 kB
-
https://huggingface.co/spaces/seberthe/BaraMusik/resolve/main/utils.py
- Command line
-
hf download hf://spaces/seberthe/BaraMusik/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/seberthe/BaraMusik/resolve/main/utils.py
1.44 kB
| import os | |
| from openai import OpenAI | |
| from googletrans import Translator, LANGUAGES, LANGCODES | |
| LANGUAGES['bm'] = 'bambara' | |
| LANGCODES = dict(map(reversed, LANGUAGES.items())) | |
| from dotenv import load_dotenv, find_dotenv | |
| _ = load_dotenv(find_dotenv()) # read local .env file | |
| # account for deprecation of LLM model | |
| import datetime | |
| # Get the current date | |
| current_date = datetime.datetime.now().date() | |
| # Define the date after which the model should be set to "gpt-3.5-turbo" | |
| target_date = datetime.date(2024, 6, 12) | |
| # Set the model variable based on the current date | |
| if current_date > target_date: | |
| llm_model = "gpt-3.5-turbo" | |
| else: | |
| llm_model = "gpt-3.5-turbo-0301" | |
| client = OpenAI(api_key=os.getenv('OPEN_AI_KEY')) | |
| translator = Translator() | |
| def ask_llm(prompt, model=llm_model): | |
| messages = [{"role": "user", "content": prompt}] | |
| response = client.chat.completions.create( | |
| model=model, | |
| messages=messages, | |
| temperature=0 | |
| ) | |
| # print(response) | |
| return response.choices[0].message.content | |
| def translate_to_english(text): | |
| result = translator.translate(text, dest='en') | |
| # print(f"Translation of ({text}) Result = ({result.text}) ------------> {result}") | |
| return result.text | |
| def translate_to_bambara(text): | |
| result = translator.translate(text, src='fr', dest='bm') # , src='bm' | |
| # print(f"Translation of ({text}) Result = ({result.text}) ------------> {result}") | |
| return result.text |