Spaces:
Sleeping
Sleeping
Daryl Lim
commited on
Commit
•
c7ce343
1
Parent(s):
67b917d
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This module provides an interface for summarizing medical text using FALCONS.AI's medical_summarization model.
|
3 |
+
The interface allows users to enter text from a medical document.
|
4 |
+
The user will receive a generated summary of the medical text.
|
5 |
+
"""
|
6 |
+
|
7 |
+
import gradio as gr
|
8 |
+
import spaces
|
9 |
+
import torch
|
10 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
11 |
+
|
12 |
+
# Set device
|
13 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
+
|
15 |
+
# Load tokenizer with fast processing enabled
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
17 |
+
"Falconsai/medical_summarization",
|
18 |
+
use_fast=True
|
19 |
+
)
|
20 |
+
|
21 |
+
# Load model with bf16 for optimized memory usage
|
22 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(
|
23 |
+
"Falconsai/medical_summarization",
|
24 |
+
torch_dtype=torch.bfloat16
|
25 |
+
)
|
26 |
+
|
27 |
+
# Move model to device
|
28 |
+
model.to(DEVICE)
|
29 |
+
|
30 |
+
@spaces.GPU
|
31 |
+
def summarize(text):
|
32 |
+
"""
|
33 |
+
Generate a summary of the text from a medical document.
|
34 |
+
|
35 |
+
Args:
|
36 |
+
text (str): The text of a medical document.
|
37 |
+
|
38 |
+
Returns:
|
39 |
+
str: The generated summary of the medical text.
|
40 |
+
"""
|
41 |
+
# Tokenize the text for summarization
|
42 |
+
tokenized_input = tokenizer(
|
43 |
+
text,
|
44 |
+
return_tensors="pt"
|
45 |
+
).input_ids.to(DEVICE)
|
46 |
+
|
47 |
+
# Generate a summary prediction using the model
|
48 |
+
summary_ids = model.generate(
|
49 |
+
input_ids=tokenized_input,
|
50 |
+
max_new_tokens=500
|
51 |
+
)
|
52 |
+
|
53 |
+
# Decode the generated summary
|
54 |
+
summary = tokenizer.batch_decode(
|
55 |
+
summary_ids,
|
56 |
+
skip_special_tokens=True
|
57 |
+
)
|
58 |
+
|
59 |
+
return summary[0]
|
60 |
+
|
61 |
+
TITLE = "Medical Text Summarizer"
|
62 |
+
DESCRIPTION = """
|
63 |
+
Summarize medical text using FALCONS.AI's medical_summarization model.
|
64 |
+
"""
|
65 |
+
|
66 |
+
# Gradio components
|
67 |
+
input_text = gr.Textbox(
|
68 |
+
label="Medical document",
|
69 |
+
placeholder="Enter text here"
|
70 |
+
)
|
71 |
+
|
72 |
+
output_text = gr.Textbox(label="Summary")
|
73 |
+
|
74 |
+
# Define the Gradio interface
|
75 |
+
demo = gr.Interface(
|
76 |
+
fn=summarize,
|
77 |
+
inputs=[input_text],
|
78 |
+
outputs=[output_text],
|
79 |
+
title=TITLE,
|
80 |
+
description=DESCRIPTION
|
81 |
+
)
|
82 |
+
|
83 |
+
# Launch the Gradio interface
|
84 |
+
demo.launch()
|