import gradio as gr import torch from transformers import BartTokenizer, BartForConditionalGeneration # Initialize tokenizers and models for both healthcare and AI healthcare_model_name = 'facebook/bart-large-cnn' ai_model_name = 'facebook/bart-large-xsum' healthcare_tokenizer = BartTokenizer.from_pretrained(healthcare_model_name) ai_tokenizer = BartTokenizer.from_pretrained(ai_model_name) healthcare_model = BartForConditionalGeneration.from_pretrained(healthcare_model_name) ai_model = BartForConditionalGeneration.from_pretrained(ai_model_name) # Summarization function def generate_summary(text, tokenizer, model): inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True, padding="max_length") with torch.no_grad(): outputs = model.generate(inputs["input_ids"], max_length=150, num_beams=5, no_repeat_ngram_size=2, early_stopping=True) return tokenizer.decode(outputs[0], skip_special_tokens=True) # Functions for each agent def healthcare_agent(abstract): return generate_summary(abstract, healthcare_tokenizer, healthcare_model) def ai_agent(abstract): return generate_summary(abstract, ai_tokenizer, ai_model) # Function to generate implications based on both agents' insights def generate_implications(healthcare_summary, ai_summary): healthcare_implication = f"Healthcare Implications: {healthcare_summary} The healthcare sector can leverage these findings to improve patient care and treatment outcomes." ai_implication = f"AI Implications: {ai_summary} These insights can further enhance AI models, making them more applicable in real-world healthcare scenarios." combined_implications = f"{healthcare_implication}\n\n{ai_implication}" return combined_implications # Gradio Interface function def summarize_and_generate_implications(abstract): healthcare_summary = healthcare_agent(abstract) ai_summary = ai_agent(abstract) implications = generate_implications(healthcare_summary, ai_summary) return healthcare_summary, ai_summary, implications # Creating the Gradio interface interface = gr.Interface( fn=summarize_and_generate_implications, inputs=gr.Textbox(label="Abstract", placeholder="Enter the abstract of a research paper..."), outputs=[ gr.Textbox(label="Healthcare Summary"), gr.Textbox(label="AI Summary"), gr.Textbox(label="Implications") ], live=True, title="Research Paper Summarization and Implications", description="This app generates summaries for healthcare and AI domains and provides implications for each." ) # Launch the Gradio interface interface.launch(share=True) # share=True will generate a public link