Spaces:
Running
on
Zero
Running
on
Zero
Fix bug
Browse files
app.py
CHANGED
|
@@ -1,72 +1,16 @@
|
|
| 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
|
| 17 |
-
|
| 18 |
-
auth_token = os.environ.get("TOKEN_FROM_SECRET") or True
|
| 19 |
|
| 20 |
print(f"CPU cores: {os.cpu_count()}.")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 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,52 +41,40 @@ def generate_text(
|
|
| 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 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
input_prompt = tokenizer.apply_chat_template(
|
| 120 |
-
[{"role": "user", "content": input_prompt}],
|
| 121 |
-
tokenize=False,
|
| 122 |
-
add_generation_prompt=True,
|
| 123 |
-
)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 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 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
cache_handler.add_to_cache(
|
| 148 |
url=url, title=title, text=text, summary_type=mode, summary=temp
|
|
@@ -209,7 +141,7 @@ Para obtener solo la respuesta al clickbait, selecciona 100""",
|
|
| 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,
|
| 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 |
|
|
|
|
| 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 gradio_client import Client
|
|
|
|
|
|
|
| 7 |
|
| 8 |
print(f"CPU cores: {os.cpu_count()}.")
|
| 9 |
|
| 10 |
+
server = os.environ.get("SERVER") or True
|
| 11 |
+
auth_token = os.environ.get("TOKEN") or True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
client = Client(server)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
def finish_generation(text: str) -> str:
|
|
|
|
| 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 |
+
"❌❌❌ Inténtalo de nuevo ❌❌❌",
|
| 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 |
+
"❌❌❌ Inténtalo de nuevo ❌❌❌",
|
| 50 |
"Error",
|
| 51 |
)
|
| 52 |
|
| 53 |
progress(0.5, desc="🤖 Leyendo noticia")
|
| 54 |
|
| 55 |
+
try:
|
| 56 |
+
temp = client.predict(
|
| 57 |
+
url, # str in '🌐 URL de la noticia' Textbox component
|
| 58 |
+
title, # str in '🌐 Título de la noticia' Textbox component
|
| 59 |
+
text, # str in '📰 Cuerpo de la noticia' Textbox component
|
| 60 |
+
mode, # float (numeric value between 0 and 100) in '🎚️ Nivel de resumen' Slider component
|
| 61 |
+
api_name="/predict",
|
| 62 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
for o in temp:
|
| 65 |
+
yield title, o, text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
except Exception as e:
|
| 68 |
+
yield (
|
| 69 |
+
"🤖 El servidor no se encuentra disponible.",
|
| 70 |
+
"❌❌❌ Inténtalo de nuevo más tarde ❌❌❌",
|
| 71 |
+
"Error",
|
| 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
|
|
|
|
| 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, debería tardar menos de 10 segundos en generar el resumen, pero si muchos usuarios usan la app a la vez, tendrás que esperar tu turno.
|
| 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 |
|