|
import fitz |
|
import torch |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
import gradio as gr |
|
|
|
def extract_text_from_pdf(file_path): |
|
doc = fitz.open(file_path) |
|
text = "" |
|
for page in doc: |
|
text += page.get_text() |
|
return text |
|
|
|
def analyze_document(file, prompt): |
|
|
|
if file.name.endswith('.pdf'): |
|
text = extract_text_from_pdf(file.name) |
|
elif file.name.endswith('.txt'): |
|
text = file.read().decode('utf-8') |
|
else: |
|
return "Unsupported file type. Please upload a PDF or TXT file." |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
tokenizer = AutoTokenizer.from_pretrained("THUDM/glm-4v-9b", trust_remote_code=True) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
"THUDM/glm-4v-9b", |
|
torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32, |
|
low_cpu_mem_usage=True, |
|
trust_remote_code=True |
|
).to(device).eval() |
|
|
|
|
|
inputs = tokenizer.apply_chat_template([{"role": "user", "content": text + "\n\n" + prompt}], |
|
add_generation_prompt=True, tokenize=True, return_tensors="pt", |
|
return_dict=True) |
|
inputs = inputs.to(device) |
|
|
|
|
|
gen_kwargs = {"max_length": 2500, "do_sample": True, "top_k": 1} |
|
with torch.no_grad(): |
|
outputs = model.generate(**inputs, **gen_kwargs) |
|
outputs = outputs[:, inputs['input_ids'].shape[1]:] |
|
return tokenizer.decode(outputs[0]) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_document, |
|
inputs=[ |
|
gr.inputs.File(label="Upload PDF or TXT Document"), |
|
gr.inputs.Textbox(label="Enter your prompt") |
|
], |
|
outputs="text", |
|
title="Medical Report Analyzer", |
|
description="Upload a medical report (PDF or TXT) and enter a prompt to analyze the report." |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|