Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,13 +5,13 @@ from pydub import AudioSegment
|
|
5 |
import whisper
|
6 |
import requests
|
7 |
|
8 |
-
# π API key
|
9 |
-
groq_key = "
|
10 |
|
11 |
-
# π§ Load Whisper
|
12 |
-
whisper_model = whisper.load_model("base")
|
13 |
|
14 |
-
# π¬ Chat
|
15 |
def chat_with_groq(message, history):
|
16 |
messages = [{"role": "system", "content": "You are JAWERIA'SBOT π€ β cheerful, emoji-savvy, and sleek."}]
|
17 |
messages += history + [{"role": "user", "content": message}]
|
@@ -32,7 +32,7 @@ def chat_with_groq(message, history):
|
|
32 |
history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
|
33 |
return "", history, history
|
34 |
|
35 |
-
# ποΈ
|
36 |
def transcribe_audio(audio_path):
|
37 |
if audio_path is None or not os.path.exists(audio_path):
|
38 |
return "β οΈ No audio recorded."
|
@@ -68,17 +68,39 @@ def load_chat(name):
|
|
68 |
except Exception as e:
|
69 |
return [], [], f"β Load error: {e}"
|
70 |
|
71 |
-
#
|
72 |
with gr.Blocks(css="""
|
73 |
-
body {
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
""") as demo:
|
78 |
|
79 |
state = gr.State([])
|
80 |
|
81 |
-
gr.Markdown("
|
82 |
gr.Markdown("<div style='text-align:center;'>Speak or type β your assistant listens and replies with text π¬</div>")
|
83 |
|
84 |
chatbot = gr.Chatbot(type="messages", height=350)
|
@@ -98,7 +120,7 @@ input[type='text'], textarea, select, .gr-textbox {background-color: #222 !impor
|
|
98 |
save_msg = gr.Markdown()
|
99 |
load_msg = gr.Markdown()
|
100 |
|
101 |
-
# π Bind
|
102 |
send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
103 |
chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
104 |
voice_btn.click(transcribe_audio, inputs=[voice_input], outputs=[chat_input])
|
|
|
5 |
import whisper
|
6 |
import requests
|
7 |
|
8 |
+
# π Load API key securely from environment
|
9 |
+
groq_key = os.getenv("GROQ_API_KEY")
|
10 |
|
11 |
+
# π§ Load Whisper model
|
12 |
+
whisper_model = whisper.load_model("base")
|
13 |
|
14 |
+
# π¬ Chat function
|
15 |
def chat_with_groq(message, history):
|
16 |
messages = [{"role": "system", "content": "You are JAWERIA'SBOT π€ β cheerful, emoji-savvy, and sleek."}]
|
17 |
messages += history + [{"role": "user", "content": message}]
|
|
|
32 |
history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
|
33 |
return "", history, history
|
34 |
|
35 |
+
# ποΈ Audio transcription
|
36 |
def transcribe_audio(audio_path):
|
37 |
if audio_path is None or not os.path.exists(audio_path):
|
38 |
return "β οΈ No audio recorded."
|
|
|
68 |
except Exception as e:
|
69 |
return [], [], f"β Load error: {e}"
|
70 |
|
71 |
+
# π Gradio UI
|
72 |
with gr.Blocks(css="""
|
73 |
+
body {
|
74 |
+
background-color: #111 !important;
|
75 |
+
color: white !important;
|
76 |
+
}
|
77 |
+
* {
|
78 |
+
color: white !important;
|
79 |
+
}
|
80 |
+
h1 {
|
81 |
+
text-align: center !important;
|
82 |
+
color: #00ccff !important;
|
83 |
+
margin: auto !important;
|
84 |
+
}
|
85 |
+
.gr-chatbot-message, .gr-textbox, textarea, input[type='text'],
|
86 |
+
select, .gr-dropdown, .gr-markdown, .gr-label {
|
87 |
+
background-color: #222 !important;
|
88 |
+
color: white !important;
|
89 |
+
border-radius: 6px;
|
90 |
+
}
|
91 |
+
.gr-button {
|
92 |
+
background-color: #007acc !important;
|
93 |
+
color: white !important;
|
94 |
+
}
|
95 |
+
.gr-dropdown {
|
96 |
+
max-height: 150px;
|
97 |
+
overflow-y: auto;
|
98 |
+
}
|
99 |
""") as demo:
|
100 |
|
101 |
state = gr.State([])
|
102 |
|
103 |
+
gr.Markdown("# β¨ JAWERIA'SBOT π€")
|
104 |
gr.Markdown("<div style='text-align:center;'>Speak or type β your assistant listens and replies with text π¬</div>")
|
105 |
|
106 |
chatbot = gr.Chatbot(type="messages", height=350)
|
|
|
120 |
save_msg = gr.Markdown()
|
121 |
load_msg = gr.Markdown()
|
122 |
|
123 |
+
# π Bind events
|
124 |
send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
125 |
chat_input.submit(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
126 |
voice_btn.click(transcribe_audio, inputs=[voice_input], outputs=[chat_input])
|