From c10f528d8ac7f281ddf9ca432bd57066d3f8f5f0 Mon Sep 17 00:00:00 2001 From: thanhtl Date: Wed, 19 Feb 2025 21:54:15 +0700 Subject: [PATCH] Add cell summary.py --- cell summary.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 cell summary.py diff --git a/cell summary.py b/cell summary.py new file mode 100644 index 0000000..77ec7e5 --- /dev/null +++ b/cell summary.py @@ -0,0 +1,49 @@ +import requests +import openpyxl + +# Ollama server URL (update based on your setup) +OLLAMA_URL = "http://192.168.40.142:11434/api/generate" + +# Load the Excel file +file_path = "your_excel_file.xlsx" +wb = openpyxl.load_workbook(file_path) +sheet = wb.active # Select the active sheet + +# Define the column for original text and where to save summaries +text_column = "A" # Column containing text to summarize +summary_column = "G" # Column to store summaries +start_row = 2 # Starting row (assuming row 1 contains headers) + +def summarize_text(text): + """Send text to Ollama for summarization""" + payload = { + "model": "mistral:latest", # Change model if needed + "prompt": f"Summarize the following text:\n\n{text}", + "stream": False + } + response = requests.post(OLLAMA_URL, json=payload) + + print(f"Status Code: {response.status_code}") # Print HTTP status + print(f"Response Text: {response.text}") # Print response content + + if response.status_code == 200: + try: + return response.json()["response"] + except KeyError: + print("Error: 'response' key not found in JSON") + return "Error in summarization" + else: + return f"Error in summarization (HTTP {response.status_code})" + +# Loop through all rows and summarize text +for row in range(start_row, sheet.max_row + 1): + cell_value = sheet[f"{text_column}{row}"].value # Get text from column B + + if cell_value: + print(f"Summarizing row {row}...") + summary = summarize_text(cell_value) + sheet[f"{summary_column}{row}"] = summary # Store summary in column C + +# Save updated Excel file +wb.save(file_path) +print("\nAll summaries saved successfully!")