import gradio as gr from transformers import T5ForConditionalGeneration, T5Tokenizer import pandas as pd # Load the T5 model and tokenizer model_name = "t5-small" model = T5ForConditionalGeneration.from_pretrained(model_name) tokenizer = T5Tokenizer.from_pretrained(model_name) # Sample DataFrame (You need to upload your dataset to Hugging Face Spaces) df = pd.read_csv("t_kjv.csv") # Replace "data.csv" with the path to your uploaded CSV file in your Space # Map book numbers to book names book_mapping = { 1: "Genesis", 2: "Exodus", 3: "Leviticus", 4: "Numbers", 5: "Deuteronomy", 6: "Joshua", 7: "Judges", 8: "Ruth", 9: "1 Samuel", 10: "2 Samuel", 11: "1 Kings", 12: "2 Kings", 13: "1 Chronicles", 14: "2 Chronicles", 15: "Ezra", 16: "Nehemiah", 17: "Esther", 18: "Job", 19: "Psalms", 20: "Proverbs", 21: "Ecclesiastes", 22: "Song of Solomon", 23: "Isaiah", 24: "Jeremiah", 25: "Lamentations", 26: "Ezekiel", 27: "Daniel", 28: "Hosea", 29: "Joel", 30: "Amos", 31: "Obadiah", 32: "Jonah", 33: "Micah", 34: "Nahum", 35: "Habakkuk", 36: "Zephaniah", 37: "Haggai", 38: "Zechariah", 39: "Malachi", 40: "Matthew", 41: "Mark", 42: "Luke", 43: "John", 44: "Acts", 45: "Romans", 46: "1 Corinthians", 47: "2 Corinthians", 48: "Galatians", 49: "Ephesians", 50: "Philippians", 51: "Colossians", 52: "1 Thessalonians", 53: "2 Thessalonians", 54: "1 Timothy", 55: "2 Timothy", 56: "Titus", 57: "Philemon", 58: "Hebrews", 59: "James", 60: "1 Peter", 61: "2 Peter", 62: "1 John", 63: "2 John", 64: "3 John", 65: "Jude", 66: "Revelation" } # Create a reverse mapping of book names to book numbers reverse_book_mapping = {v: k for k, v in book_mapping.items()} def summarize_bible(book_name, chapter_number, max_summary_length=500): # Get the corresponding book number from reverse_book_mapping book_number = reverse_book_mapping.get(book_name) if book_number is None: return "Invalid Book Name" # Filter the DataFrame based on the selected book and chapter chapter_text = df[(df["b"] == book_number) & (df["c"] == int(chapter_number))]["t"].values.tolist() # Generate summary using the T5 model with custom max_length summary = model.generate( tokenizer.encode("summarize: " + chapter_text[0], return_tensors="pt"), max_length=max_summary_length, num_return_sequences=1, ) # Decode and return the summary decoded_summary = tokenizer.decode(summary[0], skip_special_tokens=True) final_summary = f"Summary of {book_name}, Chapter {chapter_number}:\n{decoded_summary}" return final_summary # Create the input and output interfaces for Gradio input_interface = [ gr.inputs.Dropdown(list(book_mapping.values()), label="Select Book"), gr.inputs.Number(label="Chapter Number") ] output_interface = gr.outputs.Textbox(label="Chapter Summary") # Create the Gradio app gr.Interface(fn=summarize_bible, inputs=input_interface, outputs=output_interface, title="Bible Chapter Summarizer").launch()