Spaces:
Sleeping
Sleeping
Update evo_plugin_example.py
Browse files- evo_plugin_example.py +7 -31
evo_plugin_example.py
CHANGED
@@ -1,29 +1,12 @@
|
|
1 |
-
|
2 |
-
evo_plugin_example.py
|
3 |
-
Step 8: Example text generator plugin (for immediate testing).
|
4 |
-
(Objective)
|
5 |
-
- Provides the same interface your real Evo plugin will expose.
|
6 |
-
- Uses a tiny HF model (distilgpt2) so you can test generative mode now.
|
7 |
-
- Replace this later with `evo_plugin.py` wrapping your EvoDecoder/Evo QA.
|
8 |
-
|
9 |
-
Real Evo instructions:
|
10 |
-
- Create a file `evo_plugin.py` with a `load_model()` that returns an object
|
11 |
-
exposing `generate(prompt: str, max_new_tokens: int, temperature: float) -> str`.
|
12 |
-
- The app will auto-prefer `evo_plugin.py` if it exists.
|
13 |
-
"""
|
14 |
-
|
15 |
import torch
|
16 |
-
from transformers import AutoTokenizer,
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
def __init__(self, model_name: str = "distilgpt2"):
|
21 |
self.device = torch.device("cpu")
|
22 |
self.tok = AutoTokenizer.from_pretrained(model_name)
|
23 |
-
self.model =
|
24 |
-
# GPT-2 models have no pad token; set to eos to avoid warnings
|
25 |
-
if self.tok.pad_token_id is None and self.tok.eos_token_id is not None:
|
26 |
-
self.tok.pad_token_id = self.tok.eos_token_id
|
27 |
|
28 |
@torch.no_grad()
|
29 |
def generate(self, prompt: str, max_new_tokens: int = 200, temperature: float = 0.4) -> str:
|
@@ -34,15 +17,8 @@ class _HFGenerator:
|
|
34 |
do_sample=temperature > 0.0,
|
35 |
temperature=float(max(0.01, temperature)),
|
36 |
top_p=0.95,
|
37 |
-
pad_token_id=self.tok.pad_token_id,
|
38 |
)
|
39 |
-
|
40 |
-
# Return only the completion after the prompt to reduce echo
|
41 |
-
return text[len(prompt):].strip()
|
42 |
-
|
43 |
|
44 |
def load_model():
|
45 |
-
|
46 |
-
(Objective) Entry-point used by evo_inference.py.
|
47 |
-
"""
|
48 |
-
return _HFGenerator()
|
|
|
1 |
+
# evo_plugin_example.py — FLAN-T5 stand-in (better instruction following)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import torch
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
4 |
|
5 |
+
class _HFSeq2SeqGenerator:
|
6 |
+
def __init__(self, model_name: str = "google/flan-t5-small"):
|
|
|
7 |
self.device = torch.device("cpu")
|
8 |
self.tok = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained(model_name).to(self.device).eval()
|
|
|
|
|
|
|
10 |
|
11 |
@torch.no_grad()
|
12 |
def generate(self, prompt: str, max_new_tokens: int = 200, temperature: float = 0.4) -> str:
|
|
|
17 |
do_sample=temperature > 0.0,
|
18 |
temperature=float(max(0.01, temperature)),
|
19 |
top_p=0.95,
|
|
|
20 |
)
|
21 |
+
return self.tok.decode(out[0], skip_special_tokens=True).strip()
|
|
|
|
|
|
|
22 |
|
23 |
def load_model():
|
24 |
+
return _HFSeq2SeqGenerator()
|
|
|
|
|
|