Spaces:
Build error
Build error
| from model_flanT5 import summarize_flan_t5 | |
| from fallback import extract_top_keywords | |
| def generate_aggregated_summary(list_of_texts: list[str], engine: str = 'flan-t5') -> dict: | |
| """ | |
| Main handler function. | |
| 1. Aggregates a list of texts into one document. | |
| 2. Tries to summarize it using the specified engine ('gpt'). | |
| 3. If summarization fails, falls back to keyword extraction. | |
| Returns a dictionary with the result. | |
| """ | |
| # 1. Aggregate all text inputs into one large string | |
| if not list_of_texts: | |
| return {"status": "error", "message": "Input text list is empty."} | |
| full_text = "\n\n".join(list_of_texts) | |
| try: | |
| # 2. Try to generate the summary | |
| summary = "" | |
| print(f"Attempting summarization with {engine}...") | |
| if engine == 'flan-t5': | |
| summary = summarize_flan_t5(full_text) | |
| print("Summarization successful.") | |
| return {"status": "success", "summary": summary} | |
| except Exception as e: | |
| # 3. If ANY exception occurs, run the fallback | |
| print(f"Summarization failed with error: {e}. Falling back to keywords.") | |
| keywords = extract_top_keywords(full_text) | |
| return {"status": "fallback", "keywords": keywords} | |
| # --- EXAMPLE USAGE --- | |
| if __name__ == '__main__': | |
| # Imagine these texts are coming from your teammates' modules | |
| sample_texts = [ | |
| "The James Webb Space Telescope (JWST) has discovered the oldest galaxy yet, GLASS-z13, which existed just 300 million years after the Big Bang. This finding challenges our current understanding of early universe cosmology and galaxy formation.", | |
| "Data analysis from the JWST's infrared cameras confirms the galaxy's redshift. Scientists are now working to understand how such a massive structure could form so quickly in the cosmic timeline. The implications for dark matter models are significant.", | |
| "Future observations are planned to further investigate GLASS-z13 and search for even earlier stellar formations. The telescope's capabilities are pushing the boundaries of modern astrophysics." | |
| ] | |
| # --- Test 1: Successful summary with Flan-T5 --- | |
| result_flan = generate_aggregated_summary(sample_texts, engine='flan-t5') | |
| print("\n--- Flan-T5 Result ---") | |
| print(result_flan) | |
| # --- Test 3: Simulating a failure to trigger the fallback --- | |
| # We can simulate a failure by passing a bad engine name | |
| print("\n--- Fallback Test ---") | |
| result_fallback = generate_aggregated_summary(sample_texts, engine='bad_engine') | |
| print(result_fallback) |