App1 / app.py
fahad11182's picture
Update app.py
1f1d539 verified
import streamlit as st
from huggingface_hub import InferenceClient
from gtts import gTTS
import os
# Initialize the Hugging Face InferenceClient
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
def generate_response(message, system_message, max_tokens, temperature, top_p):
# Prepare the conversation history
messages = [{"role": "system", "content": system_message}]
messages.append({"role": "user", "content": message})
response = ""
# Get the response from the Hugging Face model
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
response += token
return response
# Streamlit app layout
st.title("Hugging Face Chat with Voice Response")
system_message = st.text_input("System Message", value="You are a friendly chatbot.")
user_message = st.text_input("Your Message", value="")
max_tokens = st.slider("Max Tokens", 1, 2048, 512)
temperature = st.slider("Temperature", 0.1, 4.0, 0.7)
top_p = st.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95)
if st.button("Send Message"):
# Generate response from Hugging Face model
response_text = generate_response(user_message, system_message, max_tokens, temperature, top_p)
# Display the text response
st.write("Response:", response_text)
# Convert text to speech
tts = gTTS(text=response_text, lang='en')
audio_file = "response.mp3"
tts.save(audio_file)
# Play the audio file
audio_bytes = open(audio_file, "rb").read()
st.audio(audio_bytes, format="audio/mp3")