Update app.py
Browse files317 unmodified lines31831932032132232332432532632732832917 unmodified lines347348349350351352353100 unmodified lines4544554564574584594604614624634641 unmodified line466467468469470471472473474475476477478479480481482483484485486487488489490491492493317 unmodified lines"content": clean_content,"summary": full_summary,"cleaned_summary": cleaned_summary,"relevance_score": relevance_score # Ensure this line is present}processed_articles.append(processed_article)except Exception as e:print(f"Error processing article: {str(e)}")if not processed_articles:return f"Failed to process any news articles from {news_source}. Please try again or check the summarization process."17 unmodified lines# Update news_database for excel exportglobal news_databasenews_database.extend(processed_articles)return f"Processed and added {len(processed_articles)} news articles from {news_source} to the database."except Exception as e:100 unmodified linesdef export_news_to_excel():global news_databasedf = pd.DataFrame(news_database)# Use the cleaned summary for the Excel exportdf['summary'] = df['cleaned_summary']df = df.drop(columns=['cleaned_summary']) # Remove the extra column# Ensure relevance_score is present and convert to floatif 'relevance_score' not in df.columns:1 unmodified lineelse:df['relevance_score'] = pd.to_numeric(df['relevance_score'], errors='coerce').fillna(0.0)# Reorder columns to put relevance_score after summarycolumns = ['published_date', 'title', 'url', 'content', 'summary', 'relevance_score']df = df[columns]with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:excel_path = tmp.name# First, save the DataFrame to Exceldf.to_excel(excel_path, index=False, engine='openpyxl')# Then, load the workbook and modify the relevance_score columnwb = load_workbook(excel_path)ws = wb.activefor row in dataframe_to_rows(df, index=False, header=True):ws.append(row)# Format the relevance_score column as numbersfor cell in ws['F'][1:]: # Assuming relevance_score is in column Fcell.number_format = '0.00'wb.save(excel_path)return excel_path317 unmodified lines31831932032132232332432532632732832933033133233317 unmodified lines351352353354355356357358359360361100 unmodified lines4624634644654664674684694704714724734744754761 unmodified line478479480481482483484485486487488489490491492493494495496497498499317 unmodified lines"content": clean_content,"summary": full_summary,"cleaned_summary": cleaned_summary,"relevance_score": relevance_score}processed_articles.append(processed_article)except Exception as e:print(f"Error processing article: {str(e)}")# Debug printprint("Processed articles:")for article in processed_articles:print(f"Title: {article['title']}, Score: {article['relevance_score']}")if not processed_articles:return f"Failed to process any news articles from {news_source}. Please try again or check the summarization process."17 unmodified lines# Update news_database for excel exportglobal news_databasenews_database = processed_articles # Directly assign the processed articlesprint("Updated news_database:")for article in news_database:print(f"Title: {article['title']}, Score: {article['relevance_score']}")return f"Processed and added {len(processed_articles)} news articles from {news_source} to the database."except Exception as e:100 unmodified linesdef export_news_to_excel():global news_databaseif not news_database:return "No articles to export. Please fetch news first."print("Exporting the following articles:")for article in news_database:print(f"Title: {article['title']}, Score: {article.get('relevance_score', 'N/A')}")df = pd.DataFrame(news_database)# Ensure relevance_score is present and convert to floatif 'relevance_score' not in df.columns:1 unmodified lineelse:df['relevance_score'] = pd.to_numeric(df['relevance_score'], errors='coerce').fillna(0.0)# Use the cleaned summary for the Excel exportif 'cleaned_summary' in df.columns:df['summary'] = df['cleaned_summary']df = df.drop(columns=['cleaned_summary'])# Reorder columns to put relevance_score after summarycolumns = ['published_date', 'title', 'url', 'content', 'summary', 'relevance_score']df = df[[col for col in columns if col in df.columns]]print("Final DataFrame before export:")print(df[['title', 'relevance_score']])with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmp:excel_path = tmp.namedf.to_excel(excel_path, index=False, engine='openpyxl')print(f"Excel file saved to: {excel_path}")return excel_path
app.py+28-22