Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import os
|
|
4 |
import json
|
5 |
from gtts import gTTS # Import gTTS for text-to-speech
|
6 |
|
7 |
-
|
8 |
# OpenAI API setup
|
9 |
openai.api_key = os.getenv("GROQ_API_KEY")
|
10 |
openai.api_base = "https://api.groq.com/openai/v1"
|
@@ -56,11 +55,12 @@ def get_groq_response(message, history=[]):
|
|
56 |
# Text-to-Speech function
|
57 |
def text_to_speech(latest_response):
|
58 |
try:
|
|
|
|
|
59 |
tts = gTTS(latest_response, lang="en") # Generate speech from text
|
60 |
audio_file = "response_audio.mp3"
|
61 |
tts.save(audio_file)
|
62 |
-
|
63 |
-
return audio_file # Ensure correct file path is returned
|
64 |
except Exception as e:
|
65 |
print(f"Error generating audio: {e}")
|
66 |
return None
|
@@ -98,68 +98,36 @@ with gr.Blocks(css="""
|
|
98 |
background-color: #FFFFFF;
|
99 |
border-radius: 10px;
|
100 |
padding: 20px;
|
101 |
-
max-height: 600px;
|
102 |
overflow-y: auto;
|
103 |
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
|
104 |
-
scroll-behavior: smooth;
|
105 |
}
|
106 |
|
107 |
-
.user-message {
|
108 |
-
background-color: #9ACBD0;
|
109 |
-
color: #FFF;
|
110 |
-
padding: 12px;
|
111 |
border-radius: 8px;
|
112 |
margin: 10px 0;
|
113 |
max-width: 60%;
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
text-align: right;
|
115 |
float: right;
|
116 |
clear: both;
|
117 |
-
transition: transform 0.3s ease;
|
118 |
}
|
119 |
|
120 |
.bot-message {
|
121 |
background-color: #48A6A7;
|
122 |
color: #FFF;
|
123 |
-
padding: 12px;
|
124 |
-
border-radius: 8px;
|
125 |
-
margin: 10px 0;
|
126 |
-
max-width: 60%;
|
127 |
text-align: left;
|
128 |
float: left;
|
129 |
clear: both;
|
130 |
-
transition: transform 0.3s ease;
|
131 |
-
}
|
132 |
-
|
133 |
-
.user-message:hover, .bot-message:hover {
|
134 |
-
transform: scale(1.05);
|
135 |
-
box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);
|
136 |
-
}
|
137 |
-
|
138 |
-
.gr-button {
|
139 |
-
background-color: #2973B2;
|
140 |
-
color: white;
|
141 |
-
padding: 10px 15px;
|
142 |
-
border-radius: 8px;
|
143 |
-
border: none;
|
144 |
-
transition: background-color 0.3s ease;
|
145 |
-
}
|
146 |
-
|
147 |
-
.gr-button:hover {
|
148 |
-
background-color: #21689D;
|
149 |
-
}
|
150 |
-
|
151 |
-
.gr-textbox input {
|
152 |
-
padding: 15px;
|
153 |
-
font-size: 16px;
|
154 |
-
}
|
155 |
-
|
156 |
-
.gr-markdown h1 {
|
157 |
-
color: #3A5A6E;
|
158 |
-
font-size: 28px;
|
159 |
-
text-align: center;
|
160 |
}
|
161 |
""") as demo:
|
162 |
-
gr.Markdown("
|
163 |
|
164 |
# Chatbot UI
|
165 |
chatbot_ui = gr.Chatbot()
|
@@ -174,9 +142,9 @@ with gr.Blocks(css="""
|
|
174 |
# Chat interaction
|
175 |
user_input.submit(chatbot, inputs=[user_input, history_state], outputs=[chatbot_ui, history_state, user_input])
|
176 |
hear_button.click(
|
177 |
-
lambda latest: text_to_speech(latest[-1][1] if latest else ""), #
|
178 |
-
inputs=[history_state],
|
179 |
-
outputs=audio_output
|
180 |
)
|
181 |
|
182 |
# Clear history button action
|
|
|
4 |
import json
|
5 |
from gtts import gTTS # Import gTTS for text-to-speech
|
6 |
|
|
|
7 |
# OpenAI API setup
|
8 |
openai.api_key = os.getenv("GROQ_API_KEY")
|
9 |
openai.api_base = "https://api.groq.com/openai/v1"
|
|
|
55 |
# Text-to-Speech function
|
56 |
def text_to_speech(latest_response):
|
57 |
try:
|
58 |
+
if not latest_response: # If there's no response
|
59 |
+
return None
|
60 |
tts = gTTS(latest_response, lang="en") # Generate speech from text
|
61 |
audio_file = "response_audio.mp3"
|
62 |
tts.save(audio_file)
|
63 |
+
return audio_file
|
|
|
64 |
except Exception as e:
|
65 |
print(f"Error generating audio: {e}")
|
66 |
return None
|
|
|
98 |
background-color: #FFFFFF;
|
99 |
border-radius: 10px;
|
100 |
padding: 20px;
|
101 |
+
max-height: 600px;
|
102 |
overflow-y: auto;
|
103 |
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.1);
|
104 |
+
scroll-behavior: smooth;
|
105 |
}
|
106 |
|
107 |
+
.user-message, .bot-message {
|
|
|
|
|
|
|
108 |
border-radius: 8px;
|
109 |
margin: 10px 0;
|
110 |
max-width: 60%;
|
111 |
+
padding: 12px;
|
112 |
+
}
|
113 |
+
|
114 |
+
.user-message {
|
115 |
+
background-color: #9ACBD0;
|
116 |
+
color: #FFF;
|
117 |
text-align: right;
|
118 |
float: right;
|
119 |
clear: both;
|
|
|
120 |
}
|
121 |
|
122 |
.bot-message {
|
123 |
background-color: #48A6A7;
|
124 |
color: #FFF;
|
|
|
|
|
|
|
|
|
125 |
text-align: left;
|
126 |
float: left;
|
127 |
clear: both;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
}
|
129 |
""") as demo:
|
130 |
+
gr.Markdown("# ChatGPT at Home\nAsk me anything and hear the response!")
|
131 |
|
132 |
# Chatbot UI
|
133 |
chatbot_ui = gr.Chatbot()
|
|
|
142 |
# Chat interaction
|
143 |
user_input.submit(chatbot, inputs=[user_input, history_state], outputs=[chatbot_ui, history_state, user_input])
|
144 |
hear_button.click(
|
145 |
+
lambda latest: text_to_speech(latest[-1][1] if latest else "No response yet."), # Handle empty state
|
146 |
+
inputs=[history_state],
|
147 |
+
outputs=audio_output
|
148 |
)
|
149 |
|
150 |
# Clear history button action
|