fpaldrock commited on
Commit
1ec4340
·
verified ·
1 Parent(s): 9f15610

Iniciando la api

Browse files
Files changed (3) hide show
  1. app.py +70 -0
  2. requirements.txt +2 -0
  3. utils.py +110 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openai
3
+ from utils import define_prompt, model_request, compare_texts
4
+
5
+ st.write('# GPT: Resumir - Corregir - Reescribir')
6
+
7
+ _, psw_col2, _ = st.columns([1,2,1])
8
+ OPENAI_API_KEY = psw_col2.text_input('OpenAI API Key', type='password')
9
+
10
+ _, col2, col3, _ = st.columns(4)
11
+ model = col2.selectbox(
12
+ 'Model',
13
+ ('gpt-3.5-turbo', 'gpt-4o', 'gpt-4.5-preview'))
14
+
15
+ action = col3.selectbox(
16
+ 'Action',
17
+ ('summarize', 'rewrite', 'spellcheck'))
18
+
19
+ if action == 'rewrite':
20
+
21
+ sub_col1, sub_col2, sub_col3, sub_col4 = st.columns(4)
22
+
23
+ audience = sub_col1.selectbox(
24
+ 'audience',
25
+ ('Select an option', 'general', 'knowledgeable', 'expert'))
26
+
27
+ formality = sub_col2.selectbox(
28
+ 'formality',
29
+ ('Select an option', 'informal', 'neutral', 'formal'))
30
+
31
+ domain = sub_col3.selectbox(
32
+ 'domain',
33
+ ('Select an option',
34
+ 'academica',
35
+ 'business',
36
+ 'general',
37
+ 'email',
38
+ 'casual',
39
+ 'creative'))
40
+
41
+ intent = sub_col4.selectbox(
42
+ 'intent',
43
+ ('Select an option', 'inform', 'describe', 'convince', 'tell a story'))
44
+
45
+ style = st.text_input("Person's style (Steve Jobs, Yoda, García Márquez ...)", )
46
+
47
+ input_text = st.text_area('', height =350)
48
+
49
+ if st.button('Run'):
50
+ openai.api_key = OPENAI_API_KEY
51
+ if action == 'summarize':
52
+ prompt = define_prompt(action, input_text)
53
+ resumen = model_request(model, prompt)
54
+ st.markdown(resumen)
55
+
56
+ elif action == 'rewrite':
57
+ text_goals = {'audience' : audience,
58
+ 'formality' : formality,
59
+ 'domain' : domain,
60
+ 'intent' : intent,
61
+ 'style': style}
62
+ prompt = define_prompt(action, input_text, text_goals)
63
+ new_text = model_request(model, prompt)
64
+ st.markdown(new_text)
65
+
66
+ elif action == 'spellcheck':
67
+ prompt = define_prompt(action, input_text)
68
+ spellchecked_text = model_request(model, prompt)
69
+ styled_text = compare_texts(input_text, spellchecked_text)
70
+ st.markdown(styled_text, unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai
2
+ streamlit
utils.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import difflib
3
+
4
+ prompts_to_fill = [
5
+ {'summarize': """
6
+ You will be provided with text delimited by triple quotes.
7
+
8
+ Summarize the text by extracting the most relevant information in the form of bullet points.
9
+
10
+ \"\"\"{}\"\"\"
11
+ """},
12
+ {'rewrite': """
13
+ You will be provided with text delimited by triple quotes.
14
+
15
+ Rewrite the text taking into account the following information:
16
+ {}
17
+ \"\"\"{}\"\"\"
18
+ """},
19
+ {'spellcheck': """
20
+ You will be provided with text delimited by triple quotes.
21
+ If the text contains spell or grammar errors provide the text corrected.
22
+
23
+ If the text does not contain errors return back the text delimited by triple quotes.
24
+
25
+ \"\"\"{}\"\"\"
26
+ """}
27
+ ]
28
+
29
+
30
+ def define_prompt(action, input_text, text_goals=False):
31
+ prompt = ''
32
+
33
+ if action == 'summarize':
34
+ prompt = prompts_to_fill[0]['summarize'].format(input_text)
35
+ elif action == 'rewrite':
36
+ prompt_config = paraphrase_prompt(text_goals)
37
+ prompt = prompts_to_fill[1]['rewrite'].format(prompt_config, input_text)
38
+ elif action == 'spellcheck':
39
+ prompt = prompts_to_fill[2]['spellcheck'].format(input_text)
40
+
41
+ return prompt
42
+
43
+
44
+ def model_request(model, prompt, temp=0):
45
+
46
+ r = openai.chat.completions.create(
47
+ model=model,
48
+ messages=[{
49
+ 'role':'system', 'content':"You are an expert proofreader, the most advanced AI tool on the planet.",
50
+ 'role':'user', 'content': prompt}],
51
+ temperature=temp)
52
+
53
+ return r.choices[0].message.content
54
+
55
+
56
+ goals_prompts = {
57
+ 'audience' : "audience is {}\n",
58
+ 'formality' : "{} tone\n",
59
+ 'domain' : '{}\n',
60
+ 'intent' : 'intent to {}\n',
61
+ 'style' : 'in the style of "{}" \n'
62
+ }
63
+
64
+
65
+ def paraphrase_prompt(text_goals):
66
+
67
+ prompt_parameters = []
68
+ for key, value in text_goals.items():
69
+ if key == 'style':
70
+ if value != '':
71
+ prompt_parameters.append(goals_prompts[key].format(value))
72
+ elif value != 'Select an option':
73
+ prompt_parameters.append(goals_prompts[key].format(value))
74
+
75
+ if len(prompt_parameters) == 0:
76
+ prompt_config = 'it is easier to understand\navoid repetitions of words and phrases\n'
77
+ else:
78
+ prompt_config = ''.join(prompt_parameters)
79
+
80
+ return prompt_config
81
+
82
+
83
+ def correction_style(word, correction):
84
+ html_correction = """<abbr style="text-decoration: none" title="{}">\
85
+ <mark style="background-color: #afa">{}</mark></abbr>"""
86
+ return html_correction.format(word.replace('-',''), correction.replace('+',''))
87
+
88
+
89
+ def compare_texts(text_input, text_corrected):
90
+
91
+ text_corrected = text_corrected.replace('"', '')
92
+ d = difflib.Differ()
93
+ diff = d.compare(text_input.split(), text_corrected.split())
94
+ list_words = [word for word in diff if '?' not in word]
95
+
96
+ reviewed_sentence = []
97
+ correction_word = False
98
+ for idx, word in enumerate(list_words):
99
+ if correction_word:
100
+ correction_word=False
101
+ pass
102
+ elif '-' in word and '+' in list_words[idx+1]:
103
+ correction_word=True
104
+ reviewed_sentence.append(correction_style(word, list_words[idx+1]))
105
+ else:
106
+ reviewed_sentence.append(word)
107
+
108
+ return ''.join(reviewed_sentence)
109
+
110
+