# from responses import start import gradio as gr import spaces import torch from transformers import AutoTokenizer, AutoModelForCausalLM model_id = "BSC-LT/salamandraTA-2b" # Load tokenizer and model tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto") # Move model to GPU if available languages = [ "Spanish", "Catalan", "English", "French", "German", "Italian", "Portuguese", "Euskera", "Galician", "Bulgarian", "Czech", "Lithuanian", "Croatian", "Dutch", "Romanian", "Danish", "Greek", "Finnish", "Hungarian", "Slovak", "Slovenian", "Estonian", "Polish", "Latvian", "Swedish", "Maltese", "Irish", "Aranese", "Aragonese", "Asturian" ] example_sentence = ["Ahir se'n va anar, va agafar les seves coses i es va posar a navegar."] @spaces.GPU(duration=120) def translate(input_text, source, target): sentences = input_text.split('\n') sentences = [ s for s in sentences if len(s) > 1 ] generated_text = [] for sentence in sentences: prompt = f'[{source}] {sentence} \n[{target}]' input_ids = tokenizer(prompt, return_tensors='pt').input_ids.to(model.device) output_ids = model.generate(input_ids, max_length=500, num_beams=5) input_length = input_ids.shape[1] generated_text.append(tokenizer.decode(output_ids[0, input_length:], skip_special_tokens=True).strip()) return '\n'.join(generated_text), "" with gr.Blocks() as demo: gr.HTML("""