Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Initialize models and tokenizers for Healthcare and AI perspectives
|
| 7 |
+
healthcare_model_name = 'facebook/bart-large-cnn' # Healthcare-focused model
|
| 8 |
+
ai_model_name = 'facebook/bart-large-xsum' # AI-focused model
|
| 9 |
+
|
| 10 |
+
healthcare_tokenizer = BartTokenizer.from_pretrained(healthcare_model_name)
|
| 11 |
+
ai_tokenizer = BartTokenizer.from_pretrained(ai_model_name)
|
| 12 |
+
|
| 13 |
+
healthcare_model = BartForConditionalGeneration.from_pretrained(healthcare_model_name)
|
| 14 |
+
ai_model = BartForConditionalGeneration.from_pretrained(ai_model_name)
|
| 15 |
+
|
| 16 |
+
# Summarization function for both Healthcare and AI agents
|
| 17 |
+
def generate_summary(text, tokenizer, model):
|
| 18 |
+
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True, padding="max_length")
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model.generate(inputs["input_ids"], max_length=150, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
|
| 21 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 22 |
+
|
| 23 |
+
def healthcare_agent(abstract):
|
| 24 |
+
return generate_summary(abstract, healthcare_tokenizer, healthcare_model)
|
| 25 |
+
|
| 26 |
+
def ai_agent(abstract):
|
| 27 |
+
return generate_summary(abstract, ai_tokenizer, ai_model)
|
| 28 |
+
|
| 29 |
+
# Interaction function to generate implications based on both agents' insights
|
| 30 |
+
def generate_implications(healthcare_summary, ai_summary):
|
| 31 |
+
healthcare_implication = f"Healthcare Implications: {healthcare_summary} The healthcare sector can leverage these findings to improve patient care and treatment outcomes."
|
| 32 |
+
ai_implication = f"AI Implications: {ai_summary} These insights can further enhance AI models, making them more applicable in real-world healthcare scenarios."
|
| 33 |
+
|
| 34 |
+
# Combine both implications to provide a holistic view
|
| 35 |
+
combined_implications = f"{healthcare_implication}\n\n{ai_implication}"
|
| 36 |
+
return combined_implications
|
| 37 |
+
|
| 38 |
+
# Function to process the CSV and generate results
|
| 39 |
+
def process_and_generate_implications(csv_file):
|
| 40 |
+
# Read the input CSV file containing titles and abstracts
|
| 41 |
+
papers_df = pd.read_csv(csv_file.name, encoding='latin-1')
|
| 42 |
+
|
| 43 |
+
# Check if 'title' and 'abstract' columns exist
|
| 44 |
+
required_columns = ['title', 'abstract']
|
| 45 |
+
if not all(col.lower() in papers_df.columns.str.lower() for col in required_columns):
|
| 46 |
+
return "The CSV must contain 'title' and 'abstract' columns."
|
| 47 |
+
|
| 48 |
+
# Drop rows where title or abstract is missing
|
| 49 |
+
papers_df = papers_df.dropna(subset=['title', 'abstract'])
|
| 50 |
+
|
| 51 |
+
results = []
|
| 52 |
+
|
| 53 |
+
# Process each paper (row) in the CSV
|
| 54 |
+
for _, row in papers_df.iterrows():
|
| 55 |
+
title = row['title']
|
| 56 |
+
abstract = str(row['abstract'])
|
| 57 |
+
|
| 58 |
+
# Generate summaries using both agents
|
| 59 |
+
healthcare_summary = healthcare_agent(abstract)
|
| 60 |
+
ai_summary = ai_agent(abstract)
|
| 61 |
+
|
| 62 |
+
# Generate the implications based on both summaries
|
| 63 |
+
implications = generate_implications(healthcare_summary, ai_summary)
|
| 64 |
+
|
| 65 |
+
# Store the results
|
| 66 |
+
results.append({
|
| 67 |
+
"Title": title,
|
| 68 |
+
"Abstract": abstract,
|
| 69 |
+
"Healthcare Summary": healthcare_summary,
|
| 70 |
+
"AI Summary": ai_summary,
|
| 71 |
+
"Implications": implications
|
| 72 |
+
})
|
| 73 |
+
|
| 74 |
+
# Convert results into a DataFrame
|
| 75 |
+
results_df = pd.DataFrame(results)
|
| 76 |
+
|
| 77 |
+
# Return the results as a CSV string for download
|
| 78 |
+
return results_df.to_csv(index=False)
|
| 79 |
+
|
| 80 |
+
# Define Gradio interface
|
| 81 |
+
def create_interface():
|
| 82 |
+
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("## Research Paper Summarization and Implications")
|
| 84 |
+
gr.Markdown("Upload a CSV file with 'title' and 'abstract' columns to generate healthcare and AI implications.")
|
| 85 |
+
|
| 86 |
+
# Upload CSV file
|
| 87 |
+
csv_input = gr.File(label="Upload CSV File", type="file")
|
| 88 |
+
|
| 89 |
+
# Button to process the CSV and generate results
|
| 90 |
+
output_csv = gr.File(label="Download Results CSV")
|
| 91 |
+
|
| 92 |
+
# Process CSV and generate implications on button click
|
| 93 |
+
csv_input.change(process_and_generate_implications, inputs=csv_input, outputs=output_csv)
|
| 94 |
+
|
| 95 |
+
return demo
|
| 96 |
+
|
| 97 |
+
# Launch the interface
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
demo = create_interface()
|
| 100 |
+
demo.launch(debug=True) # Set debug=True to see detailed logs
|