Spaces:
Runtime error
Runtime error
Commit
·
5019432
1
Parent(s):
9d17108
app logs
Browse files- app.py +8 -3
- requirements.txt +2 -1
app.py
CHANGED
@@ -8,6 +8,7 @@ import logging
|
|
8 |
import time
|
9 |
import gc
|
10 |
import re
|
|
|
11 |
|
12 |
# Configura logging
|
13 |
logging.basicConfig(level=logging.INFO)
|
@@ -73,6 +74,9 @@ def parse_model_output(response):
|
|
73 |
"explanation": explanation
|
74 |
}
|
75 |
logger.warning(f"Parsing falhou para: {response[:200]}")
|
|
|
|
|
|
|
76 |
return {"question": response[:200] if len(response) > 200 else response, "options": [], "answer": "", "explanation": "Erro no parsing ou formato inválido"}
|
77 |
|
78 |
def generate_question_from_prompt(theme, difficulty, example_question=None):
|
@@ -85,15 +89,16 @@ def generate_question_from_prompt(theme, difficulty, example_question=None):
|
|
85 |
tokenizer = model_data["tokenizer"]
|
86 |
model = model_data["model"]
|
87 |
logger.info(f"Gerando questão com tema: {theme}, dificuldade: {difficulty}")
|
|
|
88 |
|
89 |
if example_question:
|
90 |
example_text = example_question.get("question", "") + " " + ", ".join(example_question.get("options", []))
|
91 |
-
prompt = f"
|
92 |
else:
|
93 |
-
prompt = f"Gere uma questão curta sobre '{theme}', dificuldade '{difficulty}', estilo USP. Responda
|
94 |
try:
|
95 |
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
96 |
-
outputs = model.generate(**inputs, max_new_tokens=
|
97 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
98 |
logger.debug(f"Resposta bruta: {response}")
|
99 |
result = parse_model_output(response)
|
|
|
8 |
import time
|
9 |
import gc
|
10 |
import re
|
11 |
+
import psutil # Para monitorar uso de recursos
|
12 |
|
13 |
# Configura logging
|
14 |
logging.basicConfig(level=logging.INFO)
|
|
|
74 |
"explanation": explanation
|
75 |
}
|
76 |
logger.warning(f"Parsing falhou para: {response[:200]}")
|
77 |
+
# Fallback para tentar extrair algo útil
|
78 |
+
if "Enunciado clínico" in response:
|
79 |
+
return {"question": response[:200], "options": [], "answer": "", "explanation": "Formato parcial detectado"}
|
80 |
return {"question": response[:200] if len(response) > 200 else response, "options": [], "answer": "", "explanation": "Erro no parsing ou formato inválido"}
|
81 |
|
82 |
def generate_question_from_prompt(theme, difficulty, example_question=None):
|
|
|
89 |
tokenizer = model_data["tokenizer"]
|
90 |
model = model_data["model"]
|
91 |
logger.info(f"Gerando questão com tema: {theme}, dificuldade: {difficulty}")
|
92 |
+
logger.debug(f"Uso de CPU: {psutil.cpu_percent()}%, Memória: {psutil.virtual_memory().percent}%")
|
93 |
|
94 |
if example_question:
|
95 |
example_text = example_question.get("question", "") + " " + ", ".join(example_question.get("options", []))
|
96 |
+
prompt = f"Usando '{example_text[:100]}' como exemplo, gere uma NOVA questão curta sobre '{theme}', dificuldade '{difficulty}', estilo USP. Responda SOMENTE: 'Enunciado clínico: [texto]. Alternativas: A) [opção], B) [opção], C) [opção], D) [opção]. Gabarito: [letra]. Explicação: [texto].'"
|
97 |
else:
|
98 |
+
prompt = f"Gere uma NOVA questão curta sobre '{theme}', dificuldade '{difficulty}', estilo USP. Responda SOMENTE: 'Enunciado clínico: [texto]. Alternativas: A) [opção], B) [opção], C) [opção], D) [opção]. Gabarito: [letra]. Explicação: [texto].'"
|
99 |
try:
|
100 |
inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
101 |
+
outputs = model.generate(**inputs, max_new_tokens=512, do_sample=True, temperature=0.7, top_p=0.9)
|
102 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
103 |
logger.debug(f"Resposta bruta: {response}")
|
104 |
result = parse_model_output(response)
|
requirements.txt
CHANGED
@@ -4,4 +4,5 @@ transformers==4.45.0
|
|
4 |
accelerate==0.21.0
|
5 |
huggingface_hub
|
6 |
sentencepiece
|
7 |
-
torch
|
|
|
|
4 |
accelerate==0.21.0
|
5 |
huggingface_hub
|
6 |
sentencepiece
|
7 |
+
torch
|
8 |
+
psutil
|