File size: 2,624 Bytes
647a796 e213acb 647a796 a344135 647a796 a344135 647a796 a344135 647a796 a344135 647a796 52bab4e a344135 52bab4e a344135 647a796 52bab4e a344135 647a796 3fb9b0c 647a796 ba4cb28 1310cf1 79a19e9 3fb9b0c ba4cb28 1310cf1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import gradio as gr
from transformers import T5Tokenizer, T5ForConditionalGeneration
# Load the base T5 model and tokenizer
model = T5ForConditionalGeneration.from_pretrained('t5-small')
tokenizer = T5Tokenizer.from_pretrained('t5-small')
def generate_clinical_report(input_text, max_length=256, num_beams=4, no_repeat_ngram_size=3, length_penalty=2.0, early_stopping=True):
"""
Generate a clinical report from the input text using the T5 model with configurable parameters.
"""
try:
# Prepare input text
input_ids = tokenizer.encode("summarize: " + input_text, return_tensors="pt", max_length=512, truncation=True)
# Generate report with provided parameters
outputs = model.generate(
input_ids,
max_length=max_length,
num_beams=num_beams,
no_repeat_ngram_size=no_repeat_ngram_size,
length_penalty=length_penalty,
early_stopping=early_stopping,
bad_words_ids=[[tokenizer.encode(word, add_special_tokens=False)[0]]
for word in ['http', 'www', '.com', '.org']]
)
# Decode and return the generated report
return tokenizer.decode(outputs[0], skip_special_tokens=True)
except Exception as e:
print(f"Error generating report: {str(e)}")
return f"Error: {str(e)}"
# Create Gradio interface with API configuration
demo = gr.Interface(
fn=generate_clinical_report,
inputs=[
gr.Textbox(
lines=8,
placeholder="Enter clinical notes here...",
label="Clinical Notes"
),
gr.Slider(minimum=50, maximum=500, value=256, step=1, label="Max Length"),
gr.Slider(minimum=1, maximum=8, value=4, step=1, label="Num Beams"),
gr.Slider(minimum=1, maximum=5, value=3, step=1, label="No Repeat Ngram Size"),
gr.Slider(minimum=0.1, maximum=5.0, value=2.0, step=0.1, label="Length Penalty"),
gr.Checkbox(value=True, label="Early Stopping")
],
outputs=gr.Textbox(lines=8, label="Generated Clinical Report"),
title="Clinical Report Generator",
description="Generate professional clinical reports from clinical notes using a T5 model.",
allow_flagging="never",
analytics_enabled=False
)
# Launch the app with optimized configuration for Hugging Face Spaces
if __name__ == "__main__":
demo.queue() # Enable queue with default settings
demo.launch(
server_name="0.0.0.0",
share=True,
show_error=True,
max_threads=40 # Increase max threads for better performance
)
|