Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fitz # PyMuPDF
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
def extract_text_from_pdf(file_path):
|
7 |
+
doc = fitz.open(file_path)
|
8 |
+
text = ""
|
9 |
+
for page in doc:
|
10 |
+
text += page.get_text()
|
11 |
+
return text
|
12 |
+
|
13 |
+
def analyze_document(file, prompt):
|
14 |
+
# Determine file type and extract text
|
15 |
+
if file.name.endswith('.pdf'):
|
16 |
+
text = extract_text_from_pdf(file.name)
|
17 |
+
elif file.name.endswith('.txt'):
|
18 |
+
text = file.read().decode('utf-8')
|
19 |
+
else:
|
20 |
+
return "Unsupported file type. Please upload a PDF or TXT file."
|
21 |
+
|
22 |
+
# Load the model and tokenizer
|
23 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
+
tokenizer = AutoTokenizer.from_pretrained("THUDM/glm-4v-9b", trust_remote_code=True)
|
25 |
+
model = AutoModelForCausalLM.from_pretrained(
|
26 |
+
"THUDM/glm-4v-9b",
|
27 |
+
torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32,
|
28 |
+
low_cpu_mem_usage=True,
|
29 |
+
trust_remote_code=True
|
30 |
+
).to(device).eval()
|
31 |
+
|
32 |
+
# Prepare inputs
|
33 |
+
inputs = tokenizer.apply_chat_template([{"role": "user", "content": text + "\n\n" + prompt}],
|
34 |
+
add_generation_prompt=True, tokenize=True, return_tensors="pt",
|
35 |
+
return_dict=True)
|
36 |
+
inputs = inputs.to(device)
|
37 |
+
|
38 |
+
# Generate response
|
39 |
+
gen_kwargs = {"max_length": 2500, "do_sample": True, "top_k": 1}
|
40 |
+
with torch.no_grad():
|
41 |
+
outputs = model.generate(**inputs, **gen_kwargs)
|
42 |
+
outputs = outputs[:, inputs['input_ids'].shape[1]:]
|
43 |
+
return tokenizer.decode(outputs[0])
|
44 |
+
|
45 |
+
# Gradio interface
|
46 |
+
iface = gr.Interface(
|
47 |
+
fn=analyze_document,
|
48 |
+
inputs=[
|
49 |
+
gr.inputs.File(label="Upload PDF or TXT Document"),
|
50 |
+
gr.inputs.Textbox(label="Enter your prompt")
|
51 |
+
],
|
52 |
+
outputs="text",
|
53 |
+
title="Medical Report Analyzer",
|
54 |
+
description="Upload a medical report (PDF or TXT) and enter a prompt to analyze the report."
|
55 |
+
)
|
56 |
+
|
57 |
+
if __name__ == "__main__":
|
58 |
+
iface.launch()
|