salomonsky commited on
Commit
da45dce
verified
1 Parent(s): 6c093ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -28
app.py CHANGED
@@ -1,40 +1,79 @@
1
  import streamlit as st
2
- import audioio
3
- import speech_recognition as sr
 
 
4
 
5
- def record_audio(duration=5):
6
- st.info("Grabando audio...")
 
7
 
8
- audio_data = audioio.record(duration=duration)
9
- st.audio(audio_data, format="audio/wav")
 
10
 
11
- st.success("Grabaci贸n completada.")
12
- return audio_data
 
13
 
14
- def transcribe_audio(audio_data):
15
- st.info("Transcribiendo audio...")
 
16
 
17
- recognizer = sr.Recognizer()
 
18
 
19
- try:
20
- audio_text = recognizer.recognize_sphinx(audio_data)
21
- except sr.UnknownValueError:
22
- st.warning("No se pudo reconocer el audio. 驴Intentaste grabar algo?")
23
- except sr.RequestError as e:
24
- st.error(f"Error en el reconocimiento de voz: {e}")
25
 
26
- st.success("Transcripci贸n completada.")
27
- return audio_text
 
 
 
 
28
 
29
- def main():
30
- st.title("Audio to Text Transcription")
 
 
 
 
 
 
31
 
32
- audio_data = record_audio()
 
 
33
 
34
- if st.button("Transcribir Audio"):
35
- transcribed_text = transcribe_audio(audio_data)
36
- st.subheader("Texto Transcrito:")
37
- st.write(transcribed_text)
 
 
38
 
39
- if __name__ == "__main__":
40
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import base64
3
+ import io
4
+ from huggingface_hub import InferenceClient
5
+ from gtts import gTTS
6
 
7
+ client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
8
+ pre_prompt = "Hola, te llamar谩s Chaman 3.0, una IA conductual. Tus principios son el transhumanismo ecol贸gico."
9
+ pre_prompt_sent = False
10
 
11
+ def format_prompt(message, history):
12
+ global pre_prompt_sent
13
+ prompt = "<s>"
14
 
15
+ if not pre_prompt_sent and all(f"[INST] {pre_prompt} [/INST]" not in user_prompt for user_prompt, _ in history):
16
+ prompt += f"[INST] {pre_prompt} [/INST]"
17
+ pre_prompt_sent = True
18
 
19
+ for user_prompt, bot_response in history:
20
+ prompt += f"[INST] {user_prompt} [/INST]"
21
+ prompt += f" {bot_response}</s> "
22
 
23
+ prompt += f"[INST] {message} [/INST]"
24
+ return prompt
25
 
26
+ def text_to_speech(text, speed=1.3):
27
+ tts = gTTS(text=text, lang='es')
28
+ audio_fp = io.BytesIO()
29
+ tts.write_to_fp(audio_fp)
30
+ audio_fp.seek(0)
31
+ return audio_fp
32
 
33
+ def generate(user_input, history, temperature=None, max_new_tokens=512, top_p=0.95, repetition_penalty=1.0):
34
+ global pre_prompt_sent
35
+ temperature = float(temperature) if temperature is not None else 0.9
36
+ if temperature < 1e-2:
37
+ temperature = 1e-2
38
+ top_p = float(top_p)
39
 
40
+ generate_kwargs = dict(
41
+ temperature=temperature,
42
+ max_new_tokens=max_new_tokens,
43
+ top_p=top_p,
44
+ repetition_penalty=repetition_penalty,
45
+ do_sample=True,
46
+ seed=42,
47
+ )
48
 
49
+ formatted_prompt = format_prompt(user_input, history)
50
+ stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=True)
51
+ response = ""
52
 
53
+ for response_token in stream:
54
+ response += response_token.token.text
55
+
56
+ response = ' '.join(response.split()).replace('</s>', '')
57
+ audio_file = text_to_speech(response, speed=1.3)
58
+ return response, audio_file
59
 
60
+ if "history" not in st.session_state:
61
+ st.session_state.history = []
62
+
63
+ with st.container():
64
+ user_input = st.text_input(label="Usuario", value="Saludos")
65
+ output, audio_file = generate(user_input, history=st.session_state.history)
66
+ st.text_area("Respuesta", height=400, value=output, key="output_text", disabled=True)
67
+
68
+ if user_input:
69
+ st.session_state.history.append((user_input, output))
70
+
71
+ st.write("Presiona el bot贸n y comienza a hablar...")
72
+
73
+ if audio_file is not None:
74
+ st.markdown(
75
+ f"""
76
+ <audio autoplay="autoplay" controls="controls" src="data:audio/mp3;base64,{base64.b64encode(audio_file.read()).decode()}" type="audio/mp3" id="audio_player"></audio>
77
+ """,
78
+ unsafe_allow_html=True
79
+ )