Spaces:
Sleeping
Sleeping
Refactor code
Browse files- .gitignore +2 -1
- app.py +5 -206
- functions.py +212 -0
.gitignore
CHANGED
|
@@ -1,2 +1,3 @@
|
|
| 1 |
.idea/*
|
| 2 |
-
data.json
|
|
|
|
|
|
| 1 |
.idea/*
|
| 2 |
+
data.json
|
| 3 |
+
__pycache__/
|
app.py
CHANGED
|
@@ -1,208 +1,7 @@
|
|
| 1 |
-
import
|
| 2 |
-
import time
|
| 3 |
-
import json
|
| 4 |
-
import openai
|
| 5 |
-
import gradio as gr
|
| 6 |
-
from datetime import datetime
|
| 7 |
-
from openai.error import RateLimitError, APIConnectionError, Timeout, APIError, \
|
| 8 |
-
ServiceUnavailableError, InvalidRequestError
|
| 9 |
-
from huggingface_hub import hf_hub_download, HfApi
|
| 10 |
|
| 11 |
-
openai.api_key = os.environ.get('API_KEY')
|
| 12 |
-
|
| 13 |
-
score_parameters = [
|
| 14 |
-
'Personalidad', 'Intereses', 'Lenguaje/Estilo',
|
| 15 |
-
'Autenticidad', 'Habilidad de conversaci贸n',
|
| 16 |
-
'Marca/Producto', 'Identificaci贸n', 'Experiencia de uso',
|
| 17 |
-
'Recomendacion', 'Conversaci贸n organica'
|
| 18 |
-
]
|
| 19 |
-
|
| 20 |
-
authors = ['Sofia', 'Eliza', 'Sindy', 'Carlos', 'Andres', 'Adriana', 'Carolina', 'Valeria']
|
| 21 |
-
|
| 22 |
-
models = ["gpt-4"]
|
| 23 |
-
|
| 24 |
-
temperature_values = [0.2, 0.8, 1.0]
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def innit_bot():
|
| 28 |
-
"""
|
| 29 |
-
Initialize the bot by adding the prompt from the txt file to the messages history
|
| 30 |
-
"""
|
| 31 |
-
with open('prompt.txt', encoding='utf-8') as file:
|
| 32 |
-
prompt = file.read()
|
| 33 |
-
message_history = [{"role": "system", "content": prompt}]
|
| 34 |
-
|
| 35 |
-
return message_history
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def make_visible():
|
| 39 |
-
"""
|
| 40 |
-
Makes visible the returned elements
|
| 41 |
-
"""
|
| 42 |
-
return (
|
| 43 |
-
gr.Chatbot.update(visible=True),
|
| 44 |
-
gr.Textbox.update(visible=True),
|
| 45 |
-
gr.Row.update(visible=True))
|
| 46 |
-
|
| 47 |
-
def make_noninteractive():
|
| 48 |
-
"""
|
| 49 |
-
Makes no interactive the returned elements
|
| 50 |
-
"""
|
| 51 |
-
return (
|
| 52 |
-
gr.Dropdown.update(interactive=False),
|
| 53 |
-
gr.Radio.update(interactive=False))
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def call_api(model: gr.Dropdown, msg_history: gr.State, temperature: gr.State):
|
| 57 |
-
"""
|
| 58 |
-
Returns the API's response
|
| 59 |
-
"""
|
| 60 |
-
response = openai.ChatCompletion.create(
|
| 61 |
-
model=model,
|
| 62 |
-
messages=msg_history,
|
| 63 |
-
temperature=temperature
|
| 64 |
-
)
|
| 65 |
-
return response
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
def handle_call(model: gr.Dropdown, msg_history: gr.State, temperature: gr.State):
|
| 69 |
-
"""
|
| 70 |
-
Returns the response and waiting time of the AI. It also handles the possible errors
|
| 71 |
-
"""
|
| 72 |
-
tries = 0
|
| 73 |
-
max_tries = 3
|
| 74 |
-
while True:
|
| 75 |
-
try:
|
| 76 |
-
start_time = time.time()
|
| 77 |
-
response = call_api(model, msg_history, temperature)
|
| 78 |
-
end_time = time.time()
|
| 79 |
-
break
|
| 80 |
-
|
| 81 |
-
except InvalidRequestError as e:
|
| 82 |
-
print(e)
|
| 83 |
-
response = 'Ya no tienes mas tokens disponibles. Envia lo que tengas hasta el momento e inicia otro chat'
|
| 84 |
-
raise gr.Error(response)
|
| 85 |
-
|
| 86 |
-
except (RateLimitError, APIError, Timeout, APIConnectionError, ServiceUnavailableError) as e:
|
| 87 |
-
print(e)
|
| 88 |
-
|
| 89 |
-
if tries == max_tries:
|
| 90 |
-
response = "Despues de muchos intentos, no se pudo completar la comunicacion con OpenAI. " \
|
| 91 |
-
"Envia lo que tengas hasta el momento e inicia un chat nuevo dentro de unos minutos."
|
| 92 |
-
raise gr.Error(response)
|
| 93 |
-
|
| 94 |
-
tries += 1
|
| 95 |
-
time.sleep(60)
|
| 96 |
-
|
| 97 |
-
needed_time = end_time - start_time
|
| 98 |
-
return response, needed_time
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
def get_ai_answer(msg: str, model: gr.Dropdown, msg_history: gr.State, temperature: gr.State):
|
| 102 |
-
"""
|
| 103 |
-
Returns the response given by the model, all the message history so far and the seconds
|
| 104 |
-
the api took to retrieve such response. Both depend on the model
|
| 105 |
-
"""
|
| 106 |
-
msg_history.append({"role": "user", "content": msg})
|
| 107 |
-
response, needed_time = handle_call(model, msg_history, temperature)
|
| 108 |
-
AI_response = response["choices"][0]["message"]["content"]
|
| 109 |
-
msg_history.append({'role': 'assistant', 'content': AI_response})
|
| 110 |
-
|
| 111 |
-
return AI_response, msg_history, needed_time
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
def get_answer(
|
| 115 |
-
msg: str, msg_history: gr.State,
|
| 116 |
-
chatbot_history: gr.Chatbot, waiting_time: gr.State,
|
| 117 |
-
temperature: gr.State, model: gr.Dropdown):
|
| 118 |
-
"""
|
| 119 |
-
Cleans msg box, adds the new message to the message history,
|
| 120 |
-
gets the answer from the bot and adds it to the chatbot history
|
| 121 |
-
and gets the time needed to get such answer and saves it
|
| 122 |
-
"""
|
| 123 |
-
|
| 124 |
-
# Get bot answer (output), messages history and waiting time
|
| 125 |
-
AI_response, msg_history, needed_time = get_ai_answer(msg, model, msg_history, temperature)
|
| 126 |
-
|
| 127 |
-
# Save waiting time
|
| 128 |
-
waiting_time.append(needed_time)
|
| 129 |
-
|
| 130 |
-
# Save output in the chat
|
| 131 |
-
chatbot_history.append((msg, AI_response))
|
| 132 |
-
|
| 133 |
-
return "", msg_history, chatbot_history, waiting_time
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
def save_scores(
|
| 137 |
-
author: gr.Dropdown, temperature: gr.State,
|
| 138 |
-
history: gr.Chatbot, waiting_time: gr.State,
|
| 139 |
-
model: gr.Dropdown, opinion: gr.Textbox, *score_values):
|
| 140 |
-
"""
|
| 141 |
-
Saves the scores and chat's info into the json file
|
| 142 |
-
"""
|
| 143 |
-
# Get the score of each parameter
|
| 144 |
-
scores = dict()
|
| 145 |
-
for parameter, score in zip(score_parameters, score_values):
|
| 146 |
-
|
| 147 |
-
# Check the score is a valid value if not, raise Error
|
| 148 |
-
if score is None:
|
| 149 |
-
raise gr.Error('Asegurese de haber seleccionado al menos 1 opcion en cada categoria')
|
| 150 |
-
|
| 151 |
-
scores[parameter] = score
|
| 152 |
-
|
| 153 |
-
# Get all the messages including their reaction
|
| 154 |
-
chat = []
|
| 155 |
-
for conversation in history:
|
| 156 |
-
info = {
|
| 157 |
-
'message': conversation[0],
|
| 158 |
-
'answer': conversation[1],
|
| 159 |
-
'waiting': waiting_time.pop(0)
|
| 160 |
-
}
|
| 161 |
-
chat.append(info)
|
| 162 |
-
|
| 163 |
-
date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 164 |
-
|
| 165 |
-
with open('prompt.txt', encoding='utf-8') as file:
|
| 166 |
-
prompt = file.read()
|
| 167 |
-
|
| 168 |
-
# Save the info
|
| 169 |
-
session = dict(
|
| 170 |
-
prompt=prompt,
|
| 171 |
-
temperature=temperature,
|
| 172 |
-
scores=scores,
|
| 173 |
-
opinion=opinion,
|
| 174 |
-
chat=chat,
|
| 175 |
-
author=author,
|
| 176 |
-
model=model,
|
| 177 |
-
date=date
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
-
# Open the file, add the new info and save it
|
| 181 |
-
hf_hub_download(
|
| 182 |
-
repo_id=os.environ.get('DATA'), repo_type='dataset', filename="data.json", token=os.environ.get('HUB_TOKEN'),
|
| 183 |
-
local_dir="./"
|
| 184 |
-
)
|
| 185 |
-
|
| 186 |
-
with open('data.json', 'r') as infile:
|
| 187 |
-
past_sessions = json.load(infile)
|
| 188 |
-
|
| 189 |
-
# Add the new info
|
| 190 |
-
past_sessions['sessions'].append(session)
|
| 191 |
-
with open('data.json', 'w', encoding='utf-8') as outfile:
|
| 192 |
-
json.dump(past_sessions, outfile, indent=4, ensure_ascii=False)
|
| 193 |
-
|
| 194 |
-
# Save the updated file
|
| 195 |
-
api = HfApi(token=os.environ.get('HUB_TOKEN'))
|
| 196 |
-
api.upload_file(
|
| 197 |
-
path_or_fileobj="data.json",
|
| 198 |
-
path_in_repo="data.json",
|
| 199 |
-
repo_id=os.environ.get('DATA'),
|
| 200 |
-
repo_type='dataset'
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
# Return a confirmation message
|
| 204 |
-
return 'Done'
|
| 205 |
|
|
|
|
| 206 |
|
| 207 |
with gr.Blocks() as app:
|
| 208 |
msg_history = gr.State() # Messages with the format used by OpenAI
|
|
@@ -226,8 +25,9 @@ with gr.Blocks() as app:
|
|
| 226 |
with gr.Row():
|
| 227 |
scores = [
|
| 228 |
gr.Radio(choices=['Aprovado', 'No aprovado'], label=parameter)
|
| 229 |
-
for parameter in
|
| 230 |
]
|
|
|
|
| 231 |
with gr.Column(scale=25):
|
| 232 |
opinion_box = gr.Textbox(label='Opinion')
|
| 233 |
scores_btn = gr.Button(value='Send scores')
|
|
@@ -239,8 +39,7 @@ with gr.Blocks() as app:
|
|
| 239 |
).then(
|
| 240 |
make_noninteractive, None, [author, temperature]
|
| 241 |
).then(
|
| 242 |
-
make_visible, None, [
|
| 243 |
-
chatbot, message, scores_row]
|
| 244 |
)
|
| 245 |
|
| 246 |
message.submit(
|
|
|
|
| 1 |
+
from functions import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
scores_parameters, authors, models, temperature_values = get_main_data()
|
| 5 |
|
| 6 |
with gr.Blocks() as app:
|
| 7 |
msg_history = gr.State() # Messages with the format used by OpenAI
|
|
|
|
| 25 |
with gr.Row():
|
| 26 |
scores = [
|
| 27 |
gr.Radio(choices=['Aprovado', 'No aprovado'], label=parameter)
|
| 28 |
+
for parameter in scores_parameters
|
| 29 |
]
|
| 30 |
+
|
| 31 |
with gr.Column(scale=25):
|
| 32 |
opinion_box = gr.Textbox(label='Opinion')
|
| 33 |
scores_btn = gr.Button(value='Send scores')
|
|
|
|
| 39 |
).then(
|
| 40 |
make_noninteractive, None, [author, temperature]
|
| 41 |
).then(
|
| 42 |
+
make_visible, None, [chatbot, message, scores_row]
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
message.submit(
|
functions.py
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import json
|
| 4 |
+
import openai
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
from openai.error import RateLimitError, APIConnectionError, Timeout, APIError, \
|
| 8 |
+
ServiceUnavailableError, InvalidRequestError
|
| 9 |
+
from huggingface_hub import hf_hub_download, HfApi
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def get_main_data():
|
| 13 |
+
"""
|
| 14 |
+
Initializes the key for the api and returns the parameters for the scores, name of the possible authors,
|
| 15 |
+
models and possible temperature values
|
| 16 |
+
"""
|
| 17 |
+
openai.api_key = os.environ.get('API_KEY')
|
| 18 |
+
|
| 19 |
+
scores_parameters = [
|
| 20 |
+
'Personalidad', 'Intereses', 'Lenguaje/Estilo', 'Autenticidad', 'Habilidad de conversaci贸n',
|
| 21 |
+
'Marca/Producto', 'Identificaci贸n', 'Experiencia de uso', 'Recomendacion', 'Conversaci贸n organica'
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
authors = ['Sofia', 'Eliza', 'Sindy', 'Carlos', 'Andres', 'Adriana', 'Carolina', 'Valeria']
|
| 25 |
+
models = ["gpt-4"]
|
| 26 |
+
temperature_values = [0.2, 0.8, 1.0]
|
| 27 |
+
|
| 28 |
+
return scores_parameters, authors, models, temperature_values
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def innit_bot():
|
| 32 |
+
"""
|
| 33 |
+
Initialize the bot by adding the prompt from the txt file to the messages history
|
| 34 |
+
"""
|
| 35 |
+
with open('prompt.txt', encoding='utf-8') as file:
|
| 36 |
+
prompt = file.read()
|
| 37 |
+
message_history = [{"role": "system", "content": prompt}]
|
| 38 |
+
|
| 39 |
+
return message_history
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def make_visible():
|
| 43 |
+
"""
|
| 44 |
+
Makes visible the returned elements
|
| 45 |
+
"""
|
| 46 |
+
return (
|
| 47 |
+
gr.Chatbot.update(visible=True),
|
| 48 |
+
gr.Textbox.update(visible=True),
|
| 49 |
+
gr.Row.update(visible=True))
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def make_noninteractive():
|
| 53 |
+
"""
|
| 54 |
+
Makes no interactive the returned elements
|
| 55 |
+
"""
|
| 56 |
+
return (
|
| 57 |
+
gr.Dropdown.update(interactive=False),
|
| 58 |
+
gr.Radio.update(interactive=False))
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def call_api(model: gr.Dropdown, msg_history: gr.State, temperature: gr.State):
|
| 62 |
+
"""
|
| 63 |
+
Returns the API's response
|
| 64 |
+
"""
|
| 65 |
+
response = openai.ChatCompletion.create(
|
| 66 |
+
model=model,
|
| 67 |
+
messages=msg_history,
|
| 68 |
+
temperature=temperature
|
| 69 |
+
)
|
| 70 |
+
return response
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def handle_call(model: gr.Dropdown, msg_history: gr.State, temperature: gr.State):
|
| 74 |
+
"""
|
| 75 |
+
Returns the response and waiting time of the AI. It also handles the possible errors
|
| 76 |
+
"""
|
| 77 |
+
tries = 0
|
| 78 |
+
max_tries = 3
|
| 79 |
+
while True:
|
| 80 |
+
try:
|
| 81 |
+
start_time = time.time()
|
| 82 |
+
response = call_api(model, msg_history, temperature)
|
| 83 |
+
end_time = time.time()
|
| 84 |
+
break
|
| 85 |
+
|
| 86 |
+
except InvalidRequestError as e:
|
| 87 |
+
print(e)
|
| 88 |
+
response = 'Ya no tienes mas tokens disponibles. Envia lo que tengas hasta el momento e inicia otro chat'
|
| 89 |
+
raise gr.Error(response)
|
| 90 |
+
|
| 91 |
+
except (RateLimitError, APIError, Timeout, APIConnectionError, ServiceUnavailableError) as e:
|
| 92 |
+
print(e)
|
| 93 |
+
|
| 94 |
+
if tries == max_tries:
|
| 95 |
+
response = "Despues de muchos intentos, no se pudo completar la comunicacion con OpenAI. " \
|
| 96 |
+
"Envia lo que tengas hasta el momento e inicia un chat nuevo dentro de unos minutos."
|
| 97 |
+
raise gr.Error(response)
|
| 98 |
+
|
| 99 |
+
tries += 1
|
| 100 |
+
time.sleep(60)
|
| 101 |
+
|
| 102 |
+
needed_time = end_time - start_time
|
| 103 |
+
return response, needed_time
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def get_ai_answer(msg: str, model: gr.Dropdown, msg_history: gr.State, temperature: gr.State):
|
| 107 |
+
"""
|
| 108 |
+
Returns the response given by the model, all the message history so far and the seconds
|
| 109 |
+
the api took to retrieve such response. Both depend on the model
|
| 110 |
+
"""
|
| 111 |
+
msg_history.append({"role": "user", "content": msg})
|
| 112 |
+
response, needed_time = handle_call(model, msg_history, temperature)
|
| 113 |
+
AI_response = response["choices"][0]["message"]["content"]
|
| 114 |
+
msg_history.append({'role': 'assistant', 'content': AI_response})
|
| 115 |
+
|
| 116 |
+
return AI_response, msg_history, needed_time
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def get_answer(
|
| 120 |
+
msg: str, msg_history: gr.State,
|
| 121 |
+
chatbot_history: gr.Chatbot, waiting_time: gr.State,
|
| 122 |
+
temperature: gr.State, model: gr.Dropdown):
|
| 123 |
+
"""
|
| 124 |
+
Cleans msg box, adds the new message to the message history,
|
| 125 |
+
gets the answer from the bot and adds it to the chatbot history
|
| 126 |
+
and gets the time needed to get such answer and saves it
|
| 127 |
+
"""
|
| 128 |
+
|
| 129 |
+
# Get bot answer (output), messages history and waiting time
|
| 130 |
+
AI_response, msg_history, needed_time = get_ai_answer(msg, model, msg_history, temperature)
|
| 131 |
+
|
| 132 |
+
# Save waiting time
|
| 133 |
+
waiting_time.append(needed_time)
|
| 134 |
+
|
| 135 |
+
# Save output in the chat
|
| 136 |
+
chatbot_history.append((msg, AI_response))
|
| 137 |
+
|
| 138 |
+
return "", msg_history, chatbot_history, waiting_time
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def save_scores(
|
| 142 |
+
author: gr.Dropdown, temperature: gr.State,
|
| 143 |
+
history: gr.Chatbot, waiting_time: gr.State,
|
| 144 |
+
model: gr.Dropdown, opinion: gr.Textbox, *score_values):
|
| 145 |
+
"""
|
| 146 |
+
Saves the scores and chat's info into the json file
|
| 147 |
+
"""
|
| 148 |
+
# Get the parameters for each score
|
| 149 |
+
score_parameters, _, _, _ = get_main_data()
|
| 150 |
+
|
| 151 |
+
# Get the score of each parameter
|
| 152 |
+
scores = dict()
|
| 153 |
+
for parameter, score in zip(score_parameters, score_values):
|
| 154 |
+
|
| 155 |
+
# Check the score is a valid value if not, raise Error
|
| 156 |
+
if score is None:
|
| 157 |
+
raise gr.Error('Asegurese de haber seleccionado al menos 1 opcion en cada categoria')
|
| 158 |
+
|
| 159 |
+
scores[parameter] = score
|
| 160 |
+
|
| 161 |
+
# Get all the messages including their reaction
|
| 162 |
+
chat = []
|
| 163 |
+
for conversation in history:
|
| 164 |
+
info = {
|
| 165 |
+
'message': conversation[0],
|
| 166 |
+
'answer': conversation[1],
|
| 167 |
+
'waiting': waiting_time.pop(0)
|
| 168 |
+
}
|
| 169 |
+
chat.append(info)
|
| 170 |
+
|
| 171 |
+
date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 172 |
+
|
| 173 |
+
with open('prompt.txt', encoding='utf-8') as file:
|
| 174 |
+
prompt = file.read()
|
| 175 |
+
|
| 176 |
+
# Save the info
|
| 177 |
+
session = dict(
|
| 178 |
+
prompt=prompt,
|
| 179 |
+
temperature=temperature,
|
| 180 |
+
scores=scores,
|
| 181 |
+
opinion=opinion,
|
| 182 |
+
chat=chat,
|
| 183 |
+
author=author,
|
| 184 |
+
model=model,
|
| 185 |
+
date=date
|
| 186 |
+
)
|
| 187 |
+
|
| 188 |
+
# Open the file, add the new info and save it
|
| 189 |
+
hf_hub_download(
|
| 190 |
+
repo_id=os.environ.get('DATA'), repo_type='dataset', filename="data.json", token=os.environ.get('HUB_TOKEN'),
|
| 191 |
+
local_dir="./"
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
with open('data.json', 'r') as infile:
|
| 195 |
+
past_sessions = json.load(infile)
|
| 196 |
+
|
| 197 |
+
# Add the new info
|
| 198 |
+
past_sessions['sessions'].append(session)
|
| 199 |
+
with open('data.json', 'w', encoding='utf-8') as outfile:
|
| 200 |
+
json.dump(past_sessions, outfile, indent=4, ensure_ascii=False)
|
| 201 |
+
|
| 202 |
+
# Save the updated file
|
| 203 |
+
api = HfApi(token=os.environ.get('HUB_TOKEN'))
|
| 204 |
+
api.upload_file(
|
| 205 |
+
path_or_fileobj="data.json",
|
| 206 |
+
path_in_repo="data.json",
|
| 207 |
+
repo_id=os.environ.get('DATA'),
|
| 208 |
+
repo_type='dataset'
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
# Return a confirmation message
|
| 212 |
+
return 'Done'
|