Update README.md
Browse files
README.md
CHANGED
@@ -9,6 +9,7 @@ metrics:
|
|
9 |
- rouge
|
10 |
library_name: transformers
|
11 |
pipeline_tag: text-generation
|
|
|
12 |
tags:
|
13 |
- clickbait
|
14 |
- noticia
|
@@ -48,7 +49,6 @@ widget:
|
|
48 |
la señora ha encantado a los usuarios de la red. Es por eso que el
|
49 |
relato ha acumulado más de 1.000 me gusta y cerca de 100 retuits,
|
50 |
además de una multitud de comentarios.\\n"
|
51 |
-
|
52 |
---
|
53 |
|
54 |
<table>
|
@@ -69,11 +69,19 @@ If you are looking for a larger model, with better performance, check out [Click
|
|
69 |
|
70 |
|
71 |
# Usage example:
|
|
|
|
|
72 |
```python
|
73 |
import torch # pip install torch
|
74 |
-
from
|
75 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig # pip install transformers
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
def prompt(
|
79 |
headline: str,
|
@@ -107,10 +115,81 @@ def prompt(
|
|
107 |
f"{body}\n"
|
108 |
)
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
dataset = load_dataset("Iker/NoticIA")
|
111 |
example = dataset["test"][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
-
prompt = prompt(headline=
|
114 |
|
115 |
tokenizer = AutoTokenizer.from_pretrained("Iker/ClickbaitFighter-2B")
|
116 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
9 |
- rouge
|
10 |
library_name: transformers
|
11 |
pipeline_tag: text-generation
|
12 |
+
base_model: google/gemma-2b-it
|
13 |
tags:
|
14 |
- clickbait
|
15 |
- noticia
|
|
|
49 |
la señora ha encantado a los usuarios de la red. Es por eso que el
|
50 |
relato ha acumulado más de 1.000 me gusta y cerca de 100 retuits,
|
51 |
además de una multitud de comentarios.\\n"
|
|
|
52 |
---
|
53 |
|
54 |
<table>
|
|
|
69 |
|
70 |
|
71 |
# Usage example:
|
72 |
+
|
73 |
+
## Summarize a web article
|
74 |
```python
|
75 |
import torch # pip install torch
|
76 |
+
from newspaper import Article #pip3 install newspaper3k
|
77 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig # pip install transformers
|
78 |
|
79 |
+
article_url ="https://www.huffingtonpost.es/virales/le-compra-abrigo-abuela-97nos-reaccion-fantasia.html"
|
80 |
+
article = Article(article_url)
|
81 |
+
article.download()
|
82 |
+
article.parse()
|
83 |
+
headline=article.title
|
84 |
+
body = article.text
|
85 |
|
86 |
def prompt(
|
87 |
headline: str,
|
|
|
115 |
f"{body}\n"
|
116 |
)
|
117 |
|
118 |
+
prompt = prompt(headline=headline, body=body)
|
119 |
+
|
120 |
+
tokenizer = AutoTokenizer.from_pretrained("Iker/ClickbaitFighter-2B")
|
121 |
+
model = AutoModelForCausalLM.from_pretrained(
|
122 |
+
"Iker/ClickbaitFighter-2B", torch_dtype=torch.bfloat16, device_map="auto"
|
123 |
+
)
|
124 |
+
|
125 |
+
formatted_prompt = tokenizer.apply_chat_template(
|
126 |
+
[{"role": "user", "content": prompt}],
|
127 |
+
tokenize=False,
|
128 |
+
add_generation_prompt=True,
|
129 |
+
)
|
130 |
+
|
131 |
+
model_inputs = tokenizer(
|
132 |
+
[formatted_prompt], return_tensors="pt", add_special_tokens=False
|
133 |
+
)
|
134 |
+
|
135 |
+
model_output = model.generate(**model_inputs.to(model.device), generation_config=GenerationConfig(
|
136 |
+
max_new_tokens=32,
|
137 |
+
min_new_tokens=1,
|
138 |
+
do_sample=False,
|
139 |
+
num_beams=1,
|
140 |
+
use_cache=True
|
141 |
+
))
|
142 |
+
|
143 |
+
summary = tokenizer.batch_decode(model_output,skip_special_tokens=True)[0]
|
144 |
+
|
145 |
+
print(summary.strip().split("\n")[-1]) # Get only the summary, without the prompt.
|
146 |
+
```
|
147 |
+
|
148 |
+
## Run inference in the NoticIA dataset
|
149 |
+
```python
|
150 |
+
import torch # pip install torch
|
151 |
+
from newspaper import Article #pip3 install newspaper3k
|
152 |
+
from datasets import load_dataset # pip install datasets
|
153 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig # pip install transformers
|
154 |
+
|
155 |
dataset = load_dataset("Iker/NoticIA")
|
156 |
example = dataset["test"][0]
|
157 |
+
headline = example["web_headline"]
|
158 |
+
body = example["web_text"]
|
159 |
+
|
160 |
+
def prompt(
|
161 |
+
headline: str,
|
162 |
+
body: str,
|
163 |
+
) -> str:
|
164 |
+
"""
|
165 |
+
Generate the prompt for the model.
|
166 |
+
|
167 |
+
Args:
|
168 |
+
headline (`str`):
|
169 |
+
The headline of the article.
|
170 |
+
body (`str`):
|
171 |
+
The body of the article.
|
172 |
+
Returns:
|
173 |
+
`str`: The formatted prompt.
|
174 |
+
"""
|
175 |
+
|
176 |
+
return (
|
177 |
+
f"Ahora eres una Inteligencia Artificial experta en desmontar titulares sensacionalistas o clickbait. "
|
178 |
+
f"Tu tarea consiste en analizar noticias con titulares sensacionalistas y "
|
179 |
+
f"generar un resumen de una sola frase que revele la verdad detrás del titular.\n"
|
180 |
+
f"Este es el titular de la noticia: {headline}\n"
|
181 |
+
f"El titular plantea una pregunta o proporciona información incompleta. "
|
182 |
+
f"Debes buscar en el cuerpo de la noticia una frase que responda lo que se sugiere en el título. "
|
183 |
+
f"Siempre que puedas cita el texto original, especialmente si se trata de una frase que alguien ha dicho. "
|
184 |
+
f"Si citas una frase que alguien ha dicho, usa comillas para indicar que es una cita. "
|
185 |
+
f"Usa siempre las mínimas palabras posibles. No es necesario que la respuesta sea una oración completa. "
|
186 |
+
f"Puede ser sólo el foco de la pregunta. "
|
187 |
+
f"Recuerda responder siempre en Español.\n"
|
188 |
+
f"Este es el cuerpo de la noticia:\n"
|
189 |
+
f"{body}\n"
|
190 |
+
)
|
191 |
|
192 |
+
prompt = prompt(headline=headline, body=body)
|
193 |
|
194 |
tokenizer = AutoTokenizer.from_pretrained("Iker/ClickbaitFighter-2B")
|
195 |
model = AutoModelForCausalLM.from_pretrained(
|