Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,22 @@
|
|
1 |
-
from
|
2 |
import os
|
3 |
|
4 |
-
#
|
5 |
-
os.environ['
|
6 |
-
os.makedirs('/tmp/gguf_cache', exist_ok=True)
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
n_threads=4 # CPU threads
|
13 |
)
|
14 |
|
15 |
-
def generate_text(prompt,
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
)
|
21 |
-
return output[
|
|
|
1 |
+
from transformers import pipeline
|
2 |
import os
|
3 |
|
4 |
+
# Cache setup
|
5 |
+
os.environ['HF_HOME'] = '/tmp/cache'
|
|
|
6 |
|
7 |
+
model = pipeline(
|
8 |
+
"text-generation",
|
9 |
+
model="gpt2",
|
10 |
+
device=-1 # Force CPU
|
|
|
11 |
)
|
12 |
|
13 |
+
def generate_text(prompt, max_new_tokens=560, max_context=1080):
|
14 |
+
"""Generate text with precise token control"""
|
15 |
+
output = model(
|
16 |
+
prompt,
|
17 |
+
max_new_tokens=max_new_tokens, # Response tokens (560)
|
18 |
+
max_length=min(max_context, 1024), # GPT-2's max context is 1024
|
19 |
+
truncation=True,
|
20 |
+
pad_token_id=50256 # Explicitly set to avoid warnings
|
21 |
)
|
22 |
+
return output[0]["generated_text"]
|