Skip to content

Commit

Permalink
added new test files
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-pawelek committed Oct 4, 2024
1 parent 26eb4d9 commit bebd4a5
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "llmtranslate"
version = "0.3.7"
version = "0.3.8"
description = "A Python library for language detection and translation using OpenAI's GPT-4o."
authors = ["Adam Pawelek <[email protected]>"]
readme = "README.md"
Expand All @@ -18,6 +18,7 @@ pytest-asyncio = "^0.23.8"




[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
23 changes: 23 additions & 0 deletions tests/my_text_generation_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os

from openai import OpenAI

# init the client but point it to TGI
client = OpenAI(
base_url="https://api-inference.huggingface.co/models/microsoft/Phi-3.5-mini-instruct/v1",
api_key=os.environ.get("YOUR_HF_TOKEN"),
)

chat_completion = client.chat.completions.create(
model="microsoft/Phi-3.5-mini-instruct",
messages=[
{"role": "system",
"content": f"You are a language translator. You should translate text provided by user to the ISO 639-1: {'English'} language. You should return response in this JSON format: {{'translated_text': 'put here content of translated text'}} Don't write additional message like This is translated text just return json format"},
{"role": "user", "content": "Cześć jak się masz? Ja mam na imię Adam"}
]
)



print(chat_completion.choices[0].message.content)
# iterate and print stream
34 changes: 34 additions & 0 deletions tests/my_text_generation_interface_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import os

from huggingface_hub import InferenceClient

client = InferenceClient(api_key=os.getenv("YOUR_HF_TOKEN"))

schema = {
"properties": {
"location": {"title": "Location", "type": "string"},
"activity": {"title": "Activity", "type": "string"},
"animals_seen": {
"maximum": 5,
"minimum": 1,
"title": "Animals Seen",
"type": "integer",
},
"animals": {"items": {"type": "string"}, "title": "Animals", "type": "array"},
},
"required": ["location", "activity", "animals_seen", "animals"],
"title": "Animals",
"type": "object",
}

user_input = "I saw a puppy a cat and a raccoon during my bike ride in the park"
resp = client.text_generation(
f"convert to JSON: 'f{user_input}'. please use the following schema: {schema}",
model="meta-llama/Llama-3.2-1B-Instruct",
max_new_tokens=100,
seed=42,
grammar={"type": "json", "value": schema},
)

print(resp)
# { "activity": "bike ride", "animals": ["puppy", "cat", "raccoon"], "animals_seen": 3, "location": "park" }
20 changes: 20 additions & 0 deletions tests/open_source_v3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
from llmtranslate import TranslatorOpenSourceLLM

translator = TranslatorOpenSourceLLM(
api_key=os.environ.get("YOUR_HF_TOKEN"),
llm_endpoint="https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct/v1",
llm_model_name="meta-llama/Llama-3.2-1B-Instruct"
)

# Detect language
detected_language = translator.get_text_language("jak ty się nazywasz")
if detected_language is not None:
print(detected_language.ISO_639_1_code) # Output: 'pl'
print(detected_language.ISO_639_2_code) # Output: 'pol'
print(detected_language.ISO_639_3_code) # Output: 'pol'
print(detected_language.language_name) # Output 'Polish'

# Translate text
translated_text = translator.translate("Cześć jak się masz? Meu nome é Adam", "en")
print(translated_text) # Output: "Hello how are you? My name is Adam"
54 changes: 54 additions & 0 deletions tests/open_source_v4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
from llmtranslate import TranslatorOpenSourceLLM

translator = TranslatorOpenSourceLLM(
api_key=os.environ.get("YOUR_HF_TOKEN"),
llm_endpoint="https://api-inference.huggingface.co/models/mistralai/Mistral-Nemo-Instruct-2407/v1",
llm_model_name="mistralai/Mistral-Nemo-Instruct-2407"
)

# Detect language
detected_language = translator.get_text_language("jak ty się nazywasz")
if detected_language is not None:
print(detected_language.ISO_639_1_code) # Output: 'pl'
print(detected_language.ISO_639_2_code) # Output: 'pol'
print(detected_language.ISO_639_3_code) # Output: 'pol'
print(detected_language.language_name) # Output 'Polish'


text = """
Gospodarstwo
Powrót panicza — Spotkanie się pierwsze w pokoiku, drugie u stołu — Ważna Sędziego nauka o grzeczności — Podkomorzego uwagi polityczne nad modami — Początek sporu o Kusego i Sokoła — Żale Wojskiego — Ostatni Woźny Trybunału — Rzut oka na ówczesny stan polityczny Litwy i Europy
Litwo! Ojczyzno moja! ty jesteś jak zdrowie:
Ile cię trzeba cenić, ten tylko się dowie,
Kto cię stracił. Dziś piękność twą w całej ozdobie
Widzę i opisuję, bo tęsknię po tobie.
Panno święta, co Jasnej bronisz Częstochowy
I w Ostrej świecisz Bramie! Ty, co gród zamkowy
Nowogródzki ochraniasz z jego wiernym ludem!
Jak mnie dziecko do zdrowia powróciłaś cudem
(Gdy od płaczącej matki, pod Twoją opiekę
Ofiarowany, martwą podniosłem powiekę;
I zaraz mogłem pieszo, do Twych świątyń progu
Iść za wrócone życie podziękować Bogu),
Tak nas powrócisz cudem na Ojczyzny łono.
Tymczasem przenoś moją duszę utęsknioną
Do tych pagórków leśnych, do tych łąk zielonych,
Szeroko nad błękitnym Niemnem rozciągnionych;
Do tych pól malowanych zbożem rozmaitem,
Wyzłacanych pszenicą, posrebrzanych żytem;
Gdzie bursztynowy świerzop, gryka jak śnieg biała,
Gdzie panieńskim rumieńcem dzięcielina pała,
A wszystko przepasane jakby wstęgą, miedzą
Zieloną, na niej z rzadka ciche grusze siedzą.
Śród takich pól przed laty, nad brzegiem ruczaju
Hi how are you?
Zobaczymy czy teraz sobie z tym poradzisz
"""



# Translate text
translated_text = translator.translate(text, "en")
print(translated_text) # Output: "Hello how are you? My name is Adam"

0 comments on commit bebd4a5

Please sign in to comment.