import gradio as gr from transformers import T5Tokenizer, T5ForConditionalGeneration # Load the fine-tuned T5 model and tokenizer model_path = "C:/Users/admin/Desktop/my_fine_tuned_t5_small" model = T5ForConditionalGeneration.from_pretrained(model_path) tokenizer = T5Tokenizer.from_pretrained(model_path) def generate_clinical_report(input_text): """ Generate a clinical report from the input text using the T5 model. """ try: # Prepare input text input_ids = tokenizer.encode("summarize: " + input_text, return_tensors="pt", max_length=512, truncation=True) # Generate report outputs = model.generate( input_ids, max_length=256, num_beams=4, no_repeat_ngram_size=3, length_penalty=2.0, early_stopping=True, 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 demo = gr.Interface( fn=generate_clinical_report, inputs=gr.Textbox( lines=8, placeholder="Enter clinical notes here...", label="Clinical Notes" ), 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" ) # Launch the app if __name__ == "__main__": demo.queue() # Enable queuing for stability demo.launch( server_name="0.0.0.0", server_port=7860, share=False, root_path="", show_api=True, allowed_paths=None, # Allow all paths quiet=True, # Reduce logging noise api_open=True, # Explicitly enable API access cors_allowed_origins="*", # Allow cross-origin requests concurrency_count=1 # Enable queuing with concurrency )