kenleeyx commited on
Commit
23192ed
·
1 Parent(s): 9a3ebb6

feat: version 1.0

Browse files

Allows user to input a file containing quotes in a column, the column
name, and a list of tags. OpenAI gpt-4o-mini is used to tag each quote
with appropriate tags from the list.

Future potential improvements:
1: Allow upload of tags as a file if deemed to be useful by users.
2: Introduce tag recommendations by getting GPT to crunch the input
quotes.

Amend with bugfix: added a missing = sign in requirements.txt

Files changed (2) hide show
  1. app.py +100 -3
  2. requirements.txt +3 -0
app.py CHANGED
@@ -1,7 +1,104 @@
1
  import gradio as gr
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
  demo.launch()
 
 
 
 
 
 
1
  import gradio as gr
2
+ import os
3
+ import pandas as pd
4
+ from openai import OpenAI
5
+ import openpyxl
6
+ import json
7
 
8
+ client = OpenAI(
9
+ api_key=os.getenv('OPENAI_KEY'),
10
+ organization='org-6bWY3KRhkJFR0jSzQ61IFaLI',
11
+ project='proj_NWVmGD4VKw62rhiIHTp1OsdL',
12
+ )
13
+
14
+ #need to give info on how to convert to CSV
15
+ title = "Automated Research Code Tagger"
16
+ description = """
17
+ ABOUT:\n
18
+ This automated tagger takes in a list of tags and a list of input quotes. Each input quote is individually fed to OpenAI's ChatGPT together with the list of tags,
19
+ and ChatGPT will respond with the subset of the input tags which are related to the content of the quote.\n
20
+
21
+ HOW TO USE:\n
22
+ 1)Upload a single sheet Excel file containing quotes in a column.(It is ok for the file to contain other data also)\n
23
+ 2)Type in the name of the column where the quotes are located\n
24
+ 3)Type in a list of tags separated by commas. For proper names/slogans/other tags that should be treated as an inseparable unit eg. Nike's "Just Do It", add a * in front of the tag eg. tag1, *Just Do It, tag3, etc.
25
+ This will ensure only quotes containing "Just Do It" exactly are tagged and not other quotes about doing other things.\n
26
+ 4)All the responses from ChatGPT will be collated and displayed in the table on the right, together with the original quotes.
27
+ You may then copy them into an Excel file for further processing. Please allow 5-10 min for processing, especially if you are giving upwards of 100 quotes!\n
28
+
29
+ Please bear in mind that the tags are AI generated so check your results to ensure they make sense before using them.
30
+ I will not be responsible for mistakes made by the AI, but I can try to fix them if you alert me.
31
+ -Kenneth
32
+ """
33
+
34
+ prompt = """
35
+ Given the quote below and the regular tag list below, evaluate each tag in the tag list and determine if the meaning of the quote can be described by that tag topic.
36
+ If so, return the relevant tag in your response. Use only the tags provided in the list. Under no circumstances should you create new tag names.
37
+
38
+ For the tags starting with a *, these tags should be treated as proper nouns(usually product names or slogans) and should not be used unless the quote explicitly contains the entire tag.
39
+ For quotes with meanings that are more ambiguous and can relate to multiple tags, make no assumptions about their meanings and only add tags if the topic of the tag is actually mentioned in the quote.
40
+ If there are no relevant tags to the quote, return an empty list.
41
+
42
+ Quote:
43
+ {quote}
44
+
45
+ Tag list:
46
+ {tags_list}
47
+
48
+ Respond in the following format:
49
+ {{
50
+ "tags":[<tagName1>, <tagName2>]
51
+ }}
52
+ """
53
+ def tag_quote(quote, tags_list):
54
+ response = client.chat.completions.create(
55
+ model = "gpt-4o-mini",
56
+ response_format={"type": "json_object"},
57
+ messages=[
58
+ {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
59
+ {"role": "user", "content": prompt.format(tags_list=tags_list, quote=quote)}
60
+ ]
61
+ )
62
+ print(response.choices[0].message.content)
63
+ return json.loads(response.choices[0].message.content)['tags']
64
+
65
+ def process_quotes(quotes_file_path, quotes_col_name, tags_string):
66
+ print(quotes_file_path)
67
+ print(quotes_col_name)
68
+ print(tags_string)
69
+ tags_list = tags_string.split(',')
70
+ tags_list = [tag.strip() for tag in tags_list]
71
+
72
+ #next 3 lines are necessary as pd.read_excel will rename duplicate columns found in the excel file eg foo -> foo.1, hence we need to extract the first row alone and not as header, and then set it as header for the rest of the DF later.
73
+ quotes_df_cols= pd.read_excel(quotes_file_path, header=None, nrows=1).values[0] #creates a df without header from the excel and takes the first row
74
+ quotes_df = pd.read_excel(quotes_file_path, header=None, skiprows=1) # converts row 2 onwards into the DF, without specifying a header
75
+ quotes_df.columns = quotes_df_cols # sets the first row of excel file as header
76
+
77
+ count = quotes_df.columns.tolist().count(quotes_col_name)
78
+ if count == 0:
79
+ raise gr.Error("No columns with this name found")
80
+ elif count > 1:
81
+ print("Count>1!!")
82
+ raise gr.Error("Multiple columns with this name found, please rename to something unique")
83
+ quotes_data = quotes_df[quotes_col_name]
84
+ quotes_df['Tags'] = quotes_data.apply(tag_quote, args=(tags_list,))
85
+ return quotes_df[[quotes_col_name, 'Tags']]
86
+
87
+ demo = gr.Interface(
88
+ fn=process_quotes,
89
+ inputs=[
90
+ gr.File(label="Quotes Excel File"), # File as generated by TFT software
91
+ gr.Textbox(label="Name of quotes column"), # use this to identify the col with the quotes
92
+ gr.Textbox(label = "List of tags separated by commas")
93
+ ],
94
+ outputs=gr.Dataframe(headers=["Quote", "Tags"], column_widths=["70%", "30%"], scale=2),
95
+ title=title,
96
+ description=description
97
+ )
98
 
 
99
  demo.launch()
100
+
101
+ # For later when I enable usage of own API key
102
+ # api_key = gr.Textbox(
103
+ # type="password", label="Enter your OpenAI API key here (Optional for Perceptech users)"
104
+ # )
requirements.txt CHANGED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio==4.31.5
2
+ openai==1.59.3
3
+ openpyxl==3.1.5