Spaces:
Runtime error
Runtime error
Commit
·
6952db2
1
Parent(s):
70e81e1
fix app legacy
Browse files- app.py +21 -15
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,8 +1,7 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
-
from transformers import
|
3 |
import json
|
4 |
import os
|
5 |
-
from huggingface_hub import HfApi
|
6 |
|
7 |
app = FastAPI()
|
8 |
|
@@ -13,27 +12,32 @@ try:
|
|
13 |
except FileNotFoundError:
|
14 |
examples = []
|
15 |
|
16 |
-
# Função para
|
17 |
def load_model():
|
18 |
try:
|
19 |
-
|
20 |
-
"
|
21 |
-
|
22 |
-
|
23 |
-
device_map="auto" if os.getenv("HF_TOKEN") else None,
|
24 |
-
model_kwargs={"load_in_8bit": True if os.getenv("HF_TOKEN") else False},
|
25 |
-
trust_remote_code=True # Permite carregar código remoto, se necessário
|
26 |
)
|
|
|
|
|
|
|
|
|
|
|
27 |
except Exception as e:
|
28 |
print(f"Erro ao carregar o modelo: {e}")
|
29 |
return None
|
30 |
|
31 |
-
# Inicializa o modelo
|
32 |
-
|
33 |
|
34 |
def generate_question_from_prompt(theme, difficulty, example_question=None):
|
35 |
-
if not
|
36 |
-
return {"question": "Erro: Modelo não carregado.", "options": [], "answer": "", "explanation": "Por favor, verifique os logs."}
|
|
|
|
|
|
|
37 |
|
38 |
if example_question:
|
39 |
example_text = (
|
@@ -58,7 +62,9 @@ def generate_question_from_prompt(theme, difficulty, example_question=None):
|
|
58 |
"Explicação: [texto].'"
|
59 |
)
|
60 |
try:
|
61 |
-
|
|
|
|
|
62 |
# Parseia a resposta para extrair os componentes
|
63 |
parts = response.split("Alternativas:")
|
64 |
if len(parts) > 1:
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
3 |
import json
|
4 |
import os
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
|
|
12 |
except FileNotFoundError:
|
13 |
examples = []
|
14 |
|
15 |
+
# Função para carregar o modelo e tokenizer
|
16 |
def load_model():
|
17 |
try:
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
19 |
+
"unicamp-dl/ptt5-base-portuguese-vocab",
|
20 |
+
legacy=False, # Usa o novo comportamento do tokenizer
|
21 |
+
clean_up_tokenization_spaces=True # Define explicitamente para evitar warnings futuros
|
|
|
|
|
|
|
22 |
)
|
23 |
+
model = T5ForConditionalGeneration.from_pretrained(
|
24 |
+
"unicamp-dl/ptt5-base-portuguese-vocab",
|
25 |
+
device_map="auto" if os.getenv("HF_TOKEN") else None
|
26 |
+
)
|
27 |
+
return {"tokenizer": tokenizer, "model": model}
|
28 |
except Exception as e:
|
29 |
print(f"Erro ao carregar o modelo: {e}")
|
30 |
return None
|
31 |
|
32 |
+
# Inicializa o modelo e tokenizer
|
33 |
+
model_data = load_model()
|
34 |
|
35 |
def generate_question_from_prompt(theme, difficulty, example_question=None):
|
36 |
+
if not model_data or not model_data["tokenizer"] or not model_data["model"]:
|
37 |
+
return {"question": "Erro: Modelo ou tokenizer não carregado.", "options": [], "answer": "", "explanation": "Por favor, verifique os logs."}
|
38 |
+
|
39 |
+
tokenizer = model_data["tokenizer"]
|
40 |
+
model = model_data["model"]
|
41 |
|
42 |
if example_question:
|
43 |
example_text = (
|
|
|
62 |
"Explicação: [texto].'"
|
63 |
)
|
64 |
try:
|
65 |
+
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
66 |
+
outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.7, top_p=0.9)
|
67 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
68 |
# Parseia a resposta para extrair os componentes
|
69 |
parts = response.split("Alternativas:")
|
70 |
if len(parts) > 1:
|
requirements.txt
CHANGED
@@ -3,4 +3,4 @@ uvicorn==0.23.2
|
|
3 |
transformers==4.45.0
|
4 |
accelerate==0.21.0
|
5 |
huggingface_hub
|
6 |
-
sentencepiece
|
|
|
3 |
transformers==4.45.0
|
4 |
accelerate==0.21.0
|
5 |
huggingface_hub
|
6 |
+
sentencepiece
|