Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -4,13 +4,19 @@ from datetime import datetime
|
|
4 |
from pydub import AudioSegment
|
5 |
import whisper
|
6 |
import requests
|
|
|
7 |
|
8 |
-
# π Load API key
|
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."}]
|
@@ -27,7 +33,8 @@ def chat_with_groq(message, history):
|
|
27 |
}
|
28 |
|
29 |
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
|
30 |
-
|
|
|
31 |
|
32 |
history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
|
33 |
return "", history, history
|
@@ -71,52 +78,78 @@ def load_chat(name):
|
|
71 |
# π Gradio UI
|
72 |
|
73 |
with gr.Blocks(css="""
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
""") as demo:
|
112 |
-
# ... same interface elements and event bindings as before ...
|
113 |
|
114 |
state = gr.State([])
|
115 |
|
116 |
gr.Markdown("# β¨ JAWERIA'SBOT π€")
|
117 |
-
gr.Markdown("
|
118 |
|
119 |
-
chatbot = gr.Chatbot(type="messages", height=
|
120 |
chat_input = gr.Textbox(label="π¬ Message", placeholder="Type or speak your message...")
|
121 |
|
122 |
send_btn = gr.Button("Send π")
|
@@ -130,8 +163,9 @@ with gr.Blocks(css="""
|
|
130 |
save_btn = gr.Button("πΎ Save")
|
131 |
saved_dropdown = gr.Dropdown(label="π Load Saved", choices=list_saved_files())
|
132 |
load_btn = gr.Button("π₯ Load")
|
133 |
-
|
134 |
-
|
|
|
135 |
|
136 |
# π Bind events
|
137 |
send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|
|
|
4 |
from pydub import AudioSegment
|
5 |
import whisper
|
6 |
import requests
|
7 |
+
import emoji
|
8 |
|
9 |
+
# π Load API key
|
10 |
groq_key = os.getenv("GROQ_API_KEY")
|
11 |
|
12 |
# π§ Load Whisper model
|
13 |
whisper_model = whisper.load_model("base")
|
14 |
|
15 |
+
# π― Emoji filter
|
16 |
+
def filter_emojis(text):
|
17 |
+
allowed_emojis = {"π", "π", "π", "π€", "β¨", "π", "π¬", "π", "π", "π’", "π§ ", "β
"}
|
18 |
+
return "".join(char if char not in emoji.EMOJI_DATA or char in allowed_emojis else "" for char in text)
|
19 |
+
|
20 |
# π¬ Chat function
|
21 |
def chat_with_groq(message, history):
|
22 |
messages = [{"role": "system", "content": "You are JAWERIA'SBOT π€ β cheerful, emoji-savvy, and sleek."}]
|
|
|
33 |
}
|
34 |
|
35 |
response = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
|
36 |
+
raw_reply = response.json()["choices"][0]["message"]["content"]
|
37 |
+
reply = filter_emojis(raw_reply)
|
38 |
|
39 |
history += [{"role": "user", "content": message}, {"role": "assistant", "content": reply}]
|
40 |
return "", history, history
|
|
|
78 |
# π Gradio UI
|
79 |
|
80 |
with gr.Blocks(css="""
|
81 |
+
body {
|
82 |
+
background-color: #111 !important;
|
83 |
+
font-family: 'Segoe UI', sans-serif !important;
|
84 |
+
margin: 0;
|
85 |
+
padding: 0;
|
86 |
+
}
|
87 |
+
|
88 |
+
.gr-block {
|
89 |
+
padding: 1rem;
|
90 |
+
}
|
91 |
+
|
92 |
+
.gr-chatbot {
|
93 |
+
border-radius: 10px;
|
94 |
+
border: 1px solid #333;
|
95 |
+
background-color: #1e1e1e !important;
|
96 |
+
}
|
97 |
+
|
98 |
+
.gr-chatbot-message {
|
99 |
+
background-color: #292929 !important;
|
100 |
+
color: #f1f1f1 !important;
|
101 |
+
border-radius: 10px;
|
102 |
+
margin-bottom: 5px;
|
103 |
+
}
|
104 |
+
|
105 |
+
textarea, input[type='text'] {
|
106 |
+
background-color: #222 !important;
|
107 |
+
color: #fff !important;
|
108 |
+
border-radius: 8px !important;
|
109 |
+
border: 1px solid #444 !important;
|
110 |
+
padding: 10px;
|
111 |
+
}
|
112 |
+
|
113 |
+
.gr-button {
|
114 |
+
background-color: #333 !important;
|
115 |
+
color: #fff !important;
|
116 |
+
border: 1px solid #444 !important;
|
117 |
+
border-radius: 8px;
|
118 |
+
padding: 8px 14px;
|
119 |
+
font-weight: bold;
|
120 |
+
transition: background-color 0.2s;
|
121 |
+
}
|
122 |
+
|
123 |
+
.gr-button:hover {
|
124 |
+
background-color: #555 !important;
|
125 |
+
cursor: pointer;
|
126 |
+
}
|
127 |
+
|
128 |
+
.gr-dropdown {
|
129 |
+
background-color: #222 !important;
|
130 |
+
color: #fff !important;
|
131 |
+
border-radius: 8px;
|
132 |
+
border: 1px solid #444;
|
133 |
+
padding: 8px;
|
134 |
+
}
|
135 |
+
|
136 |
+
h1, h2, h3, .gr-markdown {
|
137 |
+
color: #f1f1f1 !important;
|
138 |
+
text-align: center;
|
139 |
+
}
|
140 |
+
|
141 |
+
audio {
|
142 |
+
background-color: #000 !important;
|
143 |
+
border: 1px solid #333 !important;
|
144 |
+
}
|
145 |
""") as demo:
|
|
|
146 |
|
147 |
state = gr.State([])
|
148 |
|
149 |
gr.Markdown("# β¨ JAWERIA'SBOT π€")
|
150 |
+
gr.Markdown("Speak or type β your assistant listens and replies with text and emojis π¬")
|
151 |
|
152 |
+
chatbot = gr.Chatbot(type="messages", height=400)
|
153 |
chat_input = gr.Textbox(label="π¬ Message", placeholder="Type or speak your message...")
|
154 |
|
155 |
send_btn = gr.Button("Send π")
|
|
|
163 |
save_btn = gr.Button("πΎ Save")
|
164 |
saved_dropdown = gr.Dropdown(label="π Load Saved", choices=list_saved_files())
|
165 |
load_btn = gr.Button("π₯ Load")
|
166 |
+
|
167 |
+
save_msg = gr.Markdown()
|
168 |
+
load_msg = gr.Markdown()
|
169 |
|
170 |
# π Bind events
|
171 |
send_btn.click(chat_with_groq, inputs=[chat_input, state], outputs=[chat_input, chatbot, state])
|