fahad11182 commited on
Commit
6252e34
·
verified ·
1 Parent(s): 4464901

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -11
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
3
  from gtts import gTTS
 
4
  import os
5
 
6
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
@@ -26,25 +27,34 @@ def generate_response(message, system_message, max_tokens, temperature, top_p):
26
  tts.save(audio_file)
27
  return response, audio_file
28
 
29
- st.title("Chatbot with Voice Response")
 
 
 
30
 
31
- message = st.text_input("Enter your message:")
32
- submit = st.button("Generate Response")
33
 
34
- if submit and message:
35
- system_message = st.text_input("System message:", value="You are a friendly Chatbot.")
36
- max_tokens = st.slider("Max new tokens", 1, 2048, 512)
37
- temperature = st.slider("Temperature", 0.1, 4.0, 0.7, step=0.1)
38
- top_p = st.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95, step=0.05)
39
 
 
40
  response, audio_file = generate_response(message, system_message, max_tokens, temperature, top_p)
41
  st.write(response)
42
 
43
- # Add HTML to autoplay the audio
 
 
 
44
  audio_html = f"""
45
- <audio autoplay>
46
- <source src="data:audio/mpeg;base64,{open(audio_file, "rb").read().encode("base64").decode()}" type="audio/mp3">
47
  Your browser does not support the audio element.
48
  </audio>
49
  """
50
  st.markdown(audio_html, unsafe_allow_html=True)
 
 
 
 
1
  import streamlit as st
2
  from huggingface_hub import InferenceClient
3
  from gtts import gTTS
4
+ import base64
5
  import os
6
 
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
27
  tts.save(audio_file)
28
  return response, audio_file
29
 
30
+ def get_base64_audio(audio_file):
31
+ with open(audio_file, "rb") as f:
32
+ audio_data = f.read()
33
+ return base64.b64encode(audio_data).decode()
34
 
35
+ st.title("Hugging Face Chatbot with Voice Response")
 
36
 
37
+ message = st.text_input("Enter your message:")
38
+ system_message = st.text_input("System message:", value="You are a friendly Chatbot.")
39
+ max_tokens = st.slider("Max new tokens", 1, 2048, 512)
40
+ temperature = st.slider("Temperature", 0.1, 4.0, 0.7, step=0.1)
41
+ top_p = st.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95, step=0.05)
42
 
43
+ if st.button("Generate Response"):
44
  response, audio_file = generate_response(message, system_message, max_tokens, temperature, top_p)
45
  st.write(response)
46
 
47
+ # Get base64 encoded audio data
48
+ audio_base64 = get_base64_audio(audio_file)
49
+
50
+ # HTML to autoplay the audio
51
  audio_html = f"""
52
+ <audio controls autoplay>
53
+ <source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
54
  Your browser does not support the audio element.
55
  </audio>
56
  """
57
  st.markdown(audio_html, unsafe_allow_html=True)
58
+
59
+ # Clean up the audio file
60
+ os.remove(audio_file)