-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsums.py
More file actions
90 lines (75 loc) · 3.14 KB
/
Copy pathsums.py
File metadata and controls
90 lines (75 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import csv
import time
from openai import OpenAI
client = OpenAI()
# Define the input and output file names
input_folder = 'input.csv'
output_file_name = 'output.csv'
input_file_name = 'input.csv'
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
retry_limit = 3
retry_interval = 60
retry_count = 0
#limit size of prompt
while retry_count < retry_limit:
try:
response = client.chat.completions.create(
model=model,
messages=messages,
max_tokens = 3000,
temperature=0, # this is the degree of randomness of the model's output
)
retry_count = retry_limit
except Exception as e:
print(f"An error occurred: {str(e)}")
retry_count += 1
if retry_count < retry_limit:
print(f"Retrying in {retry_interval} seconds...")
time.sleep(retry_interval)
else:
print("Exceeded retry limit. Skipping...")
response = "ERROR"
#print(response.choices[0].message)
return response.choices[0].message.content
def make_summary(stream):
prompt = f"""
Read the following transcript and tell me, yes or no, if \
the transcript is likely an interview with a journalist \
and a medical expert.<transcript>{stream[:3000]}<transcript> \
"""
summary = ""
summary = get_completion(prompt)
return summary
def process_csv(encoding):
with open(input_file_name, mode='r', newline='', encoding=encoding) as infile:
reader = csv.DictReader(infile)
rows_with_counts = []
this_record = ""
for row in reader:
#print(row)
try:
summary = make_summary(row['SegmentTranscript'])
# Add the summary to the row
#Lets see a print out
this_record = f"{row['Date']} | {row['Network']} | "
print(f"{this_record}{summary}")
row['summary'] = summary
rows_with_counts.append(row)
except UnicodeDecodeError as e:
print(f"Error reading row {reader.line_num}: {e}. This row will be skipped.")
return rows_with_counts, reader.fieldnames
# Try reading with utf-8, if it fails, try with latin1
try:
rows_with_counts, fieldnames = process_csv('utf-8')
except UnicodeDecodeError:
print("Failed to read the file with utf-8 encoding, trying with latin1...")
rows_with_counts, fieldnames = process_csv('latin1')
# Define the field names for the output CSV
fieldnames = fieldnames + ['summary']
# Open the output CSV file and write the data with the additional 'count' column
with open(output_file_name, mode='w', newline='', encoding='utf-8') as outfile:
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows_with_counts)
print(f'Process completed. The output has been saved in "{output_file_name}"')