Spaces:
Runtime error
Runtime error
File size: 2,492 Bytes
47d82ab 428a5aa 5537833 47d82ab 5537833 b9ceebf 47d82ab 5537833 428a5aa 92bb964 5537833 92bb964 428a5aa 5537833 428a5aa 5537833 5bf8ab4 5537833 47d82ab 95b13f9 47d82ab 5537833 af90128 5537833 a035375 5537833 5bf8ab4 5537833 47d82ab |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import os
import requests
import torch
from bs4 import BeautifulSoup
from peft import PeftConfig, PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
# os.environ["CUDA_VISIBLE_DEVICES"] = "0"
generation_config = GenerationConfig(temperature=.8,
top_p=0.75,
top_k=40)
def extract_text(url: str):
print(['extract_text', 'start'])
if url is None or url.strip() == '':
return ''
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
text = '\n\n'.join(map(lambda p: p.text, soup.find_all('p')))
print(['extract_text', 'end'])
return text
def summarize_text(text: str):
print(['summarize_text', 'start'])
input_text = f'<s>Instruction: Elabora un resume del siguiente texto.\nInput: {text}\nOutput: '
batch = tokenizer(input_text, return_tensors='pt')
batch = batch.to('cuda')
print(['summarize_text', 'generating'])
with torch.cuda.amp.autocast():
output_tokens = model.generate(**batch,
max_new_tokens=512,
generation_config=generation_config
)
output = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
output = output.replace(input_text, '')
print(['summarize_text', 'end'])
return output
def generate_question(text:str):
return 'Pregunta de ejemplo.'
def get_answer_context():
return 'Aquí está la respuesta.'
def answer_question(question:str):
return 'Esta es la respuesta a su pregunta.'
def load_model(peft_model_id):
print(['load_model', 'start'])
config = PeftConfig.from_pretrained(peft_model_id)
print(['load_model', 'loading model'])
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path,
return_dict=True,
load_in_8bit=True,
device_map='auto')
print(['load_model', 'loading tokenizer'])
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
model = PeftModel.from_pretrained(model, peft_model_id)
model.config.use_cache = True
print(['load_model', 'end'])
return model, tokenizer
model, tokenizer = load_model(
"hackathon-somos-nlp-2023/opt-6.7b-lora-sag-t3000-v300-v2")
|