Spaces:
Runtime error
Runtime error
Soueid-ahmed
commited on
Commit
•
976d0c9
1
Parent(s):
ca3e490
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,31 @@
|
|
1 |
-
import
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer
|
2 |
|
3 |
+
def generate_text(prompt, model_name="gpt2-medium", max_length=100, num_return_sequences=1):
|
4 |
+
# Charger le modèle et le tokenizer
|
5 |
+
tokenizer = GPT2Tokenizer.from_pretrained('rombodawg/test_dataset_Codellama-3-8B')
|
6 |
+
model = GPT2LMHeadModel.from_pretrained('rombodawg/test_dataset_Codellama-3-8B')
|
7 |
+
|
8 |
+
# Encoder le prompt en tokens
|
9 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt")
|
10 |
+
|
11 |
+
# Générer du texte
|
12 |
+
output = model.generate(
|
13 |
+
input_ids,
|
14 |
+
max_length=max_length,
|
15 |
+
num_return_sequences=num_return_sequences,
|
16 |
+
no_repeat_ngram_size=2,
|
17 |
+
top_k=50,
|
18 |
+
top_p=0.95,
|
19 |
+
temperature=0.7,
|
20 |
+
pad_token_id=tokenizer.eos_token_id
|
21 |
+
)
|
22 |
+
|
23 |
+
# Décoder les résultats en texte
|
24 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
25 |
+
|
26 |
+
return generated_text
|
27 |
+
|
28 |
+
# Exemple d'utilisation
|
29 |
+
prompt = "Une fois dans une forêt sombre, un aventurier solitaire"
|
30 |
+
generated_text = generate_text(prompt)
|
31 |
+
print(generated_text)
|