Spaces:
Paused
Paused
Commit ·
f165e87
1
Parent(s): c9c4a1f
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
-
|
| 4 |
|
|
|
|
| 5 |
|
|
|
|
|
|
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("truongghieu/deci-finetuned", trust_remote_code=True)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained("truongghieu/deci-finetuned", trust_remote_code=True)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
generation_config = GenerationConfig(
|
| 11 |
penalty_alpha=0.6,
|
| 12 |
-
do_sample
|
| 13 |
top_k=5,
|
| 14 |
temperature=0.5,
|
| 15 |
repetition_penalty=1.2,
|
|
@@ -17,11 +22,10 @@ generation_config = GenerationConfig(
|
|
| 17 |
pad_token_id=tokenizer.eos_token_id
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
# Define a function that takes a text input and generates a text output
|
| 22 |
def generate_text(text):
|
| 23 |
input_text = text
|
| 24 |
-
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 25 |
output_ids = model.generate(input_ids, generation_config=generation_config)
|
| 26 |
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 27 |
return output_text
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
|
|
|
| 3 |
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Check if a GPU is available
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained("truongghieu/deci-finetuned", trust_remote_code=True)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained("truongghieu/deci-finetuned", trust_remote_code=True)
|
| 11 |
|
| 12 |
+
# Move the model to the GPU if available
|
| 13 |
+
model.to(device)
|
| 14 |
+
|
| 15 |
generation_config = GenerationConfig(
|
| 16 |
penalty_alpha=0.6,
|
| 17 |
+
do_sample=True,
|
| 18 |
top_k=5,
|
| 19 |
temperature=0.5,
|
| 20 |
repetition_penalty=1.2,
|
|
|
|
| 22 |
pad_token_id=tokenizer.eos_token_id
|
| 23 |
)
|
| 24 |
|
|
|
|
| 25 |
# Define a function that takes a text input and generates a text output
|
| 26 |
def generate_text(text):
|
| 27 |
input_text = text
|
| 28 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device) # Move input to the GPU
|
| 29 |
output_ids = model.generate(input_ids, generation_config=generation_config)
|
| 30 |
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 31 |
return output_text
|