Update app.py
Browse files317 unmodified lines31831932032132132232332432532632732832933033133233317 unmodified lines351352353350354355356357358359360361100 unmodified lines4624634644574654594604614664674684694704714724734744754761 unmodified line478479480481482483484485486487471488489490491492493494475476495478479480481482483484485486487488489490496497498499317 unmodified lines "content": clean_content, "summary": full_summary, "cleaned_summary": cleaned_summary, "relevance_score": relevance_score # Ensure this line is present "relevance_score": relevance_score } processed_articles.append(processed_article) except Exception as e: print(f"Error processing article: {str(e)}") # Debug print print("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 export global news_database news_database.extend(processed_articles) news_database = processed_articles # Directly assign the processed articles print("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 lines def export_news_to_excel(): global news_database df = pd.DataFrame(news_database) # Use the cleaned summary for the Excel export df['summary'] = df['cleaned_summary'] df = df.drop(columns=['cleaned_summary']) # Remove the extra column if 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 float if 'relevance_score' not in df.columns:1 unmodified line else: df['relevance_score'] = pd.to_numeric(df['relevance_score'], errors='coerce').fillna(0.0) # Use the cleaned summary for the Excel export if 'cleaned_summary' in df.columns: df['summary'] = df['cleaned_summary'] df = df.drop(columns=['cleaned_summary']) # Reorder columns to put relevance_score after summary columns = ['published_date', 'title', 'url', 'content', 'summary', 'relevance_score'] df = df[columns] 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.name # First, save the DataFrame to Excel df.to_excel(excel_path, index=False, engine='openpyxl') # Then, load the workbook and modify the relevance_score column wb = load_workbook(excel_path) ws = wb.active for row in dataframe_to_rows(df, index=False, header=True): ws.append(row) # Format the relevance_score column as numbers for cell in ws['F'][1:]: # Assuming relevance_score is in column F cell.number_format = '0.00' wb.save(excel_path) print(f"Excel file saved to: {excel_path}") return excel_path
app.py+28-22