File size: 2,863 Bytes
03998b1
 
055c172
03998b1
333badc
03998b1
58853b8
055c172
58853b8
eaa8097
58853b8
eaa8097
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
828e70c
 
 
 
 
 
 
 
 
c7c5d9d
 
 
828e70c
 
c7c5d9d
0d5b03d
828e70c
 
 
 
53c0209
 
 
828e70c
 
 
 
99f1718
828e70c
 
a438936
828e70c
 
 
 
 
 
 
 
03998b1
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
import csv
import askURL as askURL
import gradio as gr

def process_data(file, col_header, user_message, openAI_api_key):

    if openAI_api_key == '':
        raise gr.Error("You need to enter an API key")
    elif user_message == '':
        raise gr.Error("You need to enter user_message")
    elif col_header == '':
        raise gr.Error("You need to enter col_header")
    elif file is None:
        raise gr.Error("You need to enter file")
    else:
            
        # Open the input file
        with open(file.name, 'r', encoding="utf-8") as input_file:
            # Read the contents of the input file
            reader = csv.DictReader(input_file, delimiter='\t')
            data = list(reader)

            # iterate through every row in the input_file
            for row in data:
                # ...get the URL
                url = row['Link']
                # ...send the URL and the user message to the ask function
                response = askURL.process_url(url, user_message, openAI_api_key)
                row[col_header] = response

        output_fname = 'output_2zzz.tsv'

        # Write the processed data to a new file
        with open(output_fname, 'w') as output_file:
            # Define the fieldnames for the output file
            fieldnames = list(reader.fieldnames) + [col_header]

            # Write the header
            writer = csv.DictWriter(output_file, delimiter='\t', fieldnames=fieldnames, lineterminator='\n')
            writer.writeheader()

            # Write the processed data
            writer.writerows(data)

        return(output_fname)


def attach_articles(file):
    # Open the input file
    with open(file.name, 'r', encoding="utf-8") as input_file:
        # Read the contents of the input file
        reader = csv.DictReader(input_file, delimiter='\t')
        data = list(reader)


        rowcount = 0

        # iterate through every row in the input_file
        for row in data:
            rowcount += 1
            print('rowcount '+str(rowcount)+' of '+str(len(data)))
            # ...get the URL
            url = row['Link']
            # ...send the URL and the user message to the ask function
            content = askURL.get_main_text_from_url(url)

            content = content.replace('\t', ' ').replace('\n', ' ')

            row["Content"] = content
            
    output_fname = 'output_2zzz.tsv'

    # Write the processed data to a new file
    with open(output_fname, 'w') as output_file:
        # Define the fieldnames for the output file
        fieldnames = list(reader.fieldnames) + ["Content"]

        # Write the header
        writer = csv.DictWriter(output_file, delimiter='\t', fieldnames=fieldnames, lineterminator='\n')
        writer.writeheader()

        # Write the processed data
        writer.writerows(data)

    return(output_fname)