Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,9 @@ from huggingface_hub import InferenceClient, HfApi
|
|
3 |
import time
|
4 |
import requests
|
5 |
from requests.exceptions import RequestException
|
|
|
|
|
|
|
6 |
|
7 |
# Set page config at the very beginning
|
8 |
st.set_page_config(page_title="Phi-3.5 Chatbot", page_icon="🤖")
|
@@ -12,10 +15,7 @@ hf_token = st.text_input("Enter your Hugging Face API token", type="password")
|
|
12 |
|
13 |
@st.cache_resource
|
14 |
def get_client(token):
|
15 |
-
return InferenceClient(
|
16 |
-
"microsoft/Phi-3.5-mini-instruct",
|
17 |
-
token=token
|
18 |
-
)
|
19 |
|
20 |
def validate_token(token):
|
21 |
try:
|
@@ -67,6 +67,13 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
67 |
st.error(f"An error occurred: {str(e)}")
|
68 |
yield "I'm sorry, but I encountered an error while processing your request."
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
st.title("Phi-3.5 Mini Chatbot")
|
71 |
|
72 |
if "messages" not in st.session_state:
|
@@ -99,4 +106,8 @@ if prompt := st.chat_input("What is your message?"):
|
|
99 |
message_placeholder.markdown(response)
|
100 |
full_response = response
|
101 |
|
102 |
-
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
|
|
|
|
|
|
|
|
|
3 |
import time
|
4 |
import requests
|
5 |
from requests.exceptions import RequestException
|
6 |
+
from gtts import gTTS # Google Text-to-Speech
|
7 |
+
import tempfile
|
8 |
+
import os
|
9 |
|
10 |
# Set page config at the very beginning
|
11 |
st.set_page_config(page_title="Phi-3.5 Chatbot", page_icon="🤖")
|
|
|
15 |
|
16 |
@st.cache_resource
|
17 |
def get_client(token):
|
18 |
+
return InferenceClient("microsoft/Phi-3.5-mini-instruct", token=token)
|
|
|
|
|
|
|
19 |
|
20 |
def validate_token(token):
|
21 |
try:
|
|
|
67 |
st.error(f"An error occurred: {str(e)}")
|
68 |
yield "I'm sorry, but I encountered an error while processing your request."
|
69 |
|
70 |
+
def text_to_speech(text):
|
71 |
+
# Create a named temporary file
|
72 |
+
tmp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
73 |
+
tts = gTTS(text=text, lang='en')
|
74 |
+
tts.save(tmp_file.name)
|
75 |
+
return tmp_file.name
|
76 |
+
|
77 |
st.title("Phi-3.5 Mini Chatbot")
|
78 |
|
79 |
if "messages" not in st.session_state:
|
|
|
106 |
message_placeholder.markdown(response)
|
107 |
full_response = response
|
108 |
|
109 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
110 |
+
|
111 |
+
# Generate audio from the assistant's response
|
112 |
+
audio_file = text_to_speech(full_response)
|
113 |
+
st.audio(audio_file, format='audio/mp3')
|