Spaces:
Running
on
Zero
Running
on
Zero
Fix bug
Browse files
app.py
CHANGED
|
@@ -1,16 +1,72 @@
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from download_url import download_text_and_title
|
|
|
|
| 5 |
from cache_system import CacheHandler
|
| 6 |
-
from
|
|
|
|
|
|
|
| 7 |
|
| 8 |
print(f"CPU cores: {os.cpu_count()}.")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
def finish_generation(text: str) -> str:
|
|
@@ -41,40 +97,52 @@ def generate_text(
|
|
| 41 |
if title is None or text is None:
|
| 42 |
yield (
|
| 43 |
"🤖 No he podido acceder a la notica, asegurate que la URL es correcta y que es posible acceder a la noticia desde un navegador.",
|
| 44 |
-
"❌❌❌
|
| 45 |
"Error",
|
| 46 |
)
|
| 47 |
return (
|
| 48 |
"🤖 No he podido acceder a la notica, asegurate que la URL es correcta y que es posible acceder a la noticia desde un navegador.",
|
| 49 |
-
"❌❌❌
|
| 50 |
"Error",
|
| 51 |
)
|
| 52 |
|
| 53 |
progress(0.5, desc="🤖 Leyendo noticia")
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
)
|
| 73 |
-
return (
|
| 74 |
-
"🤖 El servidor no se encuentra disponible.",
|
| 75 |
-
"❌❌❌ Inténtalo de nuevo más tarde ❌❌❌",
|
| 76 |
-
"Error",
|
| 77 |
-
)
|
| 78 |
|
| 79 |
cache_handler.add_to_cache(
|
| 80 |
url=url, title=title, text=text, summary_type=mode, summary=temp
|
|
@@ -86,7 +154,7 @@ def generate_text(
|
|
| 86 |
|
| 87 |
|
| 88 |
cache_handler = CacheHandler(max_cache_size=1000)
|
| 89 |
-
|
| 90 |
|
| 91 |
demo = gr.Interface(
|
| 92 |
generate_text,
|
|
@@ -141,7 +209,7 @@ Para obtener solo la respuesta al clickbait, selecciona 100""",
|
|
| 141 |
|
| 142 |
🗒 La IA no es capaz de acceder a todas las webs, por ejemplo, si introduces un enlace a una noticia que requiere suscripción, la IA no podrá acceder a ella. Algunas webs pueden tener tecnologías para bloquear bots.
|
| 143 |
|
| 144 |
-
⌚ La IA se encuentra corriendo en un hardware bastante modesto,
|
| 145 |
|
| 146 |
💸 Este es un projecto sin ánimo de lucro, no se genera ningún tipo de ingreso con esta app. Los datos, la IA y el código se publicarán para su uso en la investigación académica. No puedes usar esta app para ningún uso comercial.
|
| 147 |
|
|
@@ -151,7 +219,7 @@ Para obtener solo la respuesta al clickbait, selecciona 100""",
|
|
| 151 |
concurrency_limit=1,
|
| 152 |
allow_flagging="manual",
|
| 153 |
flagging_options=[("👍", "correct"), ("👎", "incorrect")],
|
| 154 |
-
flagging_callback=
|
| 155 |
)
|
| 156 |
|
| 157 |
demo.queue(max_size=None)
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
import copy
|
| 5 |
+
from llama_cpp import Llama
|
| 6 |
+
|
| 7 |
+
# CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python --no-cache-dir
|
| 8 |
+
# CMAKE_ARGS="-DLLAMA_BLAS=ON -DLLAMA_BLAS_VENDOR=OpenBLAS" FORCE_CMAKE=1 pip install llama-cpp-python --no-cache-dir
|
| 9 |
+
|
| 10 |
+
import json
|
| 11 |
+
import datetime
|
| 12 |
+
from transformers import AutoTokenizer
|
| 13 |
from download_url import download_text_and_title
|
| 14 |
+
from prompts import clickbait_prompt, summary_prompt, clickbait_summary_prompt
|
| 15 |
from cache_system import CacheHandler
|
| 16 |
+
from huggingface_hub import hf_hub_download
|
| 17 |
+
|
| 18 |
+
auth_token = os.environ.get("TOKEN_FROM_SECRET") or True
|
| 19 |
|
| 20 |
print(f"CPU cores: {os.cpu_count()}.")
|
| 21 |
|
| 22 |
+
llm = Llama(
|
| 23 |
+
model_path=hf_hub_download(
|
| 24 |
+
repo_id=os.environ.get("REPO_ID", "Iker/ClickbaitFighter-10B"),
|
| 25 |
+
filename=os.environ.get("MODEL_FILE", "ClickbaitFighter-10B_q4_k_m.gguf"),
|
| 26 |
+
token=auth_token,
|
| 27 |
+
),
|
| 28 |
+
n_ctx=0,
|
| 29 |
+
n_gpu_layers=-1, # change n_gpu_layers if you have more or less VRAM
|
| 30 |
+
n_threads=8,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 34 |
+
"Iker/ClickbaitFighter-10B",
|
| 35 |
+
add_eos_token=True,
|
| 36 |
+
token=auth_token,
|
| 37 |
+
use_fast=True,
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
|
| 41 |
+
def generate_prompt(
|
| 42 |
+
tittle: str,
|
| 43 |
+
body: str,
|
| 44 |
+
mode: str = "finetune",
|
| 45 |
+
) -> str:
|
| 46 |
+
"""
|
| 47 |
+
Generate the prompt for the model.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
tittle (`str`):
|
| 51 |
+
The tittle of the article.
|
| 52 |
+
body (`str`):
|
| 53 |
+
The body of the article.
|
| 54 |
+
mode (`str`):
|
| 55 |
+
The mode of the model. Can be 'clickbait', 'summary' or 'clickbait-summary'.
|
| 56 |
+
Returns:
|
| 57 |
+
`str`: The formatted prompt.
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
if mode == "clickbait":
|
| 61 |
+
return clickbait_prompt(tittle, body)
|
| 62 |
+
elif mode == "summary":
|
| 63 |
+
return summary_prompt(tittle, body)
|
| 64 |
+
elif mode == "clickbait-summary":
|
| 65 |
+
return clickbait_summary_prompt(tittle, body)
|
| 66 |
+
else:
|
| 67 |
+
raise ValueError(
|
| 68 |
+
"Invalid mode. Valid modes are 'clickbait', 'summary' and 'clickbait-summary'"
|
| 69 |
+
)
|
| 70 |
|
| 71 |
|
| 72 |
def finish_generation(text: str) -> str:
|
|
|
|
| 97 |
if title is None or text is None:
|
| 98 |
yield (
|
| 99 |
"🤖 No he podido acceder a la notica, asegurate que la URL es correcta y que es posible acceder a la noticia desde un navegador.",
|
| 100 |
+
"❌❌❌",
|
| 101 |
"Error",
|
| 102 |
)
|
| 103 |
return (
|
| 104 |
"🤖 No he podido acceder a la notica, asegurate que la URL es correcta y que es posible acceder a la noticia desde un navegador.",
|
| 105 |
+
"❌❌❌",
|
| 106 |
"Error",
|
| 107 |
)
|
| 108 |
|
| 109 |
progress(0.5, desc="🤖 Leyendo noticia")
|
| 110 |
|
| 111 |
+
# 2) Generate the prompt
|
| 112 |
+
if mode == 0:
|
| 113 |
+
mo = "summary"
|
| 114 |
+
elif mode == 100:
|
| 115 |
+
mo = "clickbait"
|
| 116 |
+
else:
|
| 117 |
+
mo = "clickbait-summary"
|
| 118 |
+
input_prompt = generate_prompt(title, text, mo)
|
| 119 |
+
input_prompt = tokenizer.apply_chat_template(
|
| 120 |
+
[{"role": "user", "content": input_prompt}],
|
| 121 |
+
tokenize=False,
|
| 122 |
+
add_generation_prompt=True,
|
| 123 |
+
)
|
| 124 |
|
| 125 |
+
output = llm(
|
| 126 |
+
input_prompt,
|
| 127 |
+
temperature=0.15,
|
| 128 |
+
top_p=0.1,
|
| 129 |
+
top_k=40,
|
| 130 |
+
repeat_penalty=1.1,
|
| 131 |
+
max_tokens=256,
|
| 132 |
+
stop=[
|
| 133 |
+
"<s>" "</s>" "\n" "[/INST]" "[INST]",
|
| 134 |
+
"### User:",
|
| 135 |
+
"### Assistant:",
|
| 136 |
+
"###",
|
| 137 |
+
],
|
| 138 |
+
stream=True,
|
| 139 |
+
)
|
| 140 |
|
| 141 |
+
temp = ""
|
| 142 |
+
for out in output:
|
| 143 |
+
stream = copy.deepcopy(out)
|
| 144 |
+
temp += stream["choices"][0]["text"]
|
| 145 |
+
yield title, temp, text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
cache_handler.add_to_cache(
|
| 148 |
url=url, title=title, text=text, summary_type=mode, summary=temp
|
|
|
|
| 154 |
|
| 155 |
|
| 156 |
cache_handler = CacheHandler(max_cache_size=1000)
|
| 157 |
+
hf_writer = gr.HuggingFaceDatasetSaver(auth_token, "Iker/Clickbait-News")
|
| 158 |
|
| 159 |
demo = gr.Interface(
|
| 160 |
generate_text,
|
|
|
|
| 209 |
|
| 210 |
🗒 La IA no es capaz de acceder a todas las webs, por ejemplo, si introduces un enlace a una noticia que requiere suscripción, la IA no podrá acceder a ella. Algunas webs pueden tener tecnologías para bloquear bots.
|
| 211 |
|
| 212 |
+
⌚ La IA se encuentra corriendo en un hardware bastante modesto, por lo que puede tardar hasta un minuto en generar el resumen. Si muchos usuarios usan la app a la vez, tendrás que esperar tu turno.
|
| 213 |
|
| 214 |
💸 Este es un projecto sin ánimo de lucro, no se genera ningún tipo de ingreso con esta app. Los datos, la IA y el código se publicarán para su uso en la investigación académica. No puedes usar esta app para ningún uso comercial.
|
| 215 |
|
|
|
|
| 219 |
concurrency_limit=1,
|
| 220 |
allow_flagging="manual",
|
| 221 |
flagging_options=[("👍", "correct"), ("👎", "incorrect")],
|
| 222 |
+
flagging_callback=hf_writer,
|
| 223 |
)
|
| 224 |
|
| 225 |
demo.queue(max_size=None)
|