Spaces:
Sleeping
Sleeping
cristopher adasme
commited on
Commit
路
1d0f027
1
Parent(s):
3d8119d
first commit
Browse files- .gitignore +160 -0
- app.py +70 -0
- prompts/amsa_smart_form.json +45 -0
- prompts/rubrica_smart_form.txt +7 -0
- requirements.txt +8 -0
- speech_to_text.py +27 -0
- utils.py +30 -0
.gitignore
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Byte-compiled / optimized / DLL files
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
|
6 |
+
# C extensions
|
7 |
+
*.so
|
8 |
+
|
9 |
+
# Distribution / packaging
|
10 |
+
.Python
|
11 |
+
build/
|
12 |
+
develop-eggs/
|
13 |
+
dist/
|
14 |
+
downloads/
|
15 |
+
eggs/
|
16 |
+
.eggs/
|
17 |
+
lib/
|
18 |
+
lib64/
|
19 |
+
parts/
|
20 |
+
sdist/
|
21 |
+
var/
|
22 |
+
wheels/
|
23 |
+
share/python-wheels/
|
24 |
+
*.egg-info/
|
25 |
+
.installed.cfg
|
26 |
+
*.egg
|
27 |
+
MANIFEST
|
28 |
+
|
29 |
+
# PyInstaller
|
30 |
+
# Usually these files are written by a python script from a template
|
31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
32 |
+
*.manifest
|
33 |
+
*.spec
|
34 |
+
|
35 |
+
# Installer logs
|
36 |
+
pip-log.txt
|
37 |
+
pip-delete-this-directory.txt
|
38 |
+
|
39 |
+
# Unit test / coverage reports
|
40 |
+
htmlcov/
|
41 |
+
.tox/
|
42 |
+
.nox/
|
43 |
+
.coverage
|
44 |
+
.coverage.*
|
45 |
+
.cache
|
46 |
+
nosetests.xml
|
47 |
+
coverage.xml
|
48 |
+
*.cover
|
49 |
+
*.py,cover
|
50 |
+
.hypothesis/
|
51 |
+
.pytest_cache/
|
52 |
+
cover/
|
53 |
+
|
54 |
+
# Translations
|
55 |
+
*.mo
|
56 |
+
*.pot
|
57 |
+
|
58 |
+
# Django stuff:
|
59 |
+
*.log
|
60 |
+
local_settings.py
|
61 |
+
db.sqlite3
|
62 |
+
db.sqlite3-journal
|
63 |
+
|
64 |
+
# Flask stuff:
|
65 |
+
instance/
|
66 |
+
.webassets-cache
|
67 |
+
|
68 |
+
# Scrapy stuff:
|
69 |
+
.scrapy
|
70 |
+
|
71 |
+
# Sphinx documentation
|
72 |
+
docs/_build/
|
73 |
+
|
74 |
+
# PyBuilder
|
75 |
+
.pybuilder/
|
76 |
+
target/
|
77 |
+
|
78 |
+
# Jupyter Notebook
|
79 |
+
.ipynb_checkpoints
|
80 |
+
|
81 |
+
# IPython
|
82 |
+
profile_default/
|
83 |
+
ipython_config.py
|
84 |
+
|
85 |
+
# pyenv
|
86 |
+
# For a library or package, you might want to ignore these files since the code is
|
87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
88 |
+
# .python-version
|
89 |
+
|
90 |
+
# pipenv
|
91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
94 |
+
# install all needed dependencies.
|
95 |
+
#Pipfile.lock
|
96 |
+
|
97 |
+
# poetry
|
98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
100 |
+
# commonly ignored for libraries.
|
101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
102 |
+
#poetry.lock
|
103 |
+
|
104 |
+
# pdm
|
105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
106 |
+
#pdm.lock
|
107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
108 |
+
# in version control.
|
109 |
+
# https://pdm.fming.dev/#use-with-ide
|
110 |
+
.pdm.toml
|
111 |
+
|
112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
113 |
+
__pypackages__/
|
114 |
+
|
115 |
+
# Celery stuff
|
116 |
+
celerybeat-schedule
|
117 |
+
celerybeat.pid
|
118 |
+
|
119 |
+
# SageMath parsed files
|
120 |
+
*.sage.py
|
121 |
+
|
122 |
+
# Environments
|
123 |
+
.env
|
124 |
+
.venv
|
125 |
+
env/
|
126 |
+
venv/
|
127 |
+
ENV/
|
128 |
+
env.bak/
|
129 |
+
venv.bak/
|
130 |
+
|
131 |
+
# Spyder project settings
|
132 |
+
.spyderproject
|
133 |
+
.spyproject
|
134 |
+
|
135 |
+
# Rope project settings
|
136 |
+
.ropeproject
|
137 |
+
|
138 |
+
# mkdocs documentation
|
139 |
+
/site
|
140 |
+
|
141 |
+
# mypy
|
142 |
+
.mypy_cache/
|
143 |
+
.dmypy.json
|
144 |
+
dmypy.json
|
145 |
+
|
146 |
+
# Pyre type checker
|
147 |
+
.pyre/
|
148 |
+
|
149 |
+
# pytype static type analyzer
|
150 |
+
.pytype/
|
151 |
+
|
152 |
+
# Cython debug symbols
|
153 |
+
cython_debug/
|
154 |
+
|
155 |
+
# PyCharm
|
156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
160 |
+
#.idea/
|
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tempfile
|
2 |
+
import openai
|
3 |
+
import streamlit as st
|
4 |
+
from streamlit_chat import message
|
5 |
+
from audio_recorder_streamlit import audio_recorder
|
6 |
+
import os
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
from speech_to_text import transcribe_speech_recognition, transcribe_whisper
|
10 |
+
from utils import (
|
11 |
+
get_initial_message,
|
12 |
+
get_chatgpt_response,
|
13 |
+
update_chat,
|
14 |
+
)
|
15 |
+
|
16 |
+
# Carga las claves
|
17 |
+
load_dotenv()
|
18 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
19 |
+
|
20 |
+
# Streamlit Application
|
21 |
+
def main():
|
22 |
+
st.title("Smart Form")
|
23 |
+
st.markdown(
|
24 |
+
"""
|
25 |
+
Hackaton 2023 - Team Spaidermen
|
26 |
+
"""
|
27 |
+
)
|
28 |
+
|
29 |
+
if 'messages' not in st.session_state:
|
30 |
+
with st.spinner("Inicializando nuevo entorno..."):
|
31 |
+
st.session_state['messages'] = get_initial_message()
|
32 |
+
|
33 |
+
if st.session_state['messages']:
|
34 |
+
for i, msg in enumerate(st.session_state['messages']):
|
35 |
+
if msg['role'] == 'user':
|
36 |
+
message(msg['content'], is_user=True, key=str(i))
|
37 |
+
else:
|
38 |
+
message(msg['content'], key=str(i))
|
39 |
+
|
40 |
+
# Inicializar el texto transcrito como vac铆o
|
41 |
+
if 'transcribed_text' not in st.session_state:
|
42 |
+
st.session_state['transcribed_text'] = ''
|
43 |
+
|
44 |
+
# Grabar audio
|
45 |
+
st.session_state['text'] = ''
|
46 |
+
audio_bytes = audio_recorder(pause_threshold=5, sample_rate=16_000)
|
47 |
+
if audio_bytes:
|
48 |
+
with st.spinner('Transcribiendo...'):
|
49 |
+
# Guardar audio grabado en un archivo temporal
|
50 |
+
with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_audio:
|
51 |
+
temp_path = temp_audio.name
|
52 |
+
temp_audio.write(audio_bytes)
|
53 |
+
|
54 |
+
text = transcribe_speech_recognition(temp_path)
|
55 |
+
print(f"Texto transcrito: {text}")
|
56 |
+
|
57 |
+
# Actualizar el valor de transcribed_text con el texto transcrito
|
58 |
+
st.session_state['transcribed_text'] = text
|
59 |
+
|
60 |
+
# Mostrar el texto transcrito en el textarea
|
61 |
+
query = st.text_input("Ingresa tu texto", value=st.session_state['transcribed_text'])
|
62 |
+
|
63 |
+
if st.button("Enviar") and query:
|
64 |
+
with st.spinner("Enviando mensaje..."):
|
65 |
+
st.session_state['messages'] = update_chat(st.session_state['messages'], "user", query)
|
66 |
+
response = get_chatgpt_response(st.session_state['messages'])
|
67 |
+
st.session_state['messages'] = update_chat(st.session_state['messages'], "assistant", response)
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
main()
|
prompts/amsa_smart_form.json
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"amsa_smart_form": {
|
3 |
+
"Autor": "Team Spaidermen",
|
4 |
+
"name": "Miner Smart Form",
|
5 |
+
"version": "1.0.0",
|
6 |
+
"caracter铆sticas": {
|
7 |
+
"personalizaci贸n": {
|
8 |
+
"interacci贸n": {
|
9 |
+
"descripci贸n": "Este es el tipo de interacci贸n permitida para el usuario.",
|
10 |
+
"tipo_de_interacci贸n": {
|
11 |
+
"Texto": "El usuario proporciona informaci贸n a trav茅s de texto libre."
|
12 |
+
}
|
13 |
+
},
|
14 |
+
"verificaci贸n": {
|
15 |
+
"descripci贸n": "Estos son los niveles de verificaci贸n en el procesamiento de los datos de entrada.",
|
16 |
+
"nivel_de_verificaci贸n": {
|
17 |
+
"Estricto": "Este nivel verifica que todos los datos sean precisos y completos seg煤n los criterios predefinidos."
|
18 |
+
}
|
19 |
+
}
|
20 |
+
}
|
21 |
+
},
|
22 |
+
"rules": [
|
23 |
+
"1. Sigue el tipo de interacci贸n y nivel de verificaci贸n especificado por el administrador.",
|
24 |
+
"2. Usa plantillas predefinidas para analizar y solicitar informaci贸n.",
|
25 |
+
"3. Mant茅n una interacci贸n intuitiva y simple con el usuario.",
|
26 |
+
"4. Siempre ten en cuenta la configuraci贸n ya que representa las preferencias del administrador.",
|
27 |
+
"5. Proporciona un resumen del formulario una vez completado para su confirmaci贸n.",
|
28 |
+
"6. Prefiere el di谩logo interactivo con el usuario para obtener informaci贸n faltante."
|
29 |
+
],
|
30 |
+
"preferencias_del_administrador": {
|
31 |
+
"Descripci贸n": "Esta es la configuraci贸n/preferencias del administrador para Miner Smart Form (T脷).",
|
32 |
+
"interacci贸n": ["Texto"],
|
33 |
+
"verificaci贸n": ["Estricto"]
|
34 |
+
},
|
35 |
+
"formularios": {
|
36 |
+
"Descripci贸n": "Estos son los formularios m谩s comunes en la operaci贸n minera.",
|
37 |
+
"tipos_de_formularios": {
|
38 |
+
"Reporte de inspecci贸n": "Incluye detalles como fecha, ubicaci贸n, observaciones, medidas tomadas, etc.",
|
39 |
+
"Reporte de incidente": "Incluye detalles como fecha, hora, descripci贸n del incidente, medidas tomadas, etc.",
|
40 |
+
"Registro de mantenimiento": "Incluye detalles como equipo, fecha de mantenimiento, descripci贸n de la actividad, resultados, etc."
|
41 |
+
}
|
42 |
+
},
|
43 |
+
"init": "Saluda + nombre + version + ejecuta el formato <configuraci贸n> + pide al administrador especificar sus preferencias + seleccionar el tipo de formulario a ser completado"
|
44 |
+
}
|
45 |
+
}
|
prompts/rubrica_smart_form.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
| Criterios de Evaluaci贸n | Insuficiente (1) | B谩sico (2) | Competente (3) | Sobresaliente (4) |
|
2 |
+
|------------------------|-----------------|-------------|-----------------|------------------|
|
3 |
+
| Completitud del formulario | El formulario est谩 incompleto y falta informaci贸n cr铆tica | El formulario est谩 mayormente completo pero falta cierta informaci贸n relevante | El formulario est谩 completo pero podr铆a tener m谩s detalles | El formulario est谩 completo y detallado, no falta ninguna informaci贸n |
|
4 |
+
| Precisi贸n de la informaci贸n | La informaci贸n proporcionada es incorrecta o no tiene sentido | Alguna informaci贸n proporcionada es correcta, pero hay errores significativos | La mayor parte de la informaci贸n proporcionada es correcta, pero hay peque帽os errores | Toda la informaci贸n proporcionada es correcta y precisa |
|
5 |
+
| Consistencia de la informaci贸n | La informaci贸n proporcionada es contradictoria o inconsistente | Alguna informaci贸n proporcionada es consistente, pero hay inconsistencias significativas | La mayor parte de la informaci贸n proporcionada es consistente, pero hay peque帽as inconsistencias | Toda la informaci贸n proporcionada es consistente |
|
6 |
+
| Claridad de la escritura | Las respuestas son confusas y dif铆ciles de entender | Las respuestas son mayormente comprensibles, pero hay algunos puntos confusos | Las respuestas son claras, pero podr铆a haber m谩s detalle para facilitar la comprensi贸n | Las respuestas son muy claras y f谩ciles de entender |
|
7 |
+
| Orden y organizaci贸n | La informaci贸n est谩 desorganizada y desordenada | La informaci贸n tiene alg煤n orden, pero a煤n es dif铆cil de seguir | La informaci贸n est谩 bien organizada, pero podr铆a haber una estructura m谩s clara | La informaci贸n est谩 bien organizada y f谩cil de seguir |
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
python-dotenv
|
2 |
+
openai
|
3 |
+
streamlit
|
4 |
+
streamlit_chat
|
5 |
+
audio-recorder-streamlit
|
6 |
+
pydub
|
7 |
+
whisper-openai
|
8 |
+
SpeechRecognition
|
speech_to_text.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import whisper
|
2 |
+
import speech_recognition as sr
|
3 |
+
from pydub import AudioSegment
|
4 |
+
|
5 |
+
|
6 |
+
def transcribe_speech_recognition(file_path):
|
7 |
+
r = sr.Recognizer()
|
8 |
+
audio_file = sr.AudioFile(file_path)
|
9 |
+
|
10 |
+
with audio_file as source:
|
11 |
+
audio = r.record(source)
|
12 |
+
transcription_result = r.recognize_google(audio, language="es")
|
13 |
+
return transcription_result
|
14 |
+
|
15 |
+
# Function to convert mp3 file to wav
|
16 |
+
def convert_mp3_to_wav(mp3_path):
|
17 |
+
audio = AudioSegment.from_mp3(mp3_path)
|
18 |
+
wav_path = mp3_path.replace('.mp3', '.wav')
|
19 |
+
audio.export(wav_path, format="wav")
|
20 |
+
return wav_path
|
21 |
+
|
22 |
+
# Function to transcribe audio using OpenAI Whisper
|
23 |
+
def transcribe_whisper(file_path):
|
24 |
+
# models = ['base', 'small', 'medium', 'large', 'tiny']
|
25 |
+
model = whisper.load_model("base")
|
26 |
+
result = model.transcribe(file_path)
|
27 |
+
return result["text"]
|
utils.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import openai
|
3 |
+
|
4 |
+
def get_initial_message():
|
5 |
+
with open("prompts/amsa_smart_form.json", "r") as file:
|
6 |
+
initial_prompt = str(json.load(file))
|
7 |
+
|
8 |
+
response = get_chatgpt_response([{
|
9 |
+
"role": "user",
|
10 |
+
"content": f"[Assitant] Activando configuraci贸n. Iniciando configuraci贸n: {initial_prompt}",
|
11 |
+
}])
|
12 |
+
|
13 |
+
return [{
|
14 |
+
"role": "assistant",
|
15 |
+
"content": response
|
16 |
+
}]
|
17 |
+
|
18 |
+
def get_chatgpt_response(messages):
|
19 |
+
response = openai.ChatCompletion.create(
|
20 |
+
model="gpt-4",
|
21 |
+
messages=messages,
|
22 |
+
temperature=0,
|
23 |
+
)
|
24 |
+
return response.choices[0]["message"]["content"]
|
25 |
+
|
26 |
+
def update_chat(messages, role, content):
|
27 |
+
messages.append(
|
28 |
+
{"role": role, "content": content},
|
29 |
+
)
|
30 |
+
return messages
|