Spaces:
Sleeping
Sleeping
Update utils/interpret_lab_pdf.py
Browse files- utils/interpret_lab_pdf.py +18 -7
utils/interpret_lab_pdf.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import fitz # PyMuPDF
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b")
|
|
|
|
| 5 |
|
| 6 |
def extract_text_from_pdf(pdf_path):
|
| 7 |
doc = fitz.open(pdf_path)
|
|
@@ -9,18 +11,27 @@ def extract_text_from_pdf(pdf_path):
|
|
| 9 |
for page in doc:
|
| 10 |
text += page.get_text()
|
| 11 |
return text
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def analyze_pdf(pdf_path):
|
| 14 |
text = extract_text_from_pdf(pdf_path)
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
"Eres un experto en cannabis medicinal. Analiza el siguiente certificado de an谩lisis "
|
| 19 |
"y brinda una interpretaci贸n clara sobre los efectos, usos potenciales y caracter铆sticas del strain basado en los terpenos y cannabinoides. "
|
| 20 |
-
"No enfatices pesticidas ni contaminantes.\
|
| 21 |
-
+ text +
|
| 22 |
-
"\\n\\nInterpretaci贸n:"
|
| 23 |
)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
return result[0]['generated_text'].split("Interpretaci贸n:")[-1].strip()
|
| 26 |
|
|
|
|
| 1 |
import fitz # PyMuPDF
|
| 2 |
from transformers import pipeline
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
|
| 5 |
generator = pipeline("text-generation", model="tiiuae/falcon-rw-1b")
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("tiiuae/falcon-rw-1b")
|
| 7 |
|
| 8 |
def extract_text_from_pdf(pdf_path):
|
| 9 |
doc = fitz.open(pdf_path)
|
|
|
|
| 11 |
for page in doc:
|
| 12 |
text += page.get_text()
|
| 13 |
return text
|
| 14 |
+
|
| 15 |
+
|
| 16 |
|
| 17 |
def analyze_pdf(pdf_path):
|
| 18 |
text = extract_text_from_pdf(pdf_path)
|
| 19 |
+
|
| 20 |
+
# Prompt fijo
|
| 21 |
+
prefix = (
|
| 22 |
"Eres un experto en cannabis medicinal. Analiza el siguiente certificado de an谩lisis "
|
| 23 |
"y brinda una interpretaci贸n clara sobre los efectos, usos potenciales y caracter铆sticas del strain basado en los terpenos y cannabinoides. "
|
| 24 |
+
"No enfatices pesticidas ni contaminantes.\n\n"
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
+
|
| 27 |
+
# Codificar prompt + texto completo
|
| 28 |
+
full_input = prefix + text
|
| 29 |
+
tokens = tokenizer(full_input, truncation=True, max_length=1024, return_tensors="pt")
|
| 30 |
+
|
| 31 |
+
# Decodificar tokens truncados
|
| 32 |
+
truncated_input = tokenizer.decode(tokens["input_ids"][0], skip_special_tokens=True)
|
| 33 |
+
|
| 34 |
+
# Ejecutar modelo con input seguro
|
| 35 |
+
result = generator(truncated_input, max_new_tokens=300, do_sample=True)
|
| 36 |
return result[0]['generated_text'].split("Interpretaci贸n:")[-1].strip()
|
| 37 |
|