Spaces:
Sleeping
Sleeping
fahad11182
commited on
Commit
•
c738c50
1
Parent(s):
6faccd7
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
7 |
+
|
8 |
+
def generate_response(message, system_message, max_tokens, temperature, top_p):
|
9 |
+
messages = [{"role": "system", "content": system_message}]
|
10 |
+
messages.append({"role": "user", "content": message})
|
11 |
+
|
12 |
+
response = ""
|
13 |
+
for message in client.chat_completion(
|
14 |
+
messages,
|
15 |
+
max_tokens=max_tokens,
|
16 |
+
stream=True,
|
17 |
+
temperature=temperature,
|
18 |
+
top_p=top_p,
|
19 |
+
):
|
20 |
+
token = message.choices[0].delta.content
|
21 |
+
response += token
|
22 |
+
|
23 |
+
# Generate speech from the response text using gTTS
|
24 |
+
tts = gTTS(text=response, lang='en')
|
25 |
+
audio_file = "response.mp3"
|
26 |
+
tts.save(audio_file)
|
27 |
+
return response, audio_file
|
28 |
+
|
29 |
+
st.title("Hugging Face Chatbot with Voice Response")
|
30 |
+
|
31 |
+
message = st.text_input("Enter your message:")
|
32 |
+
system_message = st.text_input("System message:", value="You are a friendly Chatbot.")
|
33 |
+
max_tokens = st.slider("Max new tokens", 1, 2048, 512)
|
34 |
+
temperature = st.slider("Temperature", 0.1, 4.0, 0.7, step=0.1)
|
35 |
+
top_p = st.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95, step=0.05)
|
36 |
+
|
37 |
+
if st.button("Generate Response"):
|
38 |
+
response, audio_file = generate_response(message, system_message, max_tokens, temperature, top_p)
|
39 |
+
st.write(response)
|
40 |
+
st.audio(audio_file)
|