uii / app.py
0906harika's picture
Create app.py
2843264 verified
import torch
import pandas as pd
from transformers import BartTokenizer, BartForConditionalGeneration
import gradio as gr
# Initialize models and tokenizers for Healthcare and AI perspectives
healthcare_model_name = 'facebook/bart-large-cnn' # Healthcare-focused model
ai_model_name = 'facebook/bart-large-xsum' # AI-focused model
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 for both Healthcare and AI agents
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)
def healthcare_agent(abstract):
return generate_summary(abstract, healthcare_tokenizer, healthcare_model)
def ai_agent(abstract):
return generate_summary(abstract, ai_tokenizer, ai_model)
# Interaction 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."
# Combine both implications to provide a holistic view
combined_implications = f"{healthcare_implication}\n\n{ai_implication}"
return combined_implications
# Function to process the CSV and generate results
def process_and_generate_implications(csv_file):
# Read the input CSV file containing titles and abstracts
papers_df = pd.read_csv(csv_file.name, encoding='latin-1')
# Check if 'title' and 'abstract' columns exist
required_columns = ['title', 'abstract']
if not all(col.lower() in papers_df.columns.str.lower() for col in required_columns):
return "The CSV must contain 'title' and 'abstract' columns."
# Drop rows where title or abstract is missing
papers_df = papers_df.dropna(subset=['title', 'abstract'])
results = []
# Process each paper (row) in the CSV
for _, row in papers_df.iterrows():
title = row['title']
abstract = str(row['abstract'])
# Generate summaries using both agents
healthcare_summary = healthcare_agent(abstract)
ai_summary = ai_agent(abstract)
# Generate the implications based on both summaries
implications = generate_implications(healthcare_summary, ai_summary)
# Store the results
results.append({
"Title": title,
"Abstract": abstract,
"Healthcare Summary": healthcare_summary,
"AI Summary": ai_summary,
"Implications": implications
})
# Convert results into a DataFrame
results_df = pd.DataFrame(results)
# Return the results as a CSV string for download
return results_df.to_csv(index=False)
# Define Gradio interface
def create_interface():
with gr.Blocks() as demo:
gr.Markdown("## Research Paper Summarization and Implications")
gr.Markdown("Upload a CSV file with 'title' and 'abstract' columns to generate healthcare and AI implications.")
# Upload CSV file
csv_input = gr.File(label="Upload CSV File", type="file")
# Button to process the CSV and generate results
output_csv = gr.File(label="Download Results CSV")
# Process CSV and generate implications on button click
csv_input.change(process_and_generate_implications, inputs=csv_input, outputs=output_csv)
return demo
# Launch the interface
if __name__ == "__main__":
demo = create_interface()
demo.launch(debug=True) # Set debug=True to see detailed logs