50 lines
1.7 KiB
Python
50 lines
1.7 KiB
Python
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!")
|